You can accomplish this goal while using the grouping feature of Smart Markers. Please check the following piece of code as well as attached designer & resultant spreadsheets for your reference.
C#
//Create sample DataTable
var table = new DataTable(“data”); //Add columns to the newly created DataTable while specifying the column type table.Columns.Add(“Col1”, typeof(string)); table.Columns.Add(“Col2”, typeof(string)); table.Columns.Add(“Col3”, typeof(string)); table.Columns.Add(“Col4”, typeof(int)); table.Columns.Add(“Col5”, typeof(int)); table.Columns.Add(“Col6”, typeof(int));
Thank you for your quick response. I have one more question. I would like to apply this styling for different work sheets of a work book and each work sheet will have different columns. Then how to proceed with this.
All the columns in my work sheets are dynamic. I.e. the column names are not fixed, then how to proceed with this. What is the best way to create the template.xlsx. It seems like we can’t skip any column name in the template.xlsx. Then how can I apply the format for different work sheets with different columns.
But in my all work sheets the column name for which I want to apply the grouping is same but remaining columns are dynamic.
Please suggest me the the better approach for this.
Please find the below additional information for the previous question.
If I am creating work book with 2 work sheets. For example the data source of the 1st work sheet contains the columns like Colum1, Colum2, Colum3, AAA1,BBB1,CCC1,DDD1,EEE1. The data source of the 2nd work sheet contains the columns like Colum1, Colum2, Colum3, XXX1,YYY1,ZZZ1,
In this work book I have two work sheets which contains the different columns count with different column names. Only the common column names are Column1, Column2 and Column3. Remaining columns are dynamic columns whose names will be changed dynamically in the data source.
Now I would like to apply the grouping on column1, Column2 and Column3. But the issue here is the smart marker template xlsx should be created with all column names, But here different work sheets has different column names, Then how to apply the grouping.
Is there any way in Smart Markers, where we can apply the grouping only for specific column and ignore the remaining columns.
Based on your recent comments, I would suggest you to create the template dynamically in your code. This way, you can have the same number of columns as per your data source as well as the Smart Markers will correspond to the columns names. Regarding the formatting/styling, you can apply the style to your template as well as the final Workbook. Please check the following piece of code which tries to accomplish your goal of generating dynamic template as well as a few useful article links at the bottom of this post.
Please note, the code is for demonstration purposes, and you should amend it according to you application requirements.
C#
// Create two objects of DataTable for dummy data DataTable table1 = new DataTable(“patients”); table1.Columns.Add(“name”); table1.Columns.Add(“id”); table1.Rows.Add(“Sam”, 1); table1.Rows.Add(“Mark”, 2); table1.Rows.Add(“David”, 3); table1.Rows.Add(“John”, 4); table1.Rows.Add(“Abraham”, 5);
First of all, we humbly request you to always create new threads with proper problem summary and description. Moreover, the automated notifications regarding a problem are sent to the registered email address of the thread owner only.
Regarding your concerns, yes, you can use the custom objects with Smart Markers. Please check the following snippet that makes use of a list of objects from the structure Person defined at the bottom of this post.
Note: The code has been provided for demonstration purposes only therefore you should be able to amend it as per the application requirements.
C#
WorkbookDesigner myWorkbook = new WorkbookDesigner(); Worksheet curentWorksheet = myWorkbook.Workbook.Worksheets[“Sheet1”];
// Create temporary data IList myList = new List(); myList.Add(new Person() { Name = “X”, Age = 26 }); myList.Add(new Person() { Name = “X”, Age = 32 }); myList.Add(new Person() { Name = “Y”, Age = 19 });
// Set the headers and smart markers to the XL file curentWorksheet.Cells[“A1”].PutValue(“Name”); curentWorksheet.Cells[“B1”].PutValue(“Age”); curentWorksheet.Cells[“A2”].PutValue("&=Person.Name(group:merge,skip:1)"); curentWorksheet.Cells[“B2”].PutValue("&=Person.Age");
// Set the data source myWorkbook.SetDataSource(“Person”, myList);