Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What is the best way to export IEnumerable<data>to Excel ASP.Net MVC5 ?



I want to export the IEnumerable<data> to Excel with customized column names and header styles. Anyone suggest the best way to achieve this by using any plugin or dll ?


What I have tried:

I did some samples by using SimpleExcelExport and ClosedXML i coulnd't find any customization like modifying header styles and names.

Please suggest!



TIA :)
Posted
Updated 23-Mar-17 0:54am
v2

1 solution

ClosedXML[^] would be a better option for formatting
refer this to customize your need
ClosedXML - The easy way to OpenXML - Documentation[^]
ClosedXML - styles[^]

eg:

using (XLWorkbook wb = new XLWorkbook())
       {
           string fileName = "a.xlsx";

           DataTable dt = new DataTable();
           dt.Columns.Add("ID");
           dt.Columns.Add("Name");
           wb.Worksheets.Add(dt);

           var sheet = wb.Worksheet(0);
           sheet.Rows(1, 2).Style.Fill.BackgroundColor = XLColor.Blue;
           sheet.Rows(1, 2).Style.Font.Bold = true;

           sheet.Style.Font.FontSize = 9;
           sheet.Style.Font.FontName = "calibiri";

       }
 
Share this answer
 
v2
Comments
Karthik_Mahalingam 23-Mar-17 22:26pm    
then try interop which is an expensive one.

you shall use generic list to datatable converter code and pass to it
shiva sanika 23-Mar-17 22:41pm    
Tried interop if i got huge data the application was crashed.
shiva sanika 23-Mar-17 22:38pm    
I have a IEnumerable List right now and need to export the data to EXCEL.

wb.Worksheets.Add(objDataTable);

Instead of adding datatable object to workbook is there any way to add listobject to it by using ClosedXML ? Or any other plugin ?
Karthik_Mahalingam 23-Mar-17 23:59pm    
use this
https://www.codeproject.com/Tips/784090/Conversion-Between-DataTable-and-List-in-Csharp

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900