Excel Reports
AddWorksheet(Workbook,String,IEnumerable<ColumnDefinition>) Method
Example 



KellermanSoftware.ExcelReports Namespace > ExcelReportsGenerator Class > AddWorksheet Method : AddWorksheet(Workbook,String,IEnumerable<ColumnDefinition>) Method
Column order and formatting
Add a worksheet to a workbook with a specified name
Syntax
Public Overloads Function AddWorksheet( _
   ByVal workbook As Workbook, _
   ByVal worksheetName As String, _
   ByVal columnDefinitions As IEnumerable(Of ColumnDefinition) _
) As Worksheet
Dim instance As ExcelReportsGenerator
Dim workbook As Workbook
Dim worksheetName As String
Dim columnDefinitions As IEnumerable(Of ColumnDefinition)
Dim value As Worksheet
 
value = instance.AddWorksheet(workbook, worksheetName, columnDefinitions)

Parameters

workbook
worksheetName
columnDefinitions
Column order and formatting
Example
ExcelReportsGenerator generator = new ExcelReportsGenerator(); //Trial Mode
//ExcelReportsGenerator generator = new ExcelReportsGenerator("place user name here", "place license key here"); //License Mode
 
//Parameters
string filePath = "AddWorksheetWithColumnDefinitionsExample.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";
 
List<ColumnDefinition> definitions = new List<ColumnDefinition>
                                         {
                                             //Put the last name column first
                                             new ColumnDefinition("LastName"),
                                             new ColumnDefinition("FirstName"),
                                             new ColumnDefinition("Married"),
                                             new ColumnDefinition("NumberOfChildren"),
                                             new ColumnDefinition("Salary"),
                                             new ColumnDefinition("BirthDate")
                                         };
 
Worksheet worksheet = generator.AddWorksheet(workbook, "Employees", definitions);
 
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 = "AddWorksheetWithColumnDefinitionsExample.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 definitions As New List(Of ColumnDefinition)() From { _
    New ColumnDefinition("LastName"), _
    New ColumnDefinition("FirstName"), _
    New ColumnDefinition("Married"), _
    New ColumnDefinition("NumberOfChildren"), _
    New ColumnDefinition("Salary"), _
    New ColumnDefinition("BirthDate") _
}
 
Dim worksheet As Worksheet = generator.AddWorksheet(workbook, "Employees", definitions)
 
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
Overload List