Object reference not set to an instance of an object.

Jan 1, 2009 at 12:30 AM
Hi Guys,

I am getting "Object reference not set to an instance of an object." Error When updating existing Excel Sheet on Server.

 protected void UpdateECRFormByOpenXML(int _ECRNo)
        {
            try
            {            
                string xlsFileName = Server.MapPath(ConfigurationManager.AppSettings["NewECRFormName"].ToString() + _ECRNo + ".xlsx");
                FileInfo file = new FileInfo(xlsFileName);            
                using (ExcelPackage xlPackage = new ExcelPackage(file))
                {
                    //Get the WorkSheet
                    ExcelWorksheet worksheet = xlPackage.Workbook.Worksheets[1];
                    worksheet.Cell(1, 10).Value = _ECRNo.ToString();
                    worksheet.Cell(16, 1).Value = "Test Xls Automation";
                    xlPackage.Workbook.Properties.Title = "CR Form # " + _ECRNo;   
                    xlPackage.Save();   
                }
            }
            catch
            {
                throw;
            }
        }

When I am saving updated excel sheet I am getting Object reference not set error, but my excel sheet is saving properly. Hence I am skipping throw statement in Catch Block and continuing my work.

May I know why I am getting above error.

Thank You.
Mar 19, 2009 at 10:36 AM
i suffer from the same problem
do you found a soulution

regards..
Jun 13, 2009 at 1:47 PM

This problem is caused by having worksheets in the template that are not edited after you call:

ExcelPackage xlPackage = new ExcelPackage(newFile, template)

See this post:
http://excelpackage.codeplex.com/Thread/View.aspx?ThreadId=7005