我正在尝试创建一个 Excel 文件,用 C# 编写数据表。我正在使用 ClosedXML。这是我正在使用的代码:
dtbl 是数据表,fileNameFullPath 是有效目录的字符串。
using (XLWorkbook wb = new XLWorkbook())
{
wb.Worksheets.Add(dtbl);
wb.SaveAs(fileNameFullPath);
}
它抛出以下错误:
System.MissingMethodException
HResult=0x80131513
Message=Method not found: 'System.Collections.Generic.IEnumerable`1<DocumentFormat.OpenXml.Packaging.IdPartPair> DocumentFormat.OpenXml.Packaging.OpenXmlPartContainer.get_Parts()'.
Source=ClosedXML
StackTrace:
at ClosedXML.Excel.XLWorkbook.CreateParts(SpreadsheetDocument document, SaveOptions options)
at ClosedXML.Excel.XLWorkbook.CreatePackage(String filePath, SpreadsheetDocumentType spreadsheetDocumentType, SaveOptions options)
at ClosedXML.Excel.XLWorkbook.SaveAs(String file, SaveOptions options)
at ClosedXML.Excel.XLWorkbook.SaveAs(String file, Boolean validate, Boolean evaluateFormulae)
at ClosedXML.Excel.XLWorkbook.SaveAs(String file)
at MatchingPolicyToAC.MatchingProcess.SaveToExcel(DataGridView dtgv, DataGridEnum dgenm, String sname) in C:\_Source_DevOps\PolicyMatching\MatchingPolicyToAC\MatchingPolicyToAC\MatchingProcess\MatchingProcess.cs:line 418
This exception was originally thrown at this call stack:
[External Code]
MatchingPolicyToAC.MatchingProcess.SaveToExcel(System.Windows.Forms.DataGridView, MatchingPolicyToAC.DataGridEnum, string) in MatchingProcess.cs
我正在使用 v0.101.0 ClosedXML 和 DocumentFormat.OpenXml v3.0.0-ci0083
我正在使用 C#、Visual Studio 2022 v4.6.1 Windows 应用程序编写。
有什么想法吗?
ClosedXML是基于OpenXML开发的。您必须使用 ClosedXML 推荐的 OpenXML 版本。如果单独升级OpenXML版本,可能会出现这种现象。