For more exciting posts, please visit my personal blog


Jxcel profile

Jxcel is a toolkit for converting Java objects to Excel (currently XLSX, XLS).

Project address: github.com/jptangchina…

Features that

  • Java objects are output as Excel files or Workbook objects
  • Semantically converts numeric or Boolean values to and from semantically formatted values
  • The resulting Excel file can sort the columns
  • The table header matches the Java property exactly
  • Supports conversion of almost all basic data types as well as date types
  • The date format is customized
  • Table width adaptive
  • . More features

Importing dependency packages

Using Maven as an example, introducing Jxcel dependencies:

<dependency>
    <groupId>com.jptangchina</groupId>
    <artifactId>jxcel</artifactId>
    <version>${jxcel.version}</version>
</dependency>
Copy the code

Prepare data model

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@JxcelSheet(Student List)
class Student {
    @JxcelCell("Age")
    private int age;
    @JxcelCell(value = "Gender", parse = {"Male"."Female"})
    private int sex;
    @JxcelCell(value = "Name", order = 1)
    private String name;
    @JxcelCell(value = "Date of birth", format = "yyyy-MM-dd")
    private Date birthDay;
    @JxcelCell(value = "Mobile phone Number", suffix = "\t")
    private String mobile;
}
Copy the code

Export data to Excel

// Export as an XLS Workbook object
JxcelGenrator.xlsGenrator().generateWorkbook(Arrays.asList(new Student()));
// Export as an XLSX Workbook object
JxcelGenrator.xlsxGenrator().generateWorkbook(Arrays.asList(new Student()));
// Export to XLS file
JxcelGenrator.xlsGenrator().generateFile(Arrays.asList(new Student()));
// Export to XLSX file
JxcelGenrator.xlsxGenrator().generateFile(Arrays.asList(new Student()));
Copy the code

Parse Excel into Java objects

// Parse from file
JxcelParser.parser().parseFromFile(Student.class, new File(filePath));
// Parse from Workbood objects
JxcelParser.parser().parseFromWorkbook(Student.class, workbook);
Copy the code

example

Student student = new Student(18.0."JptangChina".new Date(), "18510010000");
JxcelGenrator.xlsxGenrator().generateFile(Arrays.asList(student), "/home/jptangchina/test.xlsx");
Copy the code

The output table is as follows:


For more exciting posts, please visit my personal blog