为什么Gembox电子表格在调用Excelfile.save:FileNotFoundException时崩溃无法加载文



我正在使用Gembox打开、修改和保存一个xlsx文件。对Excel文件调用Save会导致System.IO.FileNotFoundException。

这个问题发生在我们公司的串行密钥和免费密钥上。

样本代码

using GemBox.Spreadsheet;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{ 
var path = @"C:codeGemboxTestApp.xlsx"; 
SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");
ExcelFile ef = ExcelFile.Load(path); 
ExcelWorksheet ws = ef.Worksheets[0]; 
//ws.Columns[0].Cells[0].Value = 42; 
ef.Save(path); // <--------------------------------- Crash!
}
}
}

错误消息

Could not load file or assembly 'System.Security.Permissions, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
System.Security.Permissions, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51

堆栈跟踪

at   .(Stream , Byte[] , Int32 , Int32 , String )
at   .(Stream )
at   .(Stream )
at   . ()
at   .(Stream )
at    .Dispose()
at   .   ​ (Boolean )
at   .Dispose()
at    .    ​ ()
at    .(Boolean )
at    .Dispose()
at GemBox.Spreadsheet.XlsxSaveOptions.(ExcelFile , Stream ,     )
at GemBox.Spreadsheet.XlsxSaveOptions.Save(ExcelFile excelFile, Stream stream, String path)
at GemBox.Spreadsheet.SaveOptions.(ExcelFile , String )
at GemBox.Spreadsheet.ExcelFile.Save(String path, SaveOptions options)
at GemBox.Spreadsheet.ExcelFile.Save(String path)
at ConsoleApp.Program.Main(String[] args) in C:codeGemboxTestConsoleAppProgram.cs:line 14

版本

  1. .NET Core 3.1(3.0也会失败(
  2. GemBox.电子表格版本=45.0.1131
  3. Visual Studio 2019(VisualStudioVersion=16.0.29905.134(
  4. Windows 10 pro 64位

样本csproj文件

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="GemBox.Spreadsheet" Version="45.0.1131" />
</ItemGroup>
</Project>

这里是Gembox支持的回复

尝试将"System.Security.Permissions"包引用添加到您的".csproj"文件:

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>  
<ItemGroup>
<PackageReference Include="GemBox.Spreadsheet" Version="45.0.1131" />
<PackageReference Include="System.Security.Permissions" Version="4.7.0" />
</ItemGroup>
</Project>

仅供参考,我认为此包仅适用于Windows上的控制台应用程序。保存时不需要在Windows上或当您使用Linux上的任何.NET Core应用程序保存XLSX文件。

包似乎依赖于未被引用或包含的程序集。

通常,包作者会在Targetframework(每个TargetFramework(的nuspec文件中引用它们的依赖项。


您可以通过将最新的System.Security.Permissionsnuget包作为依赖项添加到项目中来解决此问题。


更新1:附加

看看他们的示例项目github-reo,我确实看到了对netcoreapp3.1的引用。

我测试了netcoreapp3.1和netcoreapp3.0,收到了Save的依赖包问题,并通过将其添加为依赖项来解决它(正如这个答案所示(。Gembox.Spreadsheet.示例

netcoreapp2.2的示例和用法在尝试Save时没有出现包缺少依赖项的问题。

更新2

删除了对更新1中提到的Load观察到的问题的引用。这似乎是不相关的,可能是我遇到的运行时和/或IDE问题。

此外,这个问题只在windows平台上的控制台应用程序中进行了测试和观察。

最新更新