Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullReferenceException when copying sheet in some Excel files #346

Closed
Paul1519 opened this issue Apr 26, 2020 · 8 comments
Closed

NullReferenceException when copying sheet in some Excel files #346

Paul1519 opened this issue Apr 26, 2020 · 8 comments
Assignees
Labels
bug file_error file format generation/writing issue
Milestone

Comments

@Paul1519
Copy link

Starting with version 2.5.1 I'm receiving the below Exception when cloning an existing sheet. This only seems to happen with some Excel files, not all of them (including those created by Microsoft Excel, some of them work). In this post I have attached an example of a file that fails with this exception.

Exception:

System.NullReferenceException: Object reference not set to an instance of an object.
at NPOI.POIXMLDocumentPart.AddRelation(PackageRelationship pr, POIXMLDocumentPart part)
at NPOI.POIXMLDocumentPart.AddRelation(String id, POIXMLDocumentPart part)
at NPOI.XSSF.UserModel.XSSFSheet.CopySheet(String name, Boolean copyStyle)
at NPOI.XSSF.UserModel.XSSFSheet.CopySheet(String Name)
at NPOITest.Program.Main(String[] args) in E:\Projects\VisualStudio\NPOITest\NPOITest\Program.cs:line 13

Example test code:

`

IWorkbook workbook = WorkbookFactory.Create("TestInput.xlsx");

workbook.GetSheetAt(0).CopySheet("Data");

using (FileStream fileWriter = new FileStream("TestOutput.xlsx", FileMode.Create, FileAccess.ReadWrite, FileShare.None))
{
    workbook.Write(fileWriter);
}

Console.WriteLine("Press enter to exit");
Console.ReadLine();

`

System Info:

Windows 10 Version 1909 (build 18363.778)
Visual Studio Enterprise 2019 version 16.5.4
Tested in both .NET Core 3.1 and .NET Framework 4.8
Microsoft Office Professional Plus 2019 (build 12624.20520) 32 bits

TestInput.xlsx

@Paul1519 Paul1519 changed the title NullReferenceException when cloning sheet in some Excel files NullReferenceException when copying sheet in some Excel files Apr 26, 2020
@tonyqus tonyqus added the bug label Apr 27, 2020
@tonyqus tonyqus self-assigned this Apr 27, 2020
@tonyqus tonyqus added this to the NPOI 2.5.2 milestone Apr 27, 2020
@tonyqus
Copy link
Member

tonyqus commented Apr 30, 2020

The issue is caused by printersettings xmlpart. I'll investigate it later

@tonyqus tonyqus added the file_error file format generation/writing issue label Jul 6, 2020
@KaarloHirvi
Copy link

Hi Tony, wonder if issue #346 is still there?

Had a similar test code as Paul1519 above. Crashes with NullReferenceException when trying to copy sheet.

Environment:
NPOI 2.5.4
Visual Studio 2017, .Net FrameWork 4.7, .NET Core SDK 2.1
Windows 10 Version 2004 x64 (KB5005565)
Excel for Microsoft 365 MSO (16.0.14326.20344) 32 bit

@tonyqus
Copy link
Member

tonyqus commented Oct 8, 2021

Can you upload your Excel file and let me know which sheet you are copying? @KaarloHirvi

@tonyqus tonyqus reopened this Oct 8, 2021
@KaarloHirvi
Copy link

Thanks for replying.

Pls find attached the file. Was trying to copy sheet at [0]
pohja3.xlsx

@tonyqus
Copy link
Member

tonyqus commented Oct 8, 2021

@KaarloHirvi your issue duplicates with #287. copying sheets with charts will cause null exception.

@tonyqus tonyqus closed this as completed Oct 18, 2021
@cp1319421
Copy link

Hi Tonyqus, i also have issue now, Crashes with NullReferenceException when trying to copy sheet

Environment:
NPOI 2.7.0
Visual Studio 2022, .Net 6.0
Windows 11 23H2
MSA_Templete.xlsx

@tonyqus
Copy link
Member

tonyqus commented May 6, 2024

@cp1319421 I double checked your Excel file. It contains charts. Even if it doesn't throw exception, charts will not be cloned to the new sheet.

@tonyqus
Copy link
Member

tonyqus commented May 6, 2024

I created a new issue #1338 for this. This issue will be closed

@tonyqus tonyqus closed this as completed May 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug file_error file format generation/writing issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants