本文介绍了EPPlus自定义标题列名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码,它们为我生成了带有标题行的excel。标头的列名称在DataItem类中被命名为变量。
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标头输出:
excel header output :
Number | FirstName | LastName | Country
例如如何自定义输出(添加空格等):
How my output can be customized for example (spaces added etc.):
Number | First Name | Last Name | Country
推荐答案
使用 DescriptionAttribute System.ComponentModel 命名空间,用于在标题中设置列名称。
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; }
}
这篇关于EPPlus自定义标题列名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!