EPPlus Header Column Name

I have the following code that excel generates to me with a title bar. Header column names are called variables in the 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 (added spaces, etc.):

Number | First Name | Last Name | Country
+4
source share
2 answers

Use the Description attribute from the System.ComponentModel namespace to specify the 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; }
}
0
source

I have it, the solution follows

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

Source: https://habr.com/ru/post/1628094/


All Articles