多sheet導出
public class Test{ @Data @ColumnWidth(20) public static class TestVO { @ExcelProperty( value = "姓名",index = 0) private String name; @ExcelProperty( value = "年齡",index = 1) private int age; @ExcelProperty( value = "學校",index = 2) private String school; } /** * 多個sheet導入測試 * @throws FileNotFoundException */ @Test public void sheetImport() throws FileNotFoundException { // 輸出流 OutputStream outputStream = null; outputStream = new FileOutputStream(new File("D:/1.xlsx")); // 導出的數據 List<TestVO> dataList = new ArrayList<>(); for (int i = 0; i < 10; i++) { TestVO testVO = new TestVO(); testVO.setAge(i + 20); testVO.setName("vo" + i); testVO.setSchool("school" + i); dataList.add(testVO); } // 標題 List<String> headList = Arrays.asList("姓名", "年齡", "學校"); // 測試多sheel導出 ExcelWriter excelWriter = EasyExcel.write(outputStream).build(); WriteSheet test1 = EasyExcel.writerSheet(0, "test1").head(TestVO.class).build(); WriteSheet test2 = EasyExcel.writerSheet(1, "test2").head(TestVO.class).build(); excelWriter.write(dataList,test1).write(dataList,test2); excelWriter.finish(); } }
// 表頭樣式 WriteCellStyle headWriteCellStyle = new WriteCellStyle(); // 單元格樣式 WriteCellStyle contentWriteCellStyle = new WriteCellStyle(); // 初始化表格樣式 HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle); WriteSheet test1 = EasyExcel.writerSheet(0, "test1").head(TestVO.class). registerWriteHandler(horizontalCellStyleStrategy).build();
導出效果
2.2 多sheet導入
public class Test{
@Data
@ColumnWidth(20)
public static class TestVO {
@ExcelProperty( value = "姓名",index = 0)
private String name;
@ExcelProperty( value = "年齡",index = 1)
private int age;
@ExcelProperty( value = "學校",index = 2)
private String school;
}
@Data
@ColumnWidth(20)
public static class TestVO1 {
@ExcelProperty( value = "姓名",index = 0)
private String name;
@ExcelProperty( value = "年齡",index = 1)
private int age;
@ExcelProperty( value = "學校",index = 2)
private String school;
}
/**
* 測試導入多個sheet導入
* @throws Exception
*/
@Test
public void read() throws Exception {
String filePath = "D:/1.xlsx";
InputStream inputStream = null;
inputStream = new FileInputStream(new File(filePath));
AnalysisEventListenerImpl<Object> listener = new AnalysisEventListenerImpl<>();
ExcelReader excelReader = EasyExcel.read(inputStream,listener).build();
// 第一個sheet讀取類型
ReadSheet readSheet1 = EasyExcel.readSheet(0).head(TestVO.class).build();
// 第二個sheet讀取類型
ReadSheet readSheet2 = EasyExcel.readSheet(1).head(TestVO1.class).build();
// 開始讀取第一個sheet
excelReader.read(readSheet1);
List<Object> list = listener.getDatas();
list.forEach((user)->{
TestVO user1= (TestVO) user;
System.out.println(user1.getName()+", "+user1.getAge()+", "+user1.getSchool());
});
// 清空之前的數據
listener.getDatas().clear();
// 開始讀取第二個sheet
excelReader.read(readSheet2);
System.out.println("---------------------------------");
List<Object> list2 = listener.getDatas();
list2.forEach((user)->{
TestVO1 user2= (TestVO1) user;
System.out.println(user2.getName()+", "+user2.getAge()+", "+user2.getSchool());
});
}
}
作者:任未然
鏈接:https://www.jianshu.com/p/7017d94ed924
