We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Define Foreach Loop in Word Template & Build Report using Java LINQ | Convert HashMap to DataSet | JSON readXml

I want to use for loops in word template to be able to print list values. Can you please provide sample code? I am using java.

@amitkumartrips,

Please see these sample input and output Word documents (foreach-LINQ-example.zip (18.4 KB)) and try running the following Java code of LINQ Reporting Engine of Aspose.Words for Java:

DataSet ds = new DataSet("data");
DataTable dt = new DataTable("bill");
dt.getColumns().add("accountid.name");
dt.getRows().add(new Object[]{"Account 1"});
dt.getRows().add(new Object[]{"Account 2"});
ds.getTables().add(dt);

Document doc = new Document("E:\\Temp\\foreach-LINQ-Template.docx");

ReportingEngine engine = new ReportingEngine();
engine.buildReport(doc, ds, "data");

doc.save("E:\\temp\\awjava-20.6.docx");

Thanks for reply, what if I am passing a template and HashMap of key values and then I try to replace values from those in the map? How do I let aspose know that one of the HashMap values has to be rendered as a DataTable i.e. it can have multiple records?

Also is there a way to access index value in template?

@amitkumartrips,

One simple way is to just transform your HashMap object into a DataSet and then populate report values from DataSet as shown in my previous post. Here is how you can convert HashMap to DataSet:

// Lets build sample HashMap
Map<Integer, String> lists = new HashMap<Integer, String>();
for (int i = 1; i < 3; i++) {
    lists.put(i, "Account " + i);
}
// Convert HashMap to DataSet
DataSet ds = new DataSet("data");
DataTable dt = new DataTable("bill");
dt.getColumns().add("accountid.id");
dt.getColumns().add("accountid.name");

for (Map.Entry<Integer, String> entry : lists.entrySet()) {
    Integer key = entry.getKey();
    String value = entry.getValue();

    dt.getRows().add(new Object[]{key, value});
}

ds.getTables().add(dt);

Document doc = new Document("E:\\Temp\\foreach-LINQ-Template.docx");

ReportingEngine engine = new ReportingEngine();
engine.buildReport(doc, ds, "data");

doc.save("E:\\temp\\awjava-20.6.docx");

Dear,

Here is what we are doing right now:
1- We create a template.
2- We have a MAP where we populate all the fields from Object.
3- In doc generation code we create dataSet.readXml method to read the Map as xml and load it into dataset.

We have a key value in map that has a JSON Array:

informations4categories
[{s1={descriptionSelectedCat= Test}, {s1={descriptionSelectedCat= 2TEST }]

Now we build the report like:
engine.buildReport(template, ds)

However following forEach loop is not rendered:
<<foreach [b in informations4categories]>>
Account Name: <<[b.“s1.descriptionSelectedCat”]>>
<>

Hope this is clear now!

@amitkumartrips,

To ensure a timely and accurate response, please ZIP and attach the following resources here for testing:

  • Your simplified template Word document
  • Aspose.Words for Java 20.6 generated output DOCX file showing the undesired behavior
  • Your expected DOCX file showing the desired output. You can create this document by using MS Word.
  • Please also create a standalone simple Java application (source code without compilation errors) that helps us to reproduce your current problem on our end and attach it here for testing. Please do not include Aspose.Words JAR files in it to reduce the file size.
  • Any other necessary resources required to reproduce the issue on our end.

As soon as you get these pieces of information ready, we will start further investigation into your scenario and provide you more information. Thanks for your cooperation.

Dear Hafeez,

I got it almost working, however I have one question:
Is there a way to add simple key value pair to DataSet? Right now I use something like this for simple fields:

DataTable dt = new DataTable(s);
dt.getColumns().add(s);
dt.getRows().add(new Object[]{o != null ? o : ""});
set.getTables().add(dt);

And then say for a simple field, I need to do following:

<<foreach [b in todaysDate_format]>>
    Date Field: <<[b.”todaysDate_format”]>>
<</foreach>>

However this does not work:

Date Field <<[todaysDate_format]>>

It shows DataRowCollection@…

Can you please let me know how to access field directly if there is just one row in a table or some other way without forEach loop?

Best Regards,
Amit

@amitkumartrips,

You can pass multiple data sources to ReportingEngine.buildReport method. Please see these sample documents (Docs.zip (18.6 KB)) and try running the following code:

DataSet ds = new DataSet("data");

DataTable dt = new DataTable("bill");
dt.getColumns().add("accountid.name");
dt.getRows().add(new Object[]{"Account 1"});
dt.getRows().add(new Object[]{"Account 2"});

DataTable dt2 = new DataTable("second");
dt2.getColumns().add("Name");
DataRow row = dt2.newRow();
row.set("Name", "My Name");
dt2.getRows().add(row);

ds.getTables().add(dt);
ds.getTables().add(dt2);

Document doc = new Document("E:\\Temp\\template.docx");

ReportingEngine engine = new ReportingEngine();
engine.buildReport(doc, new Object[]{ds, row}, new String[]{"data", "second"});

doc.save("E:\\temp\\awjava-20.6.docx");