0

I have a problem that I cannot use NPOI CopyTo.

My Code

    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);
    }

Runtime Version : .NET Core 3.1.10
NPOI Version : 2.5.2

Book2.xlsx

Error

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

SstackTrace

 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

I've checked var wb not null.

Wei Lin
  • 3,591
  • 2
  • 20
  • 52
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Ken White Jan 20 '21 at 04:26
  • @KenWhite I've checked var wb not null. – Wei Lin Jan 20 '21 at 04:27

0 Answers0