首页 > 解决方案 > 为什么调用 Excelfile.save 时 Gembox 电子表格会崩溃:FileNotFoundException 无法加载文件或程序集 System.Security.Permissions

问题描述

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

问题发生在我们公司的序列号和免费密钥上。

示例代码

using GemBox.Spreadsheet;
namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        { 
            var path = @"C:\code\GemboxTest\App.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:\code\GemboxTest\ConsoleApp\Program.cs:line 14

版本

  1. .NET Core 3.1(3.0 也失败)
  2. GemBox.Spreadsheet 版本=45.0.1131
  3. Visual Studio 2019 (VisualStudioVersion = 16.0.29905.134)
  4. Windows 10 专业版 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>

标签: c#.net-coregembox-spreadsheet

解决方案


这里是 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 上使用 ASP.NET Core 应用程序保存 XLSX 文件或在 Linux 上使用任何 .NET Core 应用程序保存 XLSX 文件时,不需要它。


推荐阅读