Which is the best way to export data to an existing xls sheet. I needs to support many versions of excel. If i was using Visual basic. I would use the CreateObject(\"Excel.a
I use EP Plus Excel Export for this. Add EP Plus library from Nuget to your project. Then add this class to your project.
ExcelExport.cs
And use it on MVC:
public ActionResult ExportToExcel(string cid)
{
var customerList = new List();
customerList = CustomerFactory.Gets();
ExcelExport.EpplusExportToExcelFromObjectList(customerList.ToList
Also you can define which column with which name shows on Excel Document. you have to create an attribute class for this.
using System.Attribute;
public class ExportAttribute : Attribute
{
public string DisplayName { get; set; }
public bool IsVisible { get; set; }
}
Then, implement this attribute to your class:
public class DtoCustomer
{
[ExportAttribute(DisplayName = "#", IsVisible = false)]
public int ID { get; set; }
[ExportAttribute(DisplayName = "Customer Name", IsVisible = true)]
public string Name{ get; set; }
[ExportAttribute(DisplayName = "Customer Surname", IsVisible = true)]
public string Surname { get; set; }
}