【问题标题】:EPPlus custom header column namesEPPlus 自定义标题列名称
【发布时间】:2016-05-20 14:33:17
【问题描述】:

我有以下代码,它为我生成了一个带有标题行的 excel。 header 的列名在 DataItem 类中被命名为变量。

// 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表头输出:

Number | FirstName | LastName | Country

如何自定义我的输出,例如(添加空格等):

Number | First Name | Last Name | Country

【问题讨论】:

    标签: c# epplus


    【解决方案1】:

    我有,解决方法如下

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

    【讨论】:

    • 只想补充一点,您需要调用 LoadFromCollection 方法,并为第二个参数 - PrintHeaders 设置为 false。从第二行开始,不打印集合中的标题:workSheet.Cells[2, 1].LoadFromCollection(rows, false);
    • @Koshera 我不确定我是否使用了第二个参数,因为即使会打印标题,我认为附加值设置会覆盖名称,但感谢您提及该参数.
    【解决方案2】:

    使用 System.ComponentModel 命名空间中的 DescriptionAttribute 在标题中设置列名。

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

    【讨论】:

    • 如果没有,有没有办法在装饰器上强制格式化 EPPlus...如何根据标题/属性名称搜索列地址?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-19
    • 2023-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-03
    相关资源
    最近更新 更多