EPPlus - How to use a template

Baxter picture Baxter · Mar 5, 2012 · Viewed 48.7k times · Source

I have recently discovered EPPlus (http://epplus.codeplex.com/). I have an excel .xlsx file in my project with all the styled column headers. I read on their site that you can use templates.

Does anyone know how or can provide code sample of how to use my template.xlsx file with EPPlus? I would like to be able to simply load my data into the rows without messing with the headings.

Answer

Baxter picture Baxter · Mar 7, 2012

The solution I ended up going with:

using System.IO;
using System.Reflection;
using OfficeOpenXml;

//Create a stream of .xlsx file contained within my project using reflection
Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("EPPlusTest.templates.VendorTemplate.xlsx");            

//EPPlusTest = Namespace/Project
//templates = folder
//VendorTemplate.xlsx = file

//ExcelPackage has a constructor that only requires a stream.
ExcelPackage pck = new OfficeOpenXml.ExcelPackage(stream);

After that you can use all the methods of ExcelPackage that you want on an .xlsx file loaded from a template.