簡體   English   中英

異常“通過安全透明方法'NPOI.OpenXml4Net.OPC.ZipPackage嘗試”

[英]Exception “Attempt by security transparent method 'NPOI.OpenXml4Net.OPC.ZipPackage”

我正在嘗試使用NPOI從C#中的excel文件讀取。 下面是我用來初始化變量的代碼片段。 但是在初始化工作簿時,以下是我遇到的異常。

public class Excelhandler
{
    public static XSSFWorkbook workbook;
    public static FileStream fileStream;
    static Excelhandler()
    {
        try
        {
            string pth = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            string actualPath = pth.Substring(0, pth.LastIndexOf("bin"));
            string projectPath = new Uri(actualPath).LocalPath;
            string path = projectPath + @"TestData\Data.xlsx";
            //string path = ConfigurationManager.AppSettings["TestDataPath"];
            fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
            workbook = new XSSFWorkbook(fileStream);

        }

同一段代碼在其他C#項目中仍在工作,但突然不起作用。 以下是我使用的版本:-

NPOI-2.3.0

SharpZipLib(NPOI依賴)-1.0.0

.Net Framework -4.5.2

例外-嘗試通過安全透明方法'NPOI.OpenXml4Net.OPC.ZipPackage..ctor(System.IO.Stream,NPOI.OpenXml4Net.OPC.PackageAccess)'訪問安全關鍵方法'ICSharpCode.SharpZipLib.Zip.ZipInputStream..ctor (System.IO.Stream)'失敗。

我也遇到了VS2017,NPOI 2.3.0和SharpZipLib 1.0.0的問題。

謝謝CrazyDev。

我沒有解決將NuGet中的SharpZipLib版本更改為0.86的問題。 經過測試,我發現它也應該刪除App.config中的以下內容:

    <assemblyIdentity name="ICSharpCode.SharpZipLib" publicKeyToken="1b03e6acf1164f73" culture="neutral"/>
    <bindingRedirect oldVersion="0.0.0.0-1.0.0.999" newVersion="1.0.0.999"/>

因此,不僅將SharpZipLib的版本回滾到0.86,而且還修改了App.config。

將SharpZipLib Nuget更新到1.0.0時,我的臉上也遇到同樣的異常。 如NPOI 2.3.0中所述,回滾到最低版本0.86.0,Nuget依賴關系再次正常運行。 它的東西。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM