Dot-Net

ClosedXML - 無法載入文件或程序集

  • April 12, 2022

我正在嘗試使用 ClosedXML 在應用程序中生成 excel 文件,但是當我嘗試執行它時,我會收到無法載入文件或程序集伺服器錯誤。

Could not load file or assembly 'DocumentFormat.OpenXml, Version=2.5.5631.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.

我已經看到了幾種解決方案,包括在其屬性中更改 .dll 的“複製本地”值並將程序集直接添加到 web.config。

我正在使用 Visual Studios 2010,並且 dll 屬性不包含“複製本地”條目。然後,當我在 Web 配置中輸入以下程序集時,我仍然收到相同的伺服器錯誤消息。我從提供的錯誤消息中得到了程序集。

<add assembly="ClosedXML, Version=0.76.0.0, Culture=neutral, PublicKeyToken=fd1eb21b62ae805b"/>

在瘋狂尋找答案並最終發布一個問題後,我遇到了最簡單的答案,一旦我引用了額外的 .dll,一切正常。

要使用 ClosedXML,您必須引用 DocumentFormat.OpenXml.dll:

NET 4.0+ 的 DocumentFormat.OpenXml.dll

我遇到了同樣的問題,但我只是添加了 nuget 包並確保它在我的項目中被正確引用

using ClosedXML.Excel;

然後我可以訪問excel文件

var wb = new XLWorkbook(MyExcelFile);

引用自:https://stackoverflow.com/questions/34907898