OpenXmlSDK 创建 xlsx ClosedXml 无法读取,但 NPOI、Epplus 可以读取,并显示消息`指定的包无效。缺少主要部分。`
OpenXmlSDK create xlsx ClosedXml can't read, but NPOI,Epplus can read, and show message `The specified package is invalid. The main part is missing.`
OpenXmlSDK 创建xlsx ClosedXml 无法读取,但NPOI、Epplus 可以读取,并显示消息The specified package is invalid. The main part is missing.
ClosedXml 代码:
void Main()
{
var path = @"C:\Users\Wei\AppData\Local\Temp9e17f7-a98b-42d5-8b75-4e6775914f9c.xlsx";
using (var workbook = new XLWorkbook(path))
{
var worksheet = workbook.Worksheets.First();
var cells = worksheet.Cells();
}
}
消息:指定的包无效。主要部分不见了。
资料来源:DocumentFormat.OpenXml
堆栈跟踪:
at DocumentFormat.OpenXml.Packaging.OpenXmlPackage.Load()
at DocumentFormat.OpenXml.Packaging.OpenXmlPackage.OpenCore(String path, Boolean readWriteMode)
at DocumentFormat.OpenXml.Packaging.SpreadsheetDocument.Open(String path, Boolean isEditable, OpenSettings openSettings)
at DocumentFormat.OpenXml.Packaging.SpreadsheetDocument.Open(String path, Boolean isEditable)
at ClosedXML.Excel.XLWorkbook.LoadSheets(String fileName) in C:\projects\closedxml\ClosedXML\Excel\XLWorkbook_Load.cs:line 42
at ClosedXML.Excel.XLWorkbook.Load(String file) in C:\projects\closedxml\ClosedXML\Excel\XLWorkbook_Load.cs:line 33
at ClosedXML.Excel.XLWorkbook..ctor(String file, XLEventTracking eventTracking) in C:\projects\closedxml\ClosedXML\Excel\XLWorkbook.cs:line 718
at ClosedXML.Excel.XLWorkbook..ctor(String file) in C:\projects\closedxml\ClosedXML\Excel\XLWorkbook.cs:line 709
at UserQuery.Main() in C:\Users\Wei\AppData\Local\Temp\LINQPad6\_zeygkceo\zmngrx\LINQPadQuery:line 5
at LINQPad.ExecutionModel.ClrQueryRunner.Run()
at LINQPad.ExecutionModel.Server.RunQuery(QueryRunner runner)
我试过像下面这样解压 xlsx 文件:
文件夹树和 link : https://github.com/shps951023/MyFiles/tree/master/2021-03-02/099e17f7-a98b-42d5-8b75-4e6775914f9c
│ [Content_Types].xml
│
├─xl
│ │ sharedStrings.xml
│ │ styles.xml
│ │ workbook.xml
│ │
│ ├─worksheets
│ │ sheet1.xml
│ │
│ └─_rels
│ workbook.xml.rels
│
└─_rels
.rels
Xlsx link : https://github.com/shps951023/MyFiles/blob/master/2021-03-02/099e17f7-a98b-42d5-8b75-4e6775914f9c.xlsx
因为 ZipArchiveEntry 创建的 xlsx 没有 CompressionOption 和 ContentType 以及 Uri 和包信息。
所以我用System.IO.Packaging.ZipPackage
可以解决问题
OpenXmlSDK 创建xlsx ClosedXml 无法读取,但NPOI、Epplus 可以读取,并显示消息The specified package is invalid. The main part is missing.
ClosedXml 代码:
void Main()
{
var path = @"C:\Users\Wei\AppData\Local\Temp9e17f7-a98b-42d5-8b75-4e6775914f9c.xlsx";
using (var workbook = new XLWorkbook(path))
{
var worksheet = workbook.Worksheets.First();
var cells = worksheet.Cells();
}
}
消息:指定的包无效。主要部分不见了。
资料来源:DocumentFormat.OpenXml
堆栈跟踪:
at DocumentFormat.OpenXml.Packaging.OpenXmlPackage.Load()
at DocumentFormat.OpenXml.Packaging.OpenXmlPackage.OpenCore(String path, Boolean readWriteMode)
at DocumentFormat.OpenXml.Packaging.SpreadsheetDocument.Open(String path, Boolean isEditable, OpenSettings openSettings)
at DocumentFormat.OpenXml.Packaging.SpreadsheetDocument.Open(String path, Boolean isEditable)
at ClosedXML.Excel.XLWorkbook.LoadSheets(String fileName) in C:\projects\closedxml\ClosedXML\Excel\XLWorkbook_Load.cs:line 42
at ClosedXML.Excel.XLWorkbook.Load(String file) in C:\projects\closedxml\ClosedXML\Excel\XLWorkbook_Load.cs:line 33
at ClosedXML.Excel.XLWorkbook..ctor(String file, XLEventTracking eventTracking) in C:\projects\closedxml\ClosedXML\Excel\XLWorkbook.cs:line 718
at ClosedXML.Excel.XLWorkbook..ctor(String file) in C:\projects\closedxml\ClosedXML\Excel\XLWorkbook.cs:line 709
at UserQuery.Main() in C:\Users\Wei\AppData\Local\Temp\LINQPad6\_zeygkceo\zmngrx\LINQPadQuery:line 5
at LINQPad.ExecutionModel.ClrQueryRunner.Run()
at LINQPad.ExecutionModel.Server.RunQuery(QueryRunner runner)
我试过像下面这样解压 xlsx 文件:
文件夹树和 link : https://github.com/shps951023/MyFiles/tree/master/2021-03-02/099e17f7-a98b-42d5-8b75-4e6775914f9c
│ [Content_Types].xml
│
├─xl
│ │ sharedStrings.xml
│ │ styles.xml
│ │ workbook.xml
│ │
│ ├─worksheets
│ │ sheet1.xml
│ │
│ └─_rels
│ workbook.xml.rels
│
└─_rels
.rels
Xlsx link : https://github.com/shps951023/MyFiles/blob/master/2021-03-02/099e17f7-a98b-42d5-8b75-4e6775914f9c.xlsx
因为 ZipArchiveEntry 创建的 xlsx 没有 CompressionOption 和 ContentType 以及 Uri 和包信息。
所以我用System.IO.Packaging.ZipPackage
可以解决问题