更多精彩博文,歡迎訪問個人我的博客java
Jxcel是一個支持Java對象與Excel(目前僅xlsx、xls)互相轉換的工具包。git
項目地址:https://github.com/jptangchin...github
以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");
輸出的表格以下:spa
更多精彩博文,歡迎訪問個人我的博客3d