Back

Create an Excel File using EPPlus

Here's an example that of how to use an external dll (EPPlus) to create an Excel / XLSX file via Canvas

I had a search for "C# Excel" tools and most people recommended EPPlus, an open source codeplex project.

The downloadable package contained the "EPPlus.dll".   

As Canvas allows you to upload 3rd Party DLLs to a project via details page for a site in the Canvas Admin.  I uploaded the dll.

Additionally, for the Dynamic Assembly I was using I added a reference to the dll by adding the follwoing to the references list of the Assembly :

~/bin/EPPlus.dll

To the references list for the Dyn Ass I was working with.


Creating an XLSX file in an ASCX

The following code creates an Excel file "my-excel-file.xlsx" in the tmp path of the application.

The first row contains "Hello" and "World" in columns A & B respectively.

The second row contains "Row" and "2" in columns A & B respectively.

These rows were rendered in a sheet named "Demo".

using (var package = new ExcelPackage()){
    
  var sheet = package.Workbook.Worksheets.Add("Demo");
    
  sheet.Cells["A1"].Value = "Hello";
  sheet.Cells["B1"].Value = "World";
  sheet.Cells["A2"].Value = "Row";
  sheet.Cells["B2"].Value = "2";
    
  var fi = new FileInfo(
    Net4orceApp.cApplicationPath + "tmp/my-excel-file.xlsx"
  );
            
  package.SaveAs(fi);
 
}


Note - : add a reference to the top of the ASCX file to reference OfficeOpenXML namespace, which is provided by the EEPlus dll.

using OfficeOpenXml;   


This is a screenshot of the created XLSX as view in Numbers on OSX