C# export to excel

后端 未结 5 581
盖世英雄少女心
盖世英雄少女心 2021-01-16 07:04

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

5条回答
  •  -上瘾入骨i
    2021-01-16 07:54

    Excel Export

    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

    Try

    And use it on MVC:

    public ActionResult ExportToExcel(string cid)
    {
        var customerList = new List();
        customerList = CustomerFactory.Gets();
        ExcelExport.EpplusExportToExcelFromObjectList(customerList.ToList(), "Customer List", true);
    
        return null;
    }
    
    
    

    Also you can define which column with which name shows on Excel Document. you have to create an attribute class for this.

    ExportAttribute

    using System.Attribute;
    public class ExportAttribute : Attribute
    {
        public string DisplayName { get; set; }
        public bool IsVisible { get; set; }
    }
    

    Class & Implementation

    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; }
    }
    

    提交回复
    热议问题