0

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?

Tamas Ionut
  • 4,240
  • 5
  • 36
  • 59

1 Answers1

2

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.

GemBox Dev Team
  • 669
  • 5
  • 18
  • Yep, I have found eventually the solution. Is there some way to make this work for BIFF5 file (older .xls files)? It throws an error when I do the same thing with that kind of file, but it works with normal (BIFF8) .xls files. – Tamas Ionut Mar 13 '14 at 09:36
  • Hi, unfortunately no. Currently GemBox.Spreadsheet only supports XLS files with BIFF8 binary file format which is for Excel files 97 – 2003. I presume you are using an older version of GemBox.Spreadsheet, but note that Excel 5.0/95 binary file format (BIFF5) is also unsupported in currently latest version (3.7). – GemBox Dev Team Mar 13 '14 at 09:42
  • @GemBoxDevTeam how can i get binary from DocumentModel object? – lazydeveloper Mar 07 '19 at 11:06