Skip to content

LoadFromDataReader

AdrianEPPlus edited this page Sep 2, 2024 · 12 revisions

This method loads data from a System.Data.IDataReader into a spreadsheet.

Basic usage

The IDataReader can be used for reading data from many different types of databases, in this sample we are using an SQLite database.

// lets connect to the sample database for some data``using(varsqlConn=newSQLiteConnection(connectionString)){sqlConn.Open();using(varsqlCmd=newSQLiteCommand("select CompanyName, [Name], Email, c.Country, o.OrderId, orderdate, ordervalue, currency from Customer c inner join Orders o on c.CustomerId=o.CustomerId inner join SalesPerson s on o.salesPersonId = s.salesPersonId ORDER BY 1,2 desc",sqlConn)){using(varsqlReader=sqlCmd.ExecuteReader()){// Create a workbook and load the data from the IDataReaderusing(varpackage=newExcelPackage()){varsheet=package.Workbook.Worksheets.Add("TestSheet");// The second argument specifies if we should print headers on the first row or notsheet.Cells["A1"].LoadFromDataReader(sqlReader,true);}}}

Arguments

PrintHeaders

Indicates if the column names from the data reader should be used as headers in the spreadsheet.

TableName

Name of the table in the spreadsheet.

Transpose

Set to true to transpose the data.

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"].LoadFromDataReader(sqlReader,true,TableStyles.Dark1);// if you want to get access to the created table:vartable=sheet.Tables.GetFromRange(filledRange);

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