FlexCel created corrupted xlsx file - c#

I use the FlexCel library to create an Excel report in the .xlsx format.
When creating a file in .xls format - everything works fine.
When I try to create a file in the .xlsx format, the file is created, but when I open it by Excel, I get an error that the file is corrupt and can not be opened. And the file has the size in half from .xls.
If someone has encountered a similar problem or knows a solution, I will be very grateful for the answer.
Edit:
My code
var templateFilePath = "D:/template.xlsx";
var newReportPath = "D:/report.xlsx";
using (var fr = new FlexCelReport(true))
{
fr.AddTable("SOReport", dataTable);
fr.Run(
templateFilePath,
newReportPath
);
}

I had the same problem with export in xlsx format with old versions of FlexCel.
Currently I've tested it on FlexCel v5.5.1.0 and there is interesting behaviour:
If I use Excel 2016 or above to create xlsx templates - then I get "file is corrupted" error while trying to open exported xlsx file.
But if I use Excel 2013 or below to create xlsx templates - then I can open it perfectly without errors.
Also mention that if you open xlsx template created by Excel 2013 or below in Excel 2016 or above and save it - you can't restore it to working state in the future. Template will lost for you.
Too late but I hope this can help you.
P.S. Probably, FlexCel team (TMS Software) fixed it starting with v6.7.16.0 version of FlexCel. I think so because documentation said something similar: https://www.tmssoftware.com/site/flexcelnet.asp?s=history
Points like Improved : Improved compatibility with invalid xls and xlsx files
But I can't argue that.

Related

How to read excel application version from xls (Excel97) files?

When using NPOI WorkbookFactory with a "modern" Excel file (*.xlsx) it produces an XSSFWorkbook, which contains the Excel-version:
xssfWorkbook.GetProperties().ExtendedProperties.AppVersion
This returns the Excel version number, e.g. "16.0300".
Is there a way to get this information for a HSSFWorkbook?
BTW: The name of the application is available in both classes:
hssfWorkbook.SummaryInformation.ApplicationName
xssfWorkbook.GetProperties().ExtendedProperties.Application
For MS Excel, this is "Microsoft Excel" in both cases.
But I couldn't find any kind of version information for HSSF.
Usecase: I get a lot of files from different sources and for support questions, it would be very helpful to know the Excel version a particular source uses. As you can also save a .xlsx as .xls, the BIFF version alone would not be of much help.

Converting any file to .xlsx leads error in opening it

I am saving as any file to .xlsx format. If the original file is not with .xlsx extension, it is throwing exception while trying to open it. The exception message is :-
Excel cannot open the file 'abc.xlsx 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.
Whereas if conversion is in .xls format, I can open the converted file with warning message.
The file you are trying to open, abc.xls, is in a different format
than specified by the file extension. Verify that the file is not
corrupted and is from a trusted source before opening the file. Do you
want to open the file now?
I need to convert file to .xlsx format by C# code regardless its extenstion and open it by Excel 2010.
Thats not how file-types work!
You can not simply rename a file and it gets converted to a different type.
Renaming *.xls to *.xlsx works, because both are Excel files which can be opened by MS Excel, but for all other types (except some other which Excel can handle, like e.g. *.csv) you need to read the file and "manually" convert them.
To write *.xlsx using C# you can use e.g. EPPlus (NuGet).
You need a library that convert the files for you.
I see that you need to open files from Office 2003, so you need to use something like NPOI
Unfortunately, even if EPPLUS is a great library for Office's files, it only support the OXML Documents like .xlsx or .docx but not the .xls.
NPOI is a free opensource library to work with Office 2003->2010/3 files.
Here is the link

Is there any Open source Library to generate and edit .xls and .ppt file?

My task is to extract information from ppt file and compare with another ppt file. I have to use windows platform so i used excel and powerpoint's COM object.
After developing and testing the code in my computer when i exported the file into another computer it just failed. I started to debug in that file, and surprisingly debug was successful from first to last but as soon as click on the executable it fails.
I am totally *ucked up now cause inspite of updating excel dll files into those computers (which will serve the com support) it's not working. In every pc it gives different error message. I don't wanna work COM object anymore. It *ucks.
Can anyone suggest me any open source alternative in which i can read and extract information from Excel and Powerpoint files (both 2003 and 2007 format) ???
My preferred language is c# ........I got an paid solution (aspose) but i don't wanna go for that.
Any help will be appreciated. Just suggest me how can i deal with office files assuming ms office is not installed in my computer.
NPOI works well for Excel; I only know to use the built in, MS object library for PPT files.

export XML -> Excel

I'm already generating an excel XML file (.xml) containing 2 worksheets.
issue #1:
the people downloading the XML file wouldn't know to open the file in Excel. (they'd complain.)
solution #1:
I rename the file to .XLS instead of .XML.
issue #2:
when opening the file in Excel i get the this error:
The file you are trying to open, ‘XXX.xls’ is in a different format than specified by the file extension. Verify that the file is not corrupted and is from a trusted source before opening the file.
Solution #2:
Convert the XML file to XLS file somehow.
so here's where i turn to you...
Goal:
they need to be able to open the file by double-clicking it.
i don't want to have to rebuild the excel file using some 3rd party library.
Ideas:
perhaps convert XML to xls using tools like the following:
http://npoi.codeplex.com/
http://excelpackage.codeplex.com/
I'd need some sample code of the simplest and LITEst way of achieving this.
any other ideas?
Just a random thought: if you're generating XML for people to open with Excel, why not generate XSL (or maby XSLX) in the first place?

Programmatically finding an Excel file's Excel version

I'm using an OleDbConnection to connect to a spreadsheet from a C# program. One of the parameters in the connection string is the Excel version.
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Book1.xls;Extended Properties="Excel 8.0;HDR=YES"
Given the path of an Excel file how can I find out which Excel format version it uses?
Thanks in advance,
T.
I addition to what was said and apart from using Excel automation to open the file you can try reading file version from your code:
xls files: those are saved as structured storage. you can use the technique from the article here: How To Determine Which Version of Excel Wrote a Workbook
xlsx files: you can open them as zip files. Version is in app.xml file AppVersion field.
Download OLE File Property Reader. Register dsofile.dll with regsvr32 and add it as a reference in your application. The following code will output the type of Excel file. It will not work on xlsx/docx since those are not OLE compunds object files, but should work on all older Office formats (2007/2003/XP).
var doc = new OleDocumentPropertiesClass();
doc.Open(#"c:\spreadhseet.xls", false, dsoFileOpenOptions.dsoOptionDefault);
Console.WriteLine(doc.OleDocumentType);

Categories