首页 > 解决方案 > NPOI CopyTo 不起作用并显示 NPOI.XSSF.UserModel.XSSFSheet.CopySheetImages 异常

问题描述

我有一个无法使用的问题NPOI CopyTo

我的代码

    var path = @"C:\Users\Wei\Downloads\Book2.xlsx";
    var tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".xlsx");
    File.Copy(path, tempPath);
    using (var fs = new FileStream(tempPath, FileMode.OpenOrCreate, FileAccess.Read))
    {
        XSSFWorkbook wb = new XSSFWorkbook(fs);
        ISheet ws1 = wb.GetSheetAt(0);
        ws1.CopyTo(wb, "sheet2", true, true);
        wb.Write(fs);
    }

运行时版本:.NET Core 3.1.10
NPOI 版本:2.5.2

Book2.xlsx

错误

NullReferenceException : Object reference not set to an instance of an object.

堆栈跟踪

 at NPOI.XSSF.UserModel.XSSFSheet.CopySheetImages(XSSFWorkbook destWorkbook, XSSFSheet destSheet)
   at NPOI.XSSF.UserModel.XSSFSheet.CopyTo(IWorkbook dest, String name, Boolean copyStyle, Boolean keepFormulas)
   at UserQuery.Main(), line 10

我检查了 var wb not null

标签: c#npoi

解决方案


推荐阅读