Save error

Topics: User Forum
Feb 5, 2007 at 2:29 PM
I'm getting an exception when I try and save a file. The exception is "Object reference not set to an instance of an object."

Here is my code:-

FileInfo newFile = new FileInfo (newpathfilename);
FileInfo tplFile = new FileInfo (tplpathfilename);
using (ExcelPackage xlPackage = new ExcelPackage(newFile, tplFile))
{
ExcelWorksheet worksheet = xlPackage.Workbook.Worksheets"Items to be ordered";
worksheet.Cell(1, 4).Value = "POR-"+no.ToString("000000");
xlPackage.Save();
}

What am I doing wrong?
Feb 5, 2007 at 9:01 PM
I ran into this problem. It had to do with having multiple worksheets, and not all of them being accessed in the code. The solution is to change line 562 of ExcelWorksheet.cs from:
XmlNode pageSetup = _worksheetXml.SelectSingleNode("//d:pageSetup", NameSpaceManager);

to:
XmlNode pageSetup = WorksheetXml.SelectSingleNode("//d:pageSetup", NameSpaceManager);

If the worksheet has not been accessed, calling the public accessor WorksheetXml instead of the private class variable initiallizes it correctly.
Feb 5, 2007 at 10:27 PM
Well done! that was it.

I had three tabs and I was only updating two of them. I now update at least one cell on all the tabs and Save() works without causing an exception.
Apr 17, 2008 at 12:53 AM
Well done indeed... this was my issue as well. I had begun to reverse modifications to the spreadsheet template made during the past day to find the issue...

Thanks...
Jun 18, 2009 at 6:06 AM
Edited Jun 18, 2009 at 6:09 AM

Great!

I was having the same problem.

Changing from _worksheetXml to WorksheetXml did the trick. :)

Thanks,

Leniel Macaferi
http://leniel.net

Aug 24, 2009 at 8:38 AM

Will somebody commit the following change to the codebase of the new version?

(...) change line 562 of ExcelWorksheet.cs from:

XmlNode pageSetup = _worksheetXml.SelectSingleNode("//d:pageSetup", NameSpaceManager);

to:

XmlNode pageSetup = WorksheetXml.SelectSingleNode("//d:pageSetup", NameSpaceManager);

Thanks...

Jul 14, 2010 at 9:20 AM

can i get the source code to generate an excel with multiple sheets using an template.

please i need it.

 

Sep 26, 2012 at 10:11 AM

I have downloaded the source code - ExcelPackageSource.zip from the download page and replaced the code as told by dereksan, but I still get the exception  "Object reference not set to an instance of an object." while saving.  My Code :

 Dim newFile As New FileInfo(CopyToPath)
                            Using xlPackage As New ExcelPackage(newFile)
                                Dim worksheet As ExcelWorksheet = xlPackage.Workbook.Worksheets("Factoring Sch")
                                 worksheet.Cell(11, 3).Value = NAMEOFCLIENT
                                worksheet.Cell(12, 3).Value = ClientRegisteredAddress
                                worksheet.Cell(34, 4).Value = FACILITYDATE                              
                                xlPackage.Save()
                            End Using

 

I m badly stuck. pl help

Regards

May 20, 2015 at 9:09 AM
Edited May 20, 2015 at 9:09 AM
Hello,

I get the same error message:

StackTrace:
   bei OfficeOpenXml.ExcelWorksheet.Save() in D:\ReportingProject\70_ServerReports\ExcelPackage\ExcelWorksheet.cs:Line 561.
   bei OfficeOpenXml.ExcelWorkbook.Save() in D:\ReportingProject\70_ServerReports\ExcelPackage\ExcelWorkbook.cs:Line 439.
   bei OfficeOpenXml.ExcelPackage.Save() in D:\ReportingProject\70_ServerReports\ExcelPackage\ExcelPackage.cs:Line 290.
but I don't have this *.cs file and I don't have drive D:
Where can I find these *.cs files?

Regards