easypoi導出動態表頭excel
1: springBoot項目maven依賴:
<dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-spring-boot-starter</artifactId> <version>4.1.2</version> </dependency>
根據自己的poi版本選擇
<dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-spring-boot-starter</artifactId> <version>3.3.0</version> </dependency>
測試導出(數據組裝如下):
@Test public void dynaCol() { try { List<ExcelExportEntity> colList = new ArrayList<ExcelExportEntity>(); ExcelExportEntity colEntity = new ExcelExportEntity("商品名稱", "title"); colEntity.setNeedMerge(true); colList.add(colEntity); colEntity = new ExcelExportEntity("供應商", "supplier"); colEntity.setNeedMerge(true); colList.add(colEntity); ExcelExportEntity deliColGroup = new ExcelExportEntity("得力", "deli"); List<ExcelExportEntity> deliColList = new ArrayList<ExcelExportEntity>(); deliColList.add(new ExcelExportEntity("市場價", "orgPrice")); deliColList.add(new ExcelExportEntity("專區價", "salePrice")); deliColGroup.setList(deliColList); colList.add(deliColGroup); ExcelExportEntity jdColGroup = new ExcelExportEntity("京東", "jd"); List<ExcelExportEntity> jdColList = new ArrayList<ExcelExportEntity>(); jdColList.add(new ExcelExportEntity("市場價", "orgPrice")); jdColList.add(new ExcelExportEntity("專區價", "salePrice")); jdColGroup.setList(jdColList); colList.add(jdColGroup); List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); for (int i = 0; i < 10; i++) { Map<String, Object> valMap = new HashMap<String, Object>(); valMap.put("title", "名稱." + i); valMap.put("supplier", "供應商." + i); List<Map<String, Object>> deliDetailList = new ArrayList<Map<String, Object>>(); for (int j = 0; j < 3; j++) { Map<String, Object> deliValMap = new HashMap<String, Object>(); deliValMap.put("orgPrice", "得力.市場價." + j); deliValMap.put("salePrice", "得力.專區價." + j); deliDetailList.add(deliValMap); } valMap.put("deli", deliDetailList); List<Map<String, Object>> jdDetailList = new ArrayList<Map<String, Object>>(); for (int j = 0; j < 2; j++) { Map<String, Object> jdValMap = new HashMap<String, Object>(); jdValMap.put("orgPrice", "京東.市場價." + j); jdValMap.put("salePrice", "京東.專區價." + j); jdDetailList.add(jdValMap); } valMap.put("jd", jdDetailList); list.add(valMap); } Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("價格分析表", "數據"), colList, list); FileOutputStream fos = new FileOutputStream("D:/價格分析表.tt.xls"); workbook.write(fos); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
導出結果圖:
多sheet導出(數據組裝)
public String export(){ Workbook workBook = null; try { List<DeptUtil> exportList = exportService.exportList(); System.err.println(JSONArray.toJSONString(exportList)); // 創建參數對象(用來設定excel得sheet得內容等信息) ExportParams deptExportParams = new ExportParams(); // 設置sheet得名稱 deptExportParams.setSheetName("員工報表1"); // 創建sheet1使用得map Map<String, Object> deptExportMap = new HashMap<>(); // title的參數為ExportParams類型,目前僅僅在ExportParams中設置了sheetName deptExportMap.put("title", deptExportParams); // 模版導出對應得實體類型 deptExportMap.put("entity", DeptUtil.class); // sheet中要填充得數據 deptExportMap.put("data", exportList); ExportParams empExportParams = new ExportParams(); empExportParams.setSheetName("員工報表2"); // 創建sheet2使用得map Map<String, Object> empExportMap = new HashMap<>(); empExportMap.put("title", empExportParams); empExportMap.put("entity", DeptUtil.class); empExportMap.put("data", exportList); // 將sheet1、sheet2、sheet3使用得map進行包裝 List<Map<String, Object>> sheetsList = new ArrayList<>(); sheetsList.add(deptExportMap); sheetsList.add(empExportMap); // 執行方法 workBook = ExcelExportUtil.exportExcel(sheetsList, ExcelType.HSSF); fileName = URLEncoder.encode("員工報表導出", "UTF-8"); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); workBook.write(outputStream); outputStream.flush(); byte[] byteArray = outputStream.toByteArray(); excelStream = new ByteArrayInputStream(byteArray,0,byteArray.length); outputStream.close(); }catch (Exception e){ e.printStackTrace(); }finally { if(workBook != null) { try { workBook.close(); } catch (IOException e) { e.printStackTrace(); } } } return "success"; }
什么場景該用哪個方法?
- 導出 1.正規excel導出 (格式簡單,數據量可以,5W以內吧) 注解方式: ExcelExportUtil.exportExcel(ExportParams entity, Class<?> pojoClass,Collection<?> dataSet) 2.不定多少列,但是格式依然簡單數據庫不大 自定義方式: ExcelExportUtil.exportExcel(ExportParams entity, List<ExcelExportEntity> entityList,Collection<?> dataSet) 3.數據量大超過5W,還在100W以內 注解方式 ExcelExportUtil.exportBigExcel(ExportParams entity, Class<?> pojoClass,IExcelExportServer server, Object queryParams) 自定義方式: ExcelExportUtil.exportBigExcel(ExportParams entity, List<ExcelExportEntity> excelParams,IExcelExportServer server, Object queryParams) 4.樣式復雜,數據量盡量別大 模板導出 ExcelExportUtil.exportExcel(TemplateExportParams params, Map<String, Object> map) 5.一次導出多個風格不一致的sheet 模板導出 ExcelExportUtil.exportExcel(Map<Integer, Map<String, Object>> map,TemplateExportParams params) 6.一個模板但是要導出非常多份 模板導出 ExcelExportUtil.exportExcelClone(Map<Integer, List<Map<String, Object>>> map,TemplateExportParams params) 7.模板無法滿足你的自定義,試試html 自己構造html,然后我給你轉成excel ExcelXorHtmlUtil.htmlToExcel(String html, ExcelType type) 8.數據量過百萬級了.放棄excel吧,csv導出 注解方式: CsvExportUtil.exportCsv(CsvExportParams params, Class<?> pojoClass, OutputStream outputStream) 自定義方式: CsvExportUtil.exportCsv(CsvExportParams params, List<ExcelExportEntity> entityList, OutputStream outputStream) 9.word導出 模板導出: WordExportUtil.exportWord07(String url, Map<String, Object> map) 10.PDF導出 模板導出: TODO - 導入 如果想提高性能 ImportParams 的concurrentTask 可以幫助並發導入,僅單行,最小1000 excel有單個的那種特殊讀取,readSingleCell 參數可以支持 1. 不需要檢驗,數據量不大(5W以內) 注解或者MAP: ExcelImportUtil.importExcel(File file, Class<?> pojoClass, ImportParams params) 2. 需要導入,數據量不大 注解或者MAP: ExcelImportUtil.importExcelMore(InputStream inputstream, Class<?> pojoClass, ImportParams params) 3. 數據量大了,或者你有特別多的導入操作,內存比較少,僅支持單行 SAX方式 ExcelImportUtil.importExcelBySax(InputStream inputstream, Class<?> pojoClass, ImportParams params, IReadHandler handler) 4. 數據量超過EXCEL限制,CSV讀取 小數據量: CsvImportUtil.importCsv(InputStream inputstream, Class<?> pojoClass,CsvImportParams params) 大數據量: CsvImportUtil.importCsv(InputStream inputstream, Class<?> pojoClass,CsvImportParams params, IReadHandler readHandler)
參考:
使用教程:
https://opensource.afterturn.cn/doc/easypoi.html#4
http://doc.wupaas.com/docs/easypoi/easypoi-1c0u4mo8p4ro8
鏈接:https://pan.baidu.com/s/1gBHBI4Lx-roEXrVwvzaBxQ
提取碼:dbht
測試項目:
http://git.oschina.net/lemur/easypoi-test
.......