简体   繁体   English

从 .xlsx 文件读取 .xls 文件时出现 Gembox 错误

[英]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.我有一个.xlsx文件,我已将文件扩展名更改为.xls ,我正在尝试使用 Gembox 库读取它。 The problem is that when I am trying to read the file using GemboxExcel.LoadXls(fileName);问题是,当我尝试使用GemboxExcel.LoadXls(fileName);读取文件时GemboxExcel.LoadXls(fileName); method I get the following error:方法我收到以下错误:

Exception message: Analysis failed: Reading error: file is not a valid OLE2 Compound File.异常消息:分析失败:读取错误:文件不是有效的 OLE2 复合文件。 Exception stack trace: System.Exception: Analysis failed: Reading error: file is not a valid OLE2 Compound File.异常堆栈跟踪:System.Exception:分析失败:读取错误:文件不是有效的 OLE2 复合文件。 ---> GemBox.CompoundFileException: Reading error: file is not a valid OLE2 Compound File. ---> GemBox.CompoundFileException:读取错误:文件不是有效的 OLE2 复合文件。 at GemBox.ReadData.ReadHeader(BinaryReader br, ArrayList& masterAllocationTable) at GemBox.ReadData..ctor(Ole2CompoundFile ole2File, Stream inputStream)在 GemBox.ReadData.ReadHeader(BinaryReader br, ArrayList& masterAllocationTable) 在 GemBox.ReadData..ctor(Ole2CompoundFile ole2File, Stream inputStream)
at GemBox.Ole2CompoundFile.Load(Stream stream, Boolean loadOnDemand)在 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在 GemBox.Spreadsheet.ExcelFile.ReadStreamHelper(ExcelFile excelFile, Stream inputStream, Boolean readSummaryStreams, Byte[]& ss, Byte[]& dss, Boolean readMacros, Byte[]& ctls, Byte[]& compObj, Ole2Storage& mStorage, String fileName ) 在 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);问题是,如果我从 Excel 保存文件(Microsoft Excel -> 另存为 -> .xls 文件),然后在我的程序中打开该文件并使用GemboxExcel.LoadXls(fileName); method, it works correctly.方法,它工作正常。

I have installed the Microsoft Office Compatibility Pack on my machine but it does not work.我已经在我的机器上安装了Microsoft Office 兼容包,但它不起作用。

Did someone came across this issue?有人遇到过这个问题吗?

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.通过仅通过重命名(例如从“Book1.xlsx”到“Book1.xls”)来更改文件扩展名,您不会更改此文件的内容或其中使用的文件格式。

You see these formats are very different, XLS is a binary based file format while XLSX is an XML based file format.您会看到这些格式非常不同,XLS 是一种基于二进制的文件格式,而 XLSX 是一种基于 XML 的文件格式。 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).我不确定为什么要将其重命名为 .XLS 扩展名,但是现在您有一个文件格式和扩展名不匹配的文件,您需要将其作为 XLSX 文件加载(因为它仍然是一个XLSX 文件格式)。

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:如果要将 XLSX 文件转换为 XLS 文件,请尝试以下操作:

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.这将具有与“Microsoft Excel -> 另存为 -> .xls 文件”操作相同的效果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM