更多精彩博文,歡迎訪問個人我的博客java
Jxcel是一個支持Java對象與Excel(目前僅xlsx、xls)互相轉換的工具包。git
項目地址:github.com/jptangchina…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
更多精彩博文,歡迎訪問個人我的博客code