![](/img/trans.png)
[英]OleDbConnection locks Excel xls file if the file is corrupted
[英]Open corrupted .xls file
我正在使用 ExcelDataReader 讀取.xls 文件並提取數據。 但是,由於無效的文件簽名異常,我無法打開從外部設備獲取的.xls 文件(我可以使用 Excel 打開它,但它給了我一個文件格式和擴展名不匹配的彈出窗口,如果我想打開無論如何)。
FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read);
IExcelDataReader excelReader = ExcelReaderFactory.CreateReader(stream);
我嘗試使用 Interop 打開它,但是,無法訪問 *** .xls 文件。 該文件可能已損壞、位於沒有響應的服務器上或只讀異常。
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);
有沒有辦法修復/恢復/打開/讀取那些損壞的文件?
ps 使用 Excel 手動打開並執行SaveAs不是一個選項,因為我需要為數百個文件執行此操作。
從https://support.microsoft.com/en-gb/office/file-formats-that-are-supported-in-excel-0943ff2c-6014-4e8d-aaea-b83d51d46247這些文件是 XMLSS 類型(2003 xml)和應該給出一個.xml 擴展名。 這將停止 Excel 抱怨內容與名稱不匹配,並希望在通過互操作嘗試時停止錯誤。
如果互操作不起作用,您可能會找到一個可以處理 XMLSS 的庫,例如https://www.codeproject.com/Articles/8459/XmlSS-NET-Spreadsheet-Component或將其作為 XML 文件和提取您需要的信息。 可能將其作為數據集讀取( da = new DataSet()
然后ds.ReadXml(path_to_file)
)提供了一種處理數據的簡單方法
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.