Hi Team,
The output of datatable gives dynamic columns, So not able to mention the column name in template.Is there any way to bind all dynamic columns data without mentioning the column name in template.
Please find the attachment of template and program.
BindDynamicColumnInTemplate.zip (10.8 KB)
Note: The attached template works by specifying column name.
Regards,
Ravi K
@ravikonnur
You can access data row values by index rather than by name. Please check the following code illustrating the idea:
DataSet dataSet = new DataSet();
DataTable table = new DataTable();
table.TableName = "Persons";
dataSet.Tables.Add(table);
table.Columns.Add("FirstName");
table.Columns.Add("LastName");
table.Rows.Add("John", "Doe");
table.Rows.Add("Jane", "Doe");
DocumentBuilder builder = new DocumentBuilder();
builder.Write("<<foreach [p in ds.Persons]>><<[p[0]]>> <<[p[1]]>>\r<</foreach>>");
new ReportingEngine().BuildReport(builder.Document, dataSet, "ds");
Console.WriteLine(builder.Document.GetText().Trim()); // Prints "John Doe\rJane Doe"
@ivan.lyagin Thanks for your response. Your solution works great! But we have one issue when we try to bind the same to table format. We used to bind the data table to a table in word template similar as below. How do we alter your solution to show the table column names as well like FirstName and LastName
Also, Is there any documentation we can refer to these scenarios where dynamic data binding to table?
@srinudhulipalla
Please check Template.docx (12.3 KB). You can use the following code do build a report upon it:
DataSet dataSet = new DataSet();
DataTable table = new DataTable();
table.TableName = "Persons";
dataSet.Tables.Add(table);
table.Columns.Add("FirstName");
table.Columns.Add("LastName");
table.Rows.Add("John", "Doe");
table.Rows.Add("Jane", "Doe");
Document document = new Document("Template.docx");
new ReportingEngine().BuildReport(document, dataSet, "ds");
document.Save("Template Out.docx");
I am afraid, this is a matter of ADO.NET classes’ usage, so it is out of LINQ Reporting Engine documentation scope. However, the approach itself is straightforward: If access by name is impossible for your scenario, you can use access by index. The engine provides an ability to access public members of ADO.NET classes in template expressions as well.
@ivan.lyagin thanks for your response.
I couldn’t able to access the Template.docx though I login. Is there any additional permissions I need?
@srinudhulipalla Only topic starter and Aspose staff can access attachments in the thread. Since @ravikonnur is a topic starter you do not have access to the attachments.
@alexey.noskov @ivan.lyagin Thanks for your updates.
Looks like we missed some part in original question from @ravikonnur
The original question is, if the columns in datatable are dynamic, (meaning that we don’t know how many columns the datatable holds) and if we use the index (example, p[5]) in template it produce the runtime error when column not found for specified index.
Is there any other alternative way to bind the data table to table in word template “as is” without specifying either column index or column name?
@srinudhulipalla
As of now, without using a fixed number of columns, it becomes much harder to build such a report. There are the following options to proceed:
-
Wait for resolution of WORDSNET-18034 that should bring an ability to affect table columns in addition to rows using the engine.
-
If at least a maximum number of data columns is known, use a complicated template with several if
and elseif
tags such as <<if [ds.Tables["Persons"].Columns.Count == 2]>>...<<elseif [ds.Tables["Persons"].Columns.Count == 3]>>...<</if>>
. Then, each if-branch should contain a table with corresponding number of columns and template syntax.
-
Depending on a number of data columns, build a table with a corresponding number of columns in a template using Document
API and inject corresponding template syntax in each column.
Hi @ivan.lyagin
Thank you for your sugestions. Based on your input from above 2nd point, we were managed to do it in single table instead of multiple tables for each if-branch. Attached the template.
Would it be recommonded? Thanks.
Template3.docx (19.3 KB)
@srinudhulipalla
The template that you provided produces empty table columns when a number of data columns is insufficient. If this is OK, then there are no problems with this approach.
@ivan.lyagin Sure, thanks. It will be not a problem for us at the moment. Will the that template for now. Once again thanks for all your valuable inputs.
1 Like
The issues you have found earlier (filed as WORDSNET-18034) have been fixed in this Aspose.Words for .NET 23.6 update also available on NuGet.
@srinudhulipalla @ravikonnur
Starting from Aspose.Words 23.6, LINQ Reporting Engine supports building of tables growing horizontally. More information can be found at Working with Table-Column Data Bands.