Search code examples
c#asp.net-mvclinqepplus

Generic Excel Generator function for EPPlus


How would one build a generic EPPlus Spreadsheet function for your LINQ Queries?

UPDATE: The need was specifically for an ASP.NET MVC application.


Solution

  • There is now a more simple way to achieve this using the LoadFromCollection method.

    public void ExportToExcel(IEnumerable<Employee> employees, FileInfo targetFile)
    {
        using (var excelFile = new ExcelPackage(targetFile))
        {
            var worksheet = excelFile.Workbook.Worksheets.Add("Sheet1");
            worksheet.Cells["A1"].LoadFromCollection(Collection: employees, PrintHeaders: true);
            excelFile.Save();
        }
    }
    

    Example taken from here: http://gruffcode.com/2013/10/30/simple-excel-export-with-epplus/