So I’ve been using NPOI all week and decide to do a quick “demo” for my team today. My demo was to show how to use NPOI to populate (update) an Excel template you’ve created. Microsoft has a bunch of free “pretty” templates on their website, so I went and choose one with nice colors, formulas and a few charts. It took me about an hour to build the complete demo. It’s actually a very simple process and only took a few lines of code to update your template.
Development Summary ( Step-by-Step )
- Get a template, I grabbed mine from here ( office.microsoft.com ).
** I only used the first sheet and deleted all the sample data - Create a new ASP.NET 3.5 Web Application projecct
- Download NPOI binaries and include in your project
- Build a UI that will be used to populate your template.
** This could also be populated by a data sources (db, XML, etc..)
** NOTE: I used Excel to create the form using Excel formulas - Add some c# code “magic” to load data into the template using NPOI
Sounds simple because it is… Here is the code to add the form contents into the Excel template.
// Open Template
FileStream fs = new FileStream(Server.MapPath(@"\template\Template_EventBudget.xls"), FileMode.Open, FileAccess.Read);
// Load the template into a NPOI workbook
HSSFWorkbook templateWorkbook = new HSSFWorkbook(fs, true);
// Load the sheet you are going to use as a template into NPOI
HSSFSheet sheet = templateWorkbook.GetSheet("Event Budget");
// Insert data into template
sheet.GetRow(1).GetCell(1).SetCellValue(EventName.Value); // Inserting a string value into Excel
sheet.GetRow(1).GetCell(5).SetCellValue(DateTime.Parse(EventDate.Value)); // Inserting a date value into Excel
sheet.GetRow(5).GetCell(2).SetCellValue(Double.Parse(Roomandhallfees.Value)); // Inserting a number value into Excel
sheet.GetRow(6).GetCell(2).SetCellValue(Double.Parse(Sitestaff.Value));
sheet.GetRow(7).GetCell(2).SetCellValue(Double.Parse(Equipment.Value));
sheet.GetRow(8).GetCell(2).SetCellValue(Double.Parse(Tablesandchairs.Value));
sheet.GetRow(12).GetCell(2).SetCellValue(Double.Parse(Flowers.Value));
sheet.GetRow(13).GetCell(2).SetCellValue(Double.Parse(Candles.Value));
sheet.GetRow(14).GetCell(2).SetCellValue(Double.Parse(Lighting.Value));
sheet.GetRow(15).GetCell(2).SetCellValue(Double.Parse(Balloons.Value));
sheet.GetRow(16).GetCell(2).SetCellValue(Double.Parse(Papersupplies.Value));
sheet.GetRow(20).GetCell(2).SetCellValue(Double.Parse(Graphicswork.Value));
sheet.GetRow(21).GetCell(2).SetCellValue(Double.Parse(Photocopying_Printing.Value));
sheet.GetRow(22).GetCell(2).SetCellValue(Double.Parse(Postage.Value));
sheet.GetRow(26).GetCell(2).SetCellValue(Double.Parse(Telephone.Value));
sheet.GetRow(27).GetCell(2).SetCellValue(Double.Parse(Transportation.Value));
sheet.GetRow(28).GetCell(2).SetCellValue(Double.Parse(Stationerysupplies.Value));
sheet.GetRow(29).GetCell(2).SetCellValue(Double.Parse(Faxservices.Value));
sheet.GetRow(33).GetCell(2).SetCellValue(Double.Parse(Food.Value));
sheet.GetRow(34).GetCell(2).SetCellValue(Double.Parse(Drinks.Value));
sheet.GetRow(35).GetCell(2).SetCellValue(Double.Parse(Linens.Value));
sheet.GetRow(36).GetCell(2).SetCellValue(Double.Parse(Staffandgratuities.Value));
sheet.GetRow(40).GetCell(2).SetCellValue(Double.Parse(Performers.Value));
sheet.GetRow(41).GetCell(2).SetCellValue(Double.Parse(Speakers.Value));
sheet.GetRow(42).GetCell(2).SetCellValue(Double.Parse(Travel.Value));
sheet.GetRow(43).GetCell(2).SetCellValue(Double.Parse(Hotel.Value));
sheet.GetRow(44).GetCell(2).SetCellValue(Double.Parse(Other.Value));
sheet.GetRow(48).GetCell(2).SetCellValue(Double.Parse(Ribbons_Plaques_Trophies.Value));
sheet.GetRow(49).GetCell(2).SetCellValue(Double.Parse(Gifts.Value));
// Force formulas to update with new data we added
sheet.ForceFormulaRecalculation = true;
// Save the NPOI workbook into a memory stream to be sent to the browser, could have saved to disk.
MemoryStream ms = new MemoryStream();
templateWorkbook.Write(ms);
// Send the memory stream to the browser
ExportDataTableToExcel(ms, "EventExpenseReport.xls");
Here are the screen shots of the form and completed template…
Here is demo UI.
Here is a populated template.
Here is a copy of the XLS file created in the program: NPOI – ASP.NET Form merged with Excel template
Here is a copy of the source code for the demo: NPOI – Visual Studio 2008 (ASP.NET 3.5) Template Merge Demo
Like all things, there is one catch. During the demo I found a bug with NPOI opening my XLS file and I had to download the source code and make two changes to get NPOI to open my XLS template (bug states this issue is caused when you save a XLSX file as XLS in Excel 2007). Read this post to see the fix or use the DLLs for NPOI included in my demo project.



#1 by samiran on May 28, 2010 - 12:54 am
Quote
hiee i have downloaded and tired your source code but i am unable to create more then 54 rows. please help its very urgent. if possible reply me in my mail..
Thanks in advance.
#2 by Zach on May 28, 2010 - 8:54 am
Quote
samiran i’ve replied to your email, give me a bit more details and I’ll try to help out. the example is based on inserting data into existing cells/rows in a template, if your trying to add/insert something based on rows I’m not sure what your referring to.
#3 by samiran on June 2, 2010 - 3:39 am
Quote
hey zach i got your mail and have done it successfully..thanks a lot for this example and ur quick response..u rock
#4 by Dane on July 16, 2010 - 7:32 am
Quote
Thank you for this article. I was able to get NPOI to work with your help. Right now my code returns my populated template to the user in a httpresponse object and they can save it, however I want to convert it to pdf. I have the pdfconversion all working fine with a test.xls file that I have on my server, but I can’t seem to figure out how to programmatically save the HSSFworkbook as an excel file rather than delivering it to the user so I can do the conversion all in the background. Any ideas for me? Again thanks for this article it really was golden.
#5 by Zach on July 20, 2010 - 1:31 pm
Quote
I’m not sure which PDF library you are using, but if you want to put your workbook into a memory stream to then convert into a PDF, then create a “MemoryStream” and call “workbook.write(memorystream)” to load it with your XLS file. Now that you have a XLS file in memory, you can use your PDF to put the file to PDF.
Let me know if you have any more questions.
#6 by Shock on July 26, 2010 - 4:39 am
Quote
Hi, I have a problem. My excel templates do not load correctly, I mean that I get null reference exception. When I use your template – everything works fine. When I created a new sheet in your template and tried to load it – the same error ocurred. Can you tell me, is there any hidden option which allows to correctly load templates to NPOI or what am I doing wrong ??/
Thanks in advance
#7 by Zach on July 27, 2010 - 7:05 am
Quote
email me a copy of your sheet zachary dot hunter at gmail.com an I’ll try debugging it. I’ve not had any problems opening/reading XLS data for awhile, so without more details I’m not sure what it could be. Just make sure your using the latest build, since there has been some important bug fixes…