jxls 2 use and share

First, let's say we're going to export a list of Employee objects to Excel. The Employee class is defined as follows:

 

publicclassEmployee{
  privateString name;
  privateDate birthDate;
  privateBigDecimal payment;
  privateBigDecimal bonus;
  // ... constructors// ... getters/setters
}
 

 

In order to output this list of objects into excel using Jxls we need:

1. Add the jar package that Jxls depends on to your project.

2. Create an Excel template with special callouts.

3. Populate the Employee data into the prepared template using Jxls API.

 

Let's take a closer look at how to use each step:

Add the Jxls dependent jar package to your project
The easiest way to use it is through Maven, in order to add the required package to the configuration file, import.
The Jxls jar is available in the Centeral Maven repository.
We need to add the following core Jxls dependencies:
<dependency>
  <groupId>org.jxls</groupId>
  <artifactId>jxls</artifactId>
  <version>2.3.0</version>
</dependency>
 
Alternatively, you can download the Jxls distribution via Sourceforge and use the jars in the distribution.
In addition to these, we need to add the implementation package of Jxls transformer engine, which is used to operate excel under Java.
 
Since Jxls does not depend on any specific java to manipulate excel package, Jxls defines some public interfaces. Now Jxls implements two packages of this interface: Apache POI and Java Excel API, which are distributed in different jars.
In order to use Apache POI, the following dependencies need to be added:
<dependency>
  <groupId>org.jxls</groupId>
  <artifactId>jxls-poi</artifactId>
  <version>1.0.9</version>
</dependency>
 
In order to use the Java Excel API, the following dependencies need to be added:
<dependency>
  <groupId>org.jxls</groupId>
  <artifactId>jxls-jexcel</artifactId>
  <version>1.0.6</version>
</dependency>
 
 
Create an Excel template
Templates are specially marked excel files that write how Jxls outputs data.
Jxls provides internal tag processors for parsing templates and extracting commands.
You can also customize the tag processor if you want. At the same time, you can customize the markup, as long as you ensure that it is converted into Jxls commands in a reasonable way.
 
Let's take a look at the tag processor inside Jxls.
Jxls supports the Apache JEXL expression language by default, which is used to manipulate the properties and methods of Java objects in templates. The object must be saved to the Jxls Context with a certain key. For example, we want to output the employee's name into excel, just write "${employee.name}" into the cell. Basically, as long as "${", "}" are added to both sides of the Jxls expression, it is of course guaranteed that the Employee object can be obtained through "employee" in the Context.
 
The attribute ID can be configured, or you can decide to use "[[employee.name]]" as the attribute ID.
The final template is as follows:
Template
 
As shown above, in the template, row 4 cell, we have introduced the employee object property with a JEXL expression.
Cell A1 contains a callout for the contents of jx:area(lastCell="D4") . It defines the root area A1:D4 of the template.
The callout for cell A4 defines the Jxls Each loop command with "jx:each(items="employees" var="employee" lastCell="D4")". The Each command traverses the list corresponding to "employees" in the Jxls Context, and stores each value in the list into the Context (defined by var) with the key of "employee". The content area of ​​the Each command is A4:D4 (defined by lastCell), and the Employee object of each context will copy this area and process the content.
 
This example uses the XlsCommentAreaBuilder to create the Jxls area from a template. Just define the Jxls command into the annotation and that's it. If you define the command via Java code, you can simply remove the annotation.
Working with templates using the Jxls API
Below you can see how the excel template is handled through the Jxls API:
...
  logger.info("Running Object Collection demo");
  List<Employee> employees = generateSampleEmployeeData();
  try(InputStreamis=ObjectCollectionDemo.class.getResourceAsStream("object_collection_template.xls")){
    try(OutputStream os =newFileOutputStream("target/object_collection_output.xls")){
      Context context =newContext();
      context.putVar("employees", employees);
      JxlsHelper.getInstance().processTemplate(is, os, context);
    }
}
...
 
In the above example we read the template file object_collection_template.xls in the classpath . The object file will be output to target/object_collection_output.xls .
One line of code handles the main job.
By default, JxlsHelper outputs to the form where the template is located.
You can also output the results to other forms through the convenience below.
The following results will be saved in the A1 cell of the Result form.
Output
 
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326459154&siteId=291194637