以Maven为例,引入Jxcel依赖包:
<dependency>
<groupId>com.jptangchina</groupId>
<artifactId>jxcel</artifactId>
<version>${jxcel.version}</version>
</dependency>
复制代码
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@JxcelSheet("学生名单")
class Student {
@JxcelCell("年龄")
private int age;
@JxcelCell(value = "性别", parse = {"男", "女"})
private int sex;
@JxcelCell(value = "姓名", order = 1)
private String name;
@JxcelCell(value = "出生日期", format = "yyyy-MM-dd")
private Date birthDay;
@JxcelCell(value = "手机号", suffix = "/t")
private String mobile;
}
复制代码
// 导出为XLS Workbook对象 JxcelGenrator.xlsGenrator().generateWorkbook(Arrays.asList(new Student())); // 导出为XLSX Workbook对象 JxcelGenrator.xlsxGenrator().generateWorkbook(Arrays.asList(new Student())); // 导出为XLS文件 JxcelGenrator.xlsGenrator().generateFile(Arrays.asList(new Student())); // 导出为XLSX文件 JxcelGenrator.xlsxGenrator().generateFile(Arrays.asList(new Student())); 复制代码
// 从文件解析 JxcelParser.parser().parseFromFile(Student.class, new File(filePath)); // 从Workbood对象解析 JxcelParser.parser().parseFromWorkbook(Student.class, workbook); 复制代码
Student student = new Student(18, 0, "JptangChina", new Date(), "18510010000"); JxcelGenrator.xlsxGenrator().generateFile(Arrays.asList(student), "/home/jptangchina/test.xlsx"); 复制代码
输出的表格如下:
更多精彩博文,欢迎访问 我的个人博客