简体   繁体   English

打开损坏的.xls 文件

[英]Open corrupted .xls file

I'm using ExcelDataReader to read.xls files and extract data.我正在使用 ExcelDataReader 读取.xls 文件并提取数据。 However, I cannot open.xls files which I got from external device due to Invalid file signature exception (I can open it with Excel, but it gives me a pop up that file format and extension don't match and if I want to open it anyway).但是,由于无效的文件签名异常,我无法打开从外部设备获取的.xls 文件(我可以使用 Excel 打开它,但它给了我一个文件格式和扩展名不匹配的弹出窗口,如果我想打开无论如何)。

FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read);
IExcelDataReader excelReader = ExcelReaderFactory.CreateReader(stream);

I have tried to open it using Interop, however, got *** .xls file cannot be accessed.我尝试使用 Interop 打开它,但是,无法访问 *** .xls 文件。 The file may be corrupted, located on a server that is not responding, or read-only exception.该文件可能已损坏、位于没有响应的服务器上或只读异常。

Missing missing = Missing.Value;
Application excel = new Application();
Workbook workbook = excel.Workbooks.Open(filePath,
         missing, missing, missing, missing, missing,
         missing, missing, missing, missing, missing,
         missing, missing, missing, XlCorruptLoad.xlRepairFile);

Is there any way to repair/restore/open/read those corrupted files?有没有办法修复/恢复/打开/读取那些损坏的文件?

ps Open manually with Excel and do SaveAs is not an option as I would need to do it for hundred of files. ps 使用 Excel 手动打开并执行SaveAs不是一个选项,因为我需要为数百个文件执行此操作。

十六进制内容

From https://support.microsoft.com/en-gb/office/file-formats-that-are-supported-in-excel-0943ff2c-6014-4e8d-aaea-b83d51d46247 these files are XMLSS type (2003 xml) and should be given a.xml extension.https://support.microsoft.com/en-gb/office/file-formats-that-are-supported-in-excel-0943ff2c-6014-4e8d-aaea-b83d51d46247这些文件是 XMLSS 类型(2003 xml)和应该给出一个.xml 扩展名。 This will stop Excel complaining that the contents don't match the name and hopefully will stop the error when trying via interop.这将停止 Excel 抱怨内容与名称不匹配,并希望在通过互操作尝试时停止错误。

If interop doesn't work out you might find a library that can deal with XMLSS such as https://www.codeproject.com/Articles/8459/XmlSS-NET-Spreadsheet-Component or plain up reading it as an XML file and extracting the info you need.如果互操作不起作用,您可能会找到一个可以处理 XMLSS 的库,例如https://www.codeproject.com/Articles/8459/XmlSS-NET-Spreadsheet-Component或将其作为 XML 文件和提取您需要的信息。 It might be that reading it as a DataSet ( da = new DataSet() then ds.ReadXml(path_to_file) ) gives an easy way to work with the data可能将其作为数据集读取( da = new DataSet()然后ds.ReadXml(path_to_file) )提供了一种处理数据的简单方法

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

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