This walkthrough considers the use case to create a Marketing Report of a company which is launching a new series of smartphones. Hence, an Excel report for the planned marketing activities needs to be created. The report details out the planned events for the launch, its budget and expenses. The datasource used for binding the data, in this case, is DataTable. The template layout is created in different Excel tabs to generate multiple reports.
The below steps describe how to create an Excel report using template. You can also download the Excel template layout here.
Template Layout: Marketing Report
The below layout uses the Group property (G=Merge), which will group the smartphones against the corresponding records by displaying it once per group. The merge value merges the cells of each group.
Template Layout: SmartPhone Expenses
The below layout uses two template properties, Cell expansion (E=H) and Cell context (C=A3)
Template Layout: Launch Events
The below layout uses four template properties, Range (R=A3:B5), Sort (S=None), Cell expansion (E=H) and Page break (PageBreak=True)
Template Layout: {{ds.Country}}
C# |
Copy Code |
---|---|
Console.WriteLine("Generating Marketing Report using Templates"); // Initialize Workbook var workbook = new Workbook(); // Load BudgetPlan_DataTable.xlsx Template in workbook from Resource var templateFile = "../../../Resources/BudgetPlan_DataTable.xlsx"; workbook.Open(templateFile); |
C# |
Copy Code |
---|---|
// We can have mutiple types of DataSources like Custom Object/ DataSet/ DataTable/ Json/ Variable. // Here dataSource is a DataTable var dataSource = new DataTable(); // Adding DataColumns in DataTable according to the Template fields dataSource.Columns.Add(new DataColumn("SmartPhone", typeof(string))); dataSource.Columns.Add(new DataColumn("Event", typeof(string))); dataSource.Columns.Add(new DataColumn("Budget", typeof(Int32))); dataSource.Columns.Add(new DataColumn("Expense", typeof(Int32))); dataSource.Columns.Add(new DataColumn("City", typeof(string))); dataSource.Columns.Add(new DataColumn("Country", typeof(string))); // Adding Data in DataTable dataSource.Rows.Add("Apple iPhone 11", "Phone Launch", 1000, 950, "Seattle", "USA"); dataSource.Rows.Add("Apple iPhone 11", "CEO Meet", 2000, 1800, "New York", "USA"); dataSource.Rows.Add("Samsung Galaxy S10", "CEO Meet", 1600, 1550, "Paris", "France"); dataSource.Rows.Add("Apple iPhone XR", "Phone Launch", 1800, 1650, "Cape Town", "South Africa"); dataSource.Rows.Add("Samsung Galaxy S9", "Phone Launch", 1500, 1300, "Paris", "France"); dataSource.Rows.Add("Apple iPhone XR", "CEO Meet", 1600, 1500, "New Jersey", "USA"); dataSource.Rows.Add("Samsung Galaxy S9", "CEO Meet", 1200, 1150, "Seattle", "USA"); dataSource.Rows.Add("Samsung Galaxy S10", "Phone Launch", 1100, 1070, "Durban", "South Africa"); |
C# |
Copy Code |
---|---|
// Add DataSource // Here "ds" is the name of dataSource which is used in templates to define fields like {{ds.SmartPhone}} workbook.AddDataSource("ds", dataSource); |
C# |
Copy Code |
---|---|
// Invoke to process the template
workbook.ProcessTemplate(); |
C# |
Copy Code |
---|---|
// Save to an excel file Console.WriteLine("BudgetPlan_DataTable.xlsx Template is now bound to DataTable and generated MarketingReport_DataTable.xlsx file"); workbook.Save("MarketingReport_DataTable.xlsx"); |
The output of the Marketing Report is shown as below.
Excel Report: Marketing Report
Excel Report: Smartphone Expenses
Excel Report: Launch Events
Excel Report: Countries (Multiple reports are created)
In the above process, the template is overwritten while generating the Excel report. To create a report while keeping the template intact, see the Create Report While Retaining Template section below.
DsExcel provides GenerateReport method in the IWorkbook interface that returns an instance of a new workbook report while retaining the template. The method also provides an overload so that you can generate report for a specific worksheet only.
To generate report using the GenerateReport method, see the example code below:
C# |
Copy Code |
---|---|
//Process the template and return the instance of report workbook IWorkbook report = workbook.GenerateReport(); // Process the template for a specific worksheet only // IWorkbook report = workbook.GenerateReport(workbook.Worksheets["Sales"]); |
To view the code in action, see Generate Report demo.