Skip to content

LoadFromDataTable

AdrianEPPlus edited this page Sep 2, 2024 · 19 revisions

This method loads a System.Data.DataTable into a spreadshet.

Basic usage

A DataTable can be filled with data from various sources, often a database. In this simple example we will create it and fill it ourselves.

vartable=newDataTable("Astronaut");table.Columns.Add("Id",typeof(int));table.Columns.Add("FirstName",typeof(string));table.Columns.Add("LastName",typeof(string));// add some datatable.Rows.Add(1,"Bob","Behnken");table.Rows.Add(2,"Doug","Hurley");//create a workbook with a spreadsheet and load the data tableusing(varpackage=newExcelPackage()){varsheet=package.Workbook.Worksheets.Add("Astronauts");varfilledRange=sheet.Cells["A1"].LoadFromDataTable(table);}

This will set the value of cell A1 to 1, B1 to "Bob" and C1 to "Behnken". A2 to 2, B2 to "Doug" and C2 to "Hurley". The return value is the resulting range, containing the added data.

The order of the columns on the worksheet will be the same as in the supplied DataTable. You can re-order the columns by using the DataColumn.SetOrdinal method:

table.Columns["LastName"].SetOrdinal(0);table.Columns["FirstName"].SetOrdinal(1);table.Columns["Id"].SetOrdinal(2);

Headers

By supplying the parameter PrintHeaders to the LoadFromDataSet method you can add headers to the first row above the data.

sheet.Cells["A1"].LoadFromDataTable(table,true);// alternatively from version 5.2.1 or highersheet.Cells["A1"].LoadFromDataTable(table, c =>c.PrintHeaders=true);

EPPlus uses the Caption property of the DataColumn's of the DataTable to set the headers. Per default these will be the column names set when the DataTable was created, but you can change the caption to any value, see below (using the example above):

table.Columns["FirstName"].Caption="First name";table.Columns["LastName"].Caption="Last name";

TableStyle

If you supply the parameter TableStyle EPPlus will create a table for the data in the worksheet. The TableStyles enum contains over 60 different table styles to choose from.

varfilledRange=sheet.Cells["A1"].LoadFromDataTable(table,true,TableStyles.Dark1);// if you want access to the created table in the sheetvarexcelTable=sheet.Tables.GetFromRange(filledRange);// alternatively from version 5.2.1 or highersheet.Cells["A1"].LoadFromDataTable(table, c =>{c.PrintHeaders=true;c.TableStyle=TableStyles.Dark1;});

Transpose

If you supply the parameter Transpose EPPlus will transpose the data in the worksheet.

vartransposed=sheet.Cells["A1"].LoadFromDataTable(table,false,TableStyles.None,true);//Alternativelysheet.Cells["A1"].LoadFromDataTable(table, c =>{c.Transpose=true});

Import XML with System.Data.DataSet

You can use this method to import XML data to a spreadsheet.

Content of astronauts.xml:

<?xml version="1.0" encoding="utf-8"?>
<Astronauts>
<AstronautId="1">
<FirstName>Bob</FirstName>
<LastName>Behnken</LastName>
</Astronaut>
<AstronautId="2">
<FirstName>Doug</FirstName>
<LastName>Hurley</LastName>
</Astronaut>
</Astronauts>
vardataSet=newDataSet();varxml=File.ReadAllText(@"c:\Temp\astronauts.xml");varreader=XmlReader.Create(newStringReader(xml));dataSet.ReadXml(reader);using(varpackage=newExcelPackage()){varsheet=package.Workbook.Worksheets.Add("test");vartable=dataSet.Tables["Astronaut"];// default the Id ends up last in the column order. This moves it to the first position.table.Columns["Id"].SetOrdinal(0);// Set caption for the headerstable.Columns["FirstName"].Caption="First name";table.Columns["LastName"].Caption="Last name";// call LoadFromDataTable, print headers and use the Dark1 table stylesheet.Cells["A1"].LoadFromDataTable(table,true,TableStyles.Dark1);// AutoFit column with for the entire rangesheet.Cells[1,1,sheet.Dimension.End.Row,sheet.Dimension.End.Row].AutoFitColumns();package.SaveAs(newFileInfo(@"c:\temp\astronauts.xlsx"));}

This results in the following worksheet:

LoadFromDataTable5

See also

EPPlus wiki

Versions

Worksheet & Ranges

Styling

Import/Export data

Formulas and filters

Charts & Drawing objects

Tables & Pivot Tables

VBA & Protection

Clone this wiki locally