This project has moved and is read-only. For the latest updates, please go here.

Error opening Excel Template

May 24, 2010 at 9:21 AM
Edited May 24, 2010 at 9:32 AM
I am trying to open an existing xlsx file as a template using ASP and get this error when loading a worksheet from the Excelpackage. On exploring the worksheets[] i can see theres a count of 7 (which is correct) but I cant get to it because of the error. I'd appreciate some help. public static void test() { FileInfo fsave = new FileInfo(@"C:\Inetpub\wwwroot\Download\MCAF.xlsx"); FileInfo fi = new FileInfo(@"C:\Inetpub\wwwroot\Templates\MC_Invoice_Service.xls"); ExcelPackage p = new ExcelPackage(fi); var ws = p.Workbook.Worksheets[0]; // <---- EXCEPTION: Object reference not set to an instance of an object. ws.Cells[1, 2].Value = "Bob"; p.SaveAs(fsave); } EDIT: Loading it as a FileStream worked. I am still trying to figure out why the error above occurs.
May 24, 2010 at 1:36 PM
Edited May 24, 2010 at 2:37 PM
Try something like this:

FileInfo newFile = new FileInfo(newFilePath);
FileInfo template = new FileInfo(tempPath);

using (ExcelPackage xlPackage = new ExcelPackage(newFile, template))
worksheet = xlPackage.Workbook.Worksheets["Sheet1"];
worksheet .Cells[1, 2].Value = "Test Text";

Also, your template may have to be .xlsx.
May 24, 2010 at 8:10 PM

Try to download the latest source and see if it works. I fixed an issue on namesstyles that could give this error. I'm not sure this is the problem, but start with that.

May 24, 2010 at 8:58 PM
Thanks for the response guys. jankallman may be right it has something to do with existing styles on the sheet, my original xlsx template had alot of style formatting going on. So I saved it as an .xls file (for Office 97) which stripped off all the styles then I saved it back to .xlsx and it worked! I have 2 versions of the files now I could email them to you if you want to experiment further. For now I am going back to COM since this is for an intranet application I don't mind the load on the server plus we haven't fully shifted to office 2007 so we still get alot of xls files too bad EPPlus dosent support that. Any plans for supporting office 2003? You guys have done a great job with EPPlus Ive been experimenting with excel libraries for days and EPPlus is the best in my opinion. Keep up the great work.