Excel Reports
FillFromDataTable Method
Example 



KellermanSoftware.ExcelReports Namespace > ExcelReportsGenerator Class : FillFromDataTable Method
The worksheet to fill
A table of records
Fill a worksheet with a data table. Use default formatting or the formatting specified in the Worksheet.
Syntax
Public Sub FillFromDataTable( _
   ByVal worksheet As Worksheet, _
   ByVal dataTable As DataTable _
) 
Dim instance As ExcelReportsGenerator
Dim worksheet As Worksheet
Dim dataTable As DataTable
 
instance.FillFromDataTable(worksheet, dataTable)
public void FillFromDataTable( 
   Worksheet worksheet,
   DataTable dataTable
)
public:
void FillFromDataTable( 
   Worksheet^ worksheet,
   DataTable^ dataTable
) 

Parameters

worksheet
The worksheet to fill
dataTable
A table of records
Example
ExcelReportsGenerator generator = new ExcelReportsGenerator(); //Trial Mode
//ExcelReportsGenerator generator = new ExcelReportsGenerator("place user name here", "place license key here"); //License Mode
 
//Parameters
string filePath = "FillWorksheetWithDatatableExample.xlsx";
 
Workbook workbook = generator.WorkbookFactory(filePath);
 
workbook.Author = "John Smith";
workbook.CreatedTime = DateTime.Now;
workbook.LastSavedBy = "John Smith";
workbook.Subject = "This is a subject";
workbook.Title = "This is a title";
 
Worksheet worksheet = generator.AddWorksheet(workbook, "Employees");
 
DataTable dataTable = new DataTable();
dataTable.Columns.Add("FirstName", typeof(string));
dataTable.Columns.Add("LastName", typeof(string));
dataTable.Columns.Add("Married", typeof(bool));
dataTable.Columns.Add("NumberOfChildren", typeof(int));
dataTable.Columns.Add("Salary", typeof(decimal));
dataTable.Columns.Add("BirthDate", typeof(DateTime));
 
DataRow employee1 = dataTable.NewRow();
employee1["FirstName"] = "John";
employee1["LastName"] = "Smith";
employee1["Married"] = true;
employee1["NumberOfChildren"] = 3;
employee1["Salary"] = 60000M;
employee1["BirthDate"] = new DateTime(1977, 12, 15);
dataTable.Rows.Add(employee1);
 
DataRow employee2 = dataTable.NewRow();
employee2["FirstName"] = "Joe";
employee2["LastName"] = "Jones";
employee2["Married"] = false;
employee2["NumberOfChildren"] = 0;
employee2["Salary"] = 30000M;
employee2["BirthDate"] = new DateTime(1993, 6, 25);
dataTable.Rows.Add(employee2);
 
generator.FillFromDataTable(worksheet, dataTable);
generator.SaveWorkbook(workbook);
Dim generator As New ExcelReportsGenerator() 'Trial Mode
'Dim generator As New ExcelReportsGenerator("place user name here", "place license key here") 'License Mode
 
'Parameters
Dim filePath As String = "FillWorksheetWithDatatableExample.xlsx"
 
Dim workbook As Workbook = generator.WorkbookFactory(filePath)
 
workbook.Author = "John Smith"
workbook.CreatedTime = Date.Now
workbook.LastSavedBy = "John Smith"
workbook.Subject = "This is a subject"
workbook.Title = "This is a title"
 
Dim worksheet As Worksheet = generator.AddWorksheet(workbook, "Employees")
 
Dim dataTable As New DataTable()
dataTable.Columns.Add("FirstName", GetType(String))
dataTable.Columns.Add("LastName", GetType(String))
dataTable.Columns.Add("Married", GetType(Boolean))
dataTable.Columns.Add("NumberOfChildren", GetType(Integer))
dataTable.Columns.Add("Salary", GetType(Decimal))
dataTable.Columns.Add("BirthDate", GetType(Date))
 
Dim employee1 As DataRow = dataTable.NewRow()
employee1("FirstName") = "John"
employee1("LastName") = "Smith"
employee1("Married") = True
employee1("NumberOfChildren") = 3
employee1("Salary") = 60000D
employee1("BirthDate") = New Date(1977, 12, 15)
dataTable.Rows.Add(employee1)
 
Dim employee2 As DataRow = dataTable.NewRow()
employee2("FirstName") = "Joe"
employee2("LastName") = "Jones"
employee2("Married") = False
employee2("NumberOfChildren") = 0
employee2("Salary") = 30000D
employee2("BirthDate") = New Date(1993, 6, 25)
dataTable.Rows.Add(employee2)
 
generator.FillFromDataTable(worksheet, dataTable)
generator.SaveWorkbook(workbook)
Requirements

Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also

Reference

ExcelReportsGenerator Class
ExcelReportsGenerator Members