EPPlus custom header column names

一笑奈何 提交于 2020-01-23 05:27:21

问题


I have following code, which generate me an excel with header row. The column names of header are named as variables in DataItem class.

// class for single row item
    public class DataItem
    {
        public int Number { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Country { get; set; }
    }

    // Retrive data items from database and store into conllection.
    var rows = database.GetData().ToList();

    // Create table from collection with automatic header
    ws.Cells["A1"].LoadFromCollection(rows, true, TableStyles.Medium25);

excel header output :

Number | FirstName | LastName | Country

How my output can be customized for example (spaces added etc.):

Number | First Name | Last Name | Country

回答1:


Use DescriptionAttribute from System.ComponentModel namespace to set column names in the header.

public class DataItem
{
    public int Number { get; set; }

    [Description("First name")]
    public string FirstName { get; set; }

    [Description("Last name")]
    public string LastName { get; set; }

    public string Country { get; set; }
}



回答2:


I have it, solution is following

ws.Cells["A1"].Value = "Number";
ws.Cells["B1"].Value = "First Name";
ws.Cells["C1"].Value = "Last Name";
ws.Cells["D1"].Value = "Country";


来源:https://stackoverflow.com/questions/35311841/epplus-custom-header-column-names

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!