c# - 为什么.NET OpenXML SDK 的 SpreadsheetDocument.Open() 方法会抛出 NullReferenceException?

标签 c# .net openxml openxml-sdk

我完全被这个问题难住了,整个早上在 Google 上搜索都找不到任何结果。

我有一个看起来或多或少像这样的方法:

public void Open(string fileName, bool isEditable)
{
    if(this.Document != null) this.Document.Close();
    this.Document = SpreadSheetDocument.Open(fileName, isEditable);
}

它只是比这稍微复杂一些(例如,如果 arg 为 null 或 ZLS,则使用先前设置的文件名),但与此处的问题无关。

当我运行此代码时,我在该方法的最后一行收到 NullReferenceException。根据 MSDN 文档,此方法应该抛出的唯一异常是 ArgumentNullException 或 OpenXMLPackageException。该文件未在任何其他应用程序中打开,我尝试将其移动到不同位置,以防出现权限问题,并且它绝对是一个格式良好的 .xslx 文件,在 Excel 中创建并保存。

如果不是很明显,这是我编写的帮助程序类的一部分,用于减少打开电子表格和提取数据所涉及的工作量,主要用于不像导入 Access 那样简单的情况。 “this.Document”是 SpreadsheetDocument 类型的属性。这是异常详细信息:

System.NullReferenceException was unhandled
Message=Object reference not set to an instance of an object.
Source=DocumentFormat.OpenXml
StackTrace:
   at DocumentFormat.OpenXml.Packaging.OpenXmlPackage.get_FileOpenAccess()
   at DocumentFormat.OpenXml.Packaging.OpenXmlPackage.SavePartContents()
   at DocumentFormat.OpenXml.Packaging.OpenXmlPackage.Dispose(Boolean disposing)
   at DocumentFormat.OpenXml.Packaging.OpenXmlPackage.Dispose()
   at DocumentFormat.OpenXml.Packaging.OpenXmlPackage.Close()
   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 CorbLib.OXML.Spreadsheets.XLDoc.Open(String fileName, Boolean isEditable) in C:\[path removed]\XLDoc.cs:line 37
   at CorbLib.OXML.Spreadsheets.XLDoc.GetCell(String sheetName, String cellRef) in C:\[path removed]\XLDoc.cs:line 48
   at CorbLib.OXML.Spreadsheets.XLDoc.GetCellsFromRange(String range, Boolean leaveOpen) in C:\[path removed]\XLDoc.cs:line 139
   at CorbLib.OXML.Spreadsheets.XLDoc.GetCellValues(String range, Boolean leaveOpen) in C:\[path removed]\XLDoc.cs:line 116
   at ConsoleApplication1.Program.Main(String[] args) in C:\[path removed]\Program.cs:line 14
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()
InnerException: 

任何帮助都会非常有帮助。

编辑:根据已接受答案中的评论,我已将上述方法修改如下......

public void Open(string fileName, bool isEditable)
{
    /*unimportant bits*/
    var fs = new FileStream(this.FullPath, FileMode.Open, FileAccess.Read);
    this.Document = SpreadsheetDocument.Open(fs, this.IsEditable);
}

...现在工作正常。

最佳答案

我认为这是 DocumentFormat.OpenXml 库中的一个错误。 Load 函数内部有一个 catch block ,它首先调用 Close 方法:

try
{
    // Some stuff
}
catch (OpenXMLPackageException)
{
    Close();
    throw;
}

当捕获异常时,Close() 方法将被调用,但如果它发生在之前,则它会完全初始化,并且 Close 方法本身会抛出 NullReferenceException,隐藏原始异常.

您可能会看到 Visual Studio 的输出窗口中有什么问题。您应该能够在使用 Visual Studio 进行调试时捕获附加的正确错误(至少您会阅读原始异常消息)。

关于c# - 为什么.NET OpenXML SDK 的 SpreadsheetDocument.Open() 方法会抛出 NullReferenceException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9621397/

相关文章:

c# - 我可以在哪里放置静态字符串?

.net - 如何在 Firefox 上测试我的 .NET 站点?它在 IE 中加载但是当我在 Firefox 中打开它时它不加载 CSS

c# - C# 如何处理调用结构上的接口(interface)方法?

c# - 将标题添加到 word,并旋转它 - 使用 OpenXML

c# - 数据透视表中的 OpenOfficeXml 重复标签

c# - 如何在 ANTLR4 中获取 IParseTree?

c# - .net 应用程序在内存中的大小

c# - 在应用程序中实现多个屏幕

c# - 无法将两个docker、一个数据库、一个.net core应用程序连接在一起

c# - OpenXml 或 Microsft.Office.Interop.Excel 在 c# 中读取 excel 文件哪个更好?