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

Create excel file on server from template

May 4, 2012 at 1:04 PM
Edited May 4, 2012 at 1:23 PM

Hi all, first of all thanks for Your work, I've just started using this library but seeing thing it can :)

I'm having a trouble figuring one little thing.


I would like to create an excel file from a template - create a new ExcelPackage in MemoryStream but using a file that I will load from server.

Something like:


using (ExcelPackage p = new ExcelPackage(new MemoryStream(), new FileInfo("~/Test.xlsx")))


But I can't pass FileInfo as second parameter :/ Any ideas how to do that?


My second question is how to copy a row (cells having name) add data to it.

In my template I have 3 cells called "row" I would like to add copy of this row for every row in my DataTable and update formulas in summary row that is after template row.



I've solved first part like so:

using (ExcelPackage p = new ExcelPackage(new MemoryStream(), new MemoryStream(File.ReadAllBytes(System.Web.HttpContext.Current.Server.MapPath("Test.xlsx")))))

I'm using System.Web.HttpContext.Current.Server.MapPath() because I'm generating my excel in ashx

Jan 29, 2013 at 4:47 PM

I have a template problem as well.  I have template.xlsx that I'm opening, then adding a new sheet which is a copy of the first sheet.  I can copy the template to a new worksheet locally, but failure occurs when I push the code to my dev server.  The following line of code works fine on localhost, but not when I push to my dev server.

Dim worksheet As ExcelWorksheet = excelPackage.Workbook.Worksheets.Add("Sheet2", excelPackage.Workbook.Worksheets(1))

That line of code throws the following error:

System.Web.HttpUnhandledException (0x80004005): Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
at OfficeOpenXml.ExcelWorksheets.get_Item(Int32 PositionID)

the rest of the stack trace just points to line numbers in my code

The file permissions are the same for the template on both servers.  Locally I have Excel installed, but that shouldn't matter.  The only file I've deployed to the dev server's bin is EPPlus.dll.

Anyone have any ideas?