Let’s look at an example of output to template a single value with information.
We can take an example from previous article and modify it a little.
Preparing template
First, we need to update the template.
Add the name of the company in the header of our report.
${companyName} that’s how we’ll refer to our field.
Coding
Now let’s start adding this parameter to the code. To simplify the example, declare a variable and assign it a value. In a real application, this value can be loaded for example from the database.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public static void main(String[] args) throws FileNotFoundException, IOException { List cars = generateTestCarData(); String companyName = "Cars LTD"; File initialFile = new File("input_excel_template.xls"); try (InputStream is = new FileInputStream(initialFile)) { try (OutputStream os = new FileOutputStream("output_excel_template.xls")) { Context context = new Context(); context.putVar("cars", cars); context.putVar("companyName", companyName); JxlsHelper.getInstance().processTemplate(is, os, context); } } } |
In the above example, we added our parameter to the template.
Let’s see what happened.
As we see, the name of the company was successfully added to the template.