Excel Reports
GenerateFromDataTable(DataTable,String,String) Method
Example 



KellermanSoftware.ExcelReports Namespace > ExcelReportsGenerator Class > GenerateFromDataTable Method : GenerateFromDataTable(DataTable,String,String) Method
A table of records
The name for the tab of the worksheet
Where to save the .xlsx file
Creates an Excel file based on the data columns of the passed in DataTable Supports all value types plus reference types of string and DateTime. By default, puts in a header with the names of the properties.
Syntax
Public Overloads Sub GenerateFromDataTable( _
   ByVal dataTable As DataTable, _
   ByVal worksheetName As String, _
   ByVal filePath As String _
) 
Dim instance As ExcelReportsGenerator
Dim dataTable As DataTable
Dim worksheetName As String
Dim filePath As String
 
instance.GenerateFromDataTable(dataTable, worksheetName, filePath)
public void GenerateFromDataTable( 
   DataTable dataTable,
   string worksheetName,
   string filePath
)
public:
void GenerateFromDataTable( 
   DataTable^ dataTable,
   String^ worksheetName,
   String^ filePath
) 

Parameters

dataTable
A table of records
worksheetName
The name for the tab of the worksheet
filePath
Where to save the .xlsx file
Example
ExcelReportsGenerator generator = new ExcelReportsGenerator(); //Trial Mode
//ExcelReportsGenerator generator = new ExcelReportsGenerator("place user name here", "place license key here"); //License Mode
 
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.GenerateFromDataTable(dataTable, "Employees", "GenerateFromDatatableExample.xlsx");
Dim generator As New ExcelReportsGenerator() 'Trial Mode
'Dim generator As New ExcelReportsGenerator("place user name here", "place license key here") 'License Mode
 
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.GenerateFromDataTable(dataTable, "Employees", "GenerateFromDatatableExample.xlsx")
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
Overload List