Spire.XLS is a professional Excel API that enables developers to create, manage, manipulate, convert and print Excel worksheets. Get free and professional technical support for Spire.XLS for .NET, Java, Android, C++, Python.

Mon Aug 06, 2018 9:47 am

I have created a worksheet dynamically and then try to delete it at the end but when I delete the worksheet and try to save the workbook , a Null Pointer Exception is thrown . Please help me out . Thank you

Sample Code :-

Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];
worksheet.Name = "Sheet 7777";
worksheet.Range["A3"].Text = "Hello World";
workbook.SaveToFile("NEw File.xlsx", ExcelVersion.Version2016);
Worksheet sheet = workbook.Worksheets[1];
sheet.Name = "Hello";
workbook.SaveToFile("NEw File.xlsx", ExcelVersion.Version2016);
workbook.Worksheets.Remove("Sheet 7777"); // tried both , same exception is thrown
//workbook.Worksheets[0].Remove();
workbook.SaveToFile("NEw File.xlsx", ExcelVersion.Version2016);

akhil007
 
Posts: 2
Joined: Mon Aug 06, 2018 9:40 am

Mon Aug 06, 2018 10:28 am

Hello Akhil,

Thanks for your inquiry.
Please add below code to load the xlsx file before removing a worksheet. If you have other questions, please do not hesitate to contact us.
Code: Select all
workbook.LoadFromFile("NEw File.xlsx");


Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1261
Joined: Wed Apr 25, 2018 3:20 am

Wed Aug 22, 2018 6:50 am

Hello Akhil,

Greetings from E-iceblue.
Did my code help you? Your feedback will be greatly appreciated.

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1261
Joined: Wed Apr 25, 2018 3:20 am

Return to Spire.XLS