I need to export some data from a few tables to a excel file. The thing is, the only working extension is '.xls', which is too old. So when I open this file it shows in protected exibition mode, even if I uncheck all boxes in Excel Protected Exibition Mode Options.
I'm not sure why I can't save in other extensions, like .xlsx
, because if I use the native function to save as .xlsx
: STR_OOXML_EXCEL_EXTENSION
or just write .xlsx
it saves, but does not open the document.
Shows this message: Excel cannot open the file 'file.xlsx' bacause the format or extension is not valid. Check if the document is corrupted or the extension matches the file format.
This is the code I'm using to create the file:
MyWorkbook := TsWorkbook.Create;
try
MyWorksheet := MyWorkbook.AddWorksheet('Orçamento');
And this is the one I'm using to save:
MyWorkbook.WriteToFile(MyDir + orcamento + '.xls', OUTPUT_FORMAT, True);
finally
MyWorkbook.Free;
The only option I can think of, is using another package, Excel or OpenOffice could help.
Settings: Lazarus 1.4.2 with FPC 2.6.4 and fpSpreadsheet 1.6.0, running on Windows 7 64-bit.
I'm not sure what your variable or constant OUTPUT_FORMAT contains but if you specify a format it should be one of these:
sfExcel2, sfExcel5, sfExcel8, sfOOXML, sfOpenDocument,
sfCSV, sfHTML, sfWikiTable_Pipes, sfWikiTable_WikiMedia
So if you want to save as .xlsx you could do this (use sfOOXML as format):
uses fpspreadsheet, fpstypes, xlsxooxml;
//...
MyWorkbook.WriteToFile(MyDir + orcamento + '.xlsx', sfOOXML, True);
You could also do this:
uses fpspreadsheet, fpstypes, xlsxooxml;
//...
MyWorkbook.WriteToFile(MyDir + orcamento + '.xlsx', True);
It will automatically detect the .xlsx extension and save the proper format.
You do need to include xlsxooxml in your uses clause because that unit registers the xlsx-format (automatically).