How open XLSM-File?

Hi,

I want to open a XLSM-File and save it to another location.
I can build the solution, but the target document cannot be opened.

I got the error "Excel cannot open the file ‘file.xlsm’ because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extenstion matches the format of the file."

I tried it with different LoadFormats (f.e. LoadFormat.Auto and LoadFormat.Xlsx) and I set the FileFormat of the workbook-Object to FileFormatType.Xlsm.

Can you tell me, what I’m doing wrong?
Thanks

Hi,

Please try this code to open xlsm file.

C#


string path = @“F:\Shak-Data-RW\Downloads\book.xlsm”;


Workbook workbook = new Workbook(path);

workbook.Save(path + “.out.xlsm”, SaveFormat.Xlsm);

Hi,

Thanks for your answer.

I have to open the document from a stream, because it is stored in Sharepoint.

Here is my code:

LoadFormat loadFormat = LoadFormat.Auto;
SaveFormat saveFormat = SaveFormat.Excel97To2003;

if(fileExtension == “xlsm”)
{
loadFormat = LoadFormat.Auto;
saveFormat = saveFormat.Xlsm;
}

LoadOptions loadOptions = new LoadOptions(loadFormat);
Workbook workbook = new Workbook(item.File.OpenBinaryStream(), loadOptions);

// do something

workbook.Save(memoryStream, saveFormat);

But it doesn’t work.
I think the loadFormat is not correct, I already tried different configurations, but none worked.

Hi,

I have tried filestream and it worked fine. Please see the code below and the output xlsm file which is working fine.

If your problem does not vanish, then please provide us your input/source xlsm file. We will check it soon.

C#


string path = @“F:\Shak-Data-RW\Downloads\Book1.xlsm”;


FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);


Workbook workbook = new Workbook(fs);


workbook.Save(path + “.out.xlsm”, SaveFormat.Xlsm);

Hi,

I found now a solution:

When I open the workbook without the LoadOptions-Parameter it works, with the parameter it doesn't.

I made now a workaround, that I use the constructor Workbook(Stream stream) when using a xlsm-File, while using another type (f.e xlsx) I will use Workbook(Stream stream, LoadOptions loadOptions).

But I think, this is a bug, that this constructor is not working with xlsm-Files.

Kind regards

Hi,

It’s good to know your problem has been sorted out. We will investigate your feedback and update you asap.

Hello


I’ve been experiencing this too.
When I need to load in an excel file; i want to specify the extension (loadformat) of the file. Now for .xlsx files, this is no problem so I can set the saveformat according to the workbook.Fileformat.

But workign with .xlsm files, i cannot set the .xlsm loadformat because it’s not there. When I try to call the workbook.fileformat, it says it’s .xlsx and therefore saves it as an .xlsx document.

Is there any possible way to let the workbook know it is a .xlsm file (similar to the case where it’s .xlsx)?

Hi,

Please also see this code, it first detects the fileformat and then save it using the detected fileformat.

I have attached the source and output xlsm file.

C#


string filePath = @“C:\Documents and Settings\Home\Desktop\Book1.xlsm”;


FileFormatType fmt = CellsHelper.DetectFileFormat(filePath);


Workbook workbook = new Workbook(filePath);

workbook.Save(filePath + “.out.” + fmt.ToString());

moved to priority support

http://www.aspose.com/community/forums/336398/showthread.aspx#336398