-
Notifications
You must be signed in to change notification settings - Fork 277
LoadFromDataReader
AdrianEPPlus edited this page Sep 2, 2024
·
12 revisions
This method loads data from a System.Data.IDataReader
into a spreadsheet.
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 (var sqlConn = new SQLiteConnection(connectionString))
{
sqlConn.Open();
using (var sqlCmd = new SQLiteCommand("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 (var sqlReader = sqlCmd.ExecuteReader())
{
// Create a workbook and load the data from the IDataReader
using(var package = new ExcelPackage())
{
var sheet = package.Workbook.Worksheets.Add("TestSheet");
// The second argument specifies if we should print headers on the first row or not
sheet.Cells["A1"].LoadFromDataReader(sqlReader, true);
}
}
}
Indicates if the column names from the data reader should be used as headers in the spreadsheet.
Name of the table in the spreadsheet.
Set to true to transpose the data.
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.
var filledRange = sheet.Cells["A1"].LoadFromDataReader(sqlReader, true, TableStyles.Dark1);
// if you want to get access to the created table:
var table = sheet.Tables.GetFromRange(filledRange);
EPPlus Software AB - https://epplussoftware.com
- What is new in EPPlus 5+
- Breaking Changes in EPPlus 5
- Breaking Changes in EPPlus 6
- Breaking Changes in EPPlus 7
- Addressing a worksheet
- Dimension/Used range
- Copying ranges/sheets
- Insert/Delete
- Filling ranges
- Sorting ranges
- Taking and skipping columns/rows
- Data validation
- Comments
- Freeze and Split Panes
- Header and Footer
- Autofit columns
- Grouping and Ungrouping Rows and Columns
- Formatting and styling
- Conditional formatting
- Using Themes
- Working with custom named table- or slicer- styles