Search code examples
c#excelolegembox-spreadsheet

Gembox error when reading an .xls file from .xlsx file


I have an .xlsx file and I have changed the file extension to .xls and I am trying to read it using Gembox library. The problem is that when I am trying to read the file using GemboxExcel.LoadXls(fileName); method I get the following error:

Exception message: Analysis failed: Reading error: file is not a valid OLE2 Compound File. Exception stack trace: System.Exception: Analysis failed: Reading error: file is not a valid OLE2 Compound File. ---> GemBox.CompoundFileException: Reading error: file is not a valid OLE2 Compound File. at GemBox.ReadData.ReadHeader(BinaryReader br, ArrayList& masterAllocationTable) at GemBox.ReadData..ctor(Ole2CompoundFile ole2File, Stream inputStream)
at GemBox.Ole2CompoundFile.Load(Stream stream, Boolean loadOnDemand)
at GemBox.Spreadsheet.ExcelFile.ReadStreamHelper(ExcelFile excelFile, Stream inputStream, Boolean readSummaryStreams, Byte[]& ss, Byte[]& dss, Boolean readMacros, Byte[]& ctls, Byte[]& compObj, Ole2Storage& mStorage, String fileName) at GemBox.Spreadsheet.ExcelFile.LoadXls(String fileName, XlsOptions xlsOptions

The problem is that if that if I save the file from Excel (Microsoft Excel -> Save As -> .xls file) and then I open the file in my program and use the GemboxExcel.LoadXls(fileName); method, it works correctly.

I have installed the Microsoft Office Compatibility Pack on my machine but it does not work.

Did someone came across this issue?


Solution

  • By changing files extension just by renaming it (for example from "Book1.xlsx" to "Book1.xls") you are not changing the content of this file nor the file format used in it.

    You see these formats are very different, XLS is a binary based file format while XLSX is an XML based file format. I'm not sure why you want to rename it to an .XLS extension, but nevertheless now you have a file in which file format and extension don't match and you need to load it as an XLSX file (because it still is an XLSX file format).

    ExcelFile file = new ExcelFile();
    file.LoadXlsx("Book1.xls", XlsxOptions.None);
    

    If you want to convert the XLSX file to an XLS file then try the following:

    ExcelFile file = new ExcelFile();
    file.LoadXlsx("Book1.xlsx", XlsxOptions.None);
    file.SaveXls("Book1.xls");
    

    This will have the same effect just as "Microsoft Excel -> Save As -> .xls file" action.