Skip to content

LoadFromDataReader

Mats Alm edited this page Jul 25, 2020 · 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 (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");
                 sheet.LoadFromDataReader(sqlReader);
         } 
     } 
 }

EPPlus wiki

Versions

Worksheet & Ranges

Styling

Import/Export data

Formulas and filters

Charts & Drawing objects

Tables & Pivot Tables

VBA & Protection

Clone this wiki locally