Skip to content

Create Excel from Array (List, DataTable, DataSet, ...)

License

Notifications You must be signed in to change notification settings

mustaddon/ArrayToExcel

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

50 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ArrayToExcel NuGet version

Create Excel from Array (List, DataTable, DataSet, ...)

Example 1: Create with default settings

using ArrayToExcel;

var items = Enumerable.Range(1, 10).Select(x => new
{
    Prop1 = $"Text #{x}",
    Prop2 = x * 1000,
    Prop3 = DateTime.Now.AddDays(-x),
});

var excel = items.ToExcel();

Result: example1.xlsx

Example 2: Rename sheet and columns

var excel = items.ToExcel(schema => schema
    .SheetName("Example name")
    .ColumnName(m => m.Name.Replace("Prop", "Column #")));

Result: example2.xlsx

Example 3: Sort columns

var excel = items.ToExcel(schema => schema
    .ColumnSort(m => m.Name, desc: true));

Result: example3.xlsx

Example 4: Custom column's mapping

var excel = items.ToExcel(schema => schema
    .AddColumn("MyColumnName#1", x => new CellHyperlink($"https://www.google.com/search?q={x.Prop1}", x.Prop1))
    .AddColumn("MyColumnName#2", x => $"test:{x.Prop2}")
    .AddColumn("MyColumnName#3", x => x.Prop3));

Result: example4.xlsx

Example 5: Additional sheets

var excel = items.ToExcel(schema => schema
    .SheetName("Main")
    .AddSheet(extraItems));

Result: example5.xlsx

Example 6: Create from DataSet

var dataSet = new DataSet();

for (var i = 1; i <= 3; i++)
{
    var table = new DataTable($"Table{i}");
    dataSet.Tables.Add(table);

    table.Columns.Add($"Column {i}-1", typeof(string));
    table.Columns.Add($"Column {i}-2", typeof(int));
    table.Columns.Add($"Column {i}-3", typeof(DateTime));

    for (var x = 1; x <= 10 * i; x++)
        table.Rows.Add($"Text #{x}", x * 1000, DateTime.Now.AddDays(-x));
}

var excel = dataSet.ToExcel();

Result: example6.xlsx

Example.ConsoleApp

Releases

No releases published

Packages

No packages published