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

Free Support Forum - aspose.com

How to Get DataTable Field Name in foreach Loop and Populate Data using Java | LINQ Reporting

Known that in aspose linq template, it can identify fixed string fieldname to get the current row’s fieldvalue.
But would like to ask how to get how to dynamically get field value from a fieldname variable or column index with thanks.

@ngai1004

Unfortunately, your requirement is not clear enough. Could you please share some more detail about your requirement along with input document and code example that you are using? Please also share your expected output. We will then provide you more information about your query.

There are the further information requested.

Requirement

Input dataset data to the MS word LINQ template, then generate output report in Java application.
(using aspose-words v21.7 for Java)

Input data

Java code:

// Call point in main function
engine.buildReport(doc, 
	new Object[] { 
		getTable5DS(), 
		aryAgeGroups() 
	},
	new String[] { "T_5", "T_5_series" }
);

// Dataset return function

public static String[] aryAgeGroups(){
	return new String[]{
		"25 or below",
		"26-35",
		"36-45",
		"45 or above"
	};
}

public static DataSet getTable5DS() throws Exception {
	DataSet ds = new DataSet("ds");

	String[] aryAgeGroups = aryAgeGroups();

	DataTable dtTA = new DataTable("t_a");
	dtTA.getColumns().add("dig");
	for(String strAge : aryAgeGroups) {
		dtTA.getColumns().add(strAge);
	}
	ds.getTables().add(dtTA);

	String[] argTADigs = new String[]{
		"Dig A",
		"Dig B",
		"Dig C"
	};
	int[][] argTAData = new int[][]{
		{5, 67, 413, 511},
		{1, 13, 86, 83},
		{0, 10, 51, 72}
	};
	for(int idxTA = 0; idxTA < argTADigs.length; idxTA++) {
		DataRow dr = dtTA.newRow();
		dr.set("dig", argTADigs[idxTA]);
		for (int idxTAAge = 0; idxTAAge < aryAgeGroups.length; idxTAAge++) {
			dr.set(aryAgeGroups[idxTAAge], argTAData[idxTA][idxTAAge]);
		}
		dtTA.getRows().add(dr);
	};
	
	return ds;
}

MS word LINQ template used

Table template in MS word
-------------------
Diagnosis|<<[T_5_series[0]]>>|<<[T_5_series[1]]>>|<<[T_5_series[2]]>>|<<[T_5_series[3]]>>
-------------------
<<foreach [row in T_5.t_a]>><<[row.dig]>>|<<[row.get(T_5_series[0])]>>|<<[row.get(T_5_series[1])]>>|<<[row.get(T_5_series[2])]>>|<<[row.get(T_5_series[3])]>>|<<[sum of [T_5_series[0 to 3]]]>><</foreach>>
-------------------
Total|<<[T_5.t_a sum of col T_5_series[0]]>>|<<[T_5.t_a sum of col T_5_series[1]]>>|<<[T_5.t_a sum of col T_5_series[2]]>>|<<[T_5.t_a sum of col T_5_series[3]]>>|<<[sum of the total values]>>

Error occurred

Exception in thread “main” java.lang.IllegalStateException: An error has been encountered at the end of expression ‘row.get(T_5_series[0])]>’. Can not resolve method ‘get’ on type ‘class com.aspose.words.net.System.Data.DataRow’.

Expected output

Render out all data values and generate the output report

Solution desired

  • Correct LINQ template in this case (can be dynamically get fieldvalue from a fieldname variable)
  • Other DataRow methods can be used on the MS Word LINQ template

@ngai1004

We have logged your requirement in our issue tracking system as WORDSNET-22707. We will inform you via this forum thread once there is an update available on it. We apologize for your inconvenience.

@ngai1004

Your issue has been resolved and its fix will be available in the next version of Aspose.Words i.e. 21.11.

You can use template syntax <<[row.get(0)]>> to access a value of the first column of a data row denoted as row .

To get the sum of values of the first column for several rows of a data table denoted as table , you can use template syntax <<[table.sum(r => (Double)r.get(0))]>> .

Please note that the typecast to Double - it is required because DataRow.get(int) returns an instance of the Object class that does not has an addition operator defined. You can use another numeric type instead of Double as per your data structure. Moreover, to make the typecast work, you also need to make Double.class (or another actual type) be known by the engine as per following article.

Useful enhancement with thanks :+1:

The issues you have found earlier (filed as WORDSNET-22707) have been fixed in this Aspose.Words for Java 21.11.0 update.