一、Maven依賴
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>3.2.0</version>
</dependency>
二、編輯導出數據對象類,使用注解的形式進行標記各個字段類型,name對應excel表格的表頭,orderNum每一列排序,從左到右,width代表每一列的寬度,具體代碼如下(省略Get和Set方法)
public class AssetsStatistics { @Excel(name = "序號", orderNum = "0",width = 10) private String id; @Excel(name = "所屬公司", orderNum = "1",width = 20) private String provincename; @Excel(name = "公司編碼", orderNum = "2",width = 10) private String provincecode; @Excel(name = "ip", orderNum = "3",width = 15) private String ip; @Excel(name = "資產URL", orderNum = "4",width = 20) private String url; }
三、設置excel自定義樣式PoiExportStyle
public class PoiExportStyle extends ExcelExportStylerDefaultImpl implements IExcelExportStyler { public PoiExportStyle(Workbook workbook) { super(workbook); } @Override public CellStyle getHeaderStyle(short headerColor) { CellStyle titleStyle = workbook.createCellStyle(); Font font = workbook.createFont(); font.setFontHeightInPoints((short) 20); titleStyle.setFont(font); titleStyle.setBorderLeft((short) 1); // 左邊框 titleStyle.setBorderRight((short) 1); // 右邊框 titleStyle.setBorderBottom((short) 1); titleStyle.setBorderTop((short) 1); titleStyle.setFillForegroundColor((short) 41); // 填充的背景顏色 titleStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND.getCode()); titleStyle.setAlignment(CellStyle.ALIGN_CENTER); titleStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); return titleStyle; } @Override public CellStyle stringNoneStyle(Workbook workbook, boolean isWarp) { CellStyle style = workbook.createCellStyle(); style.setBorderLeft((short) 1); // 左邊框 style.setBorderRight((short) 1); // 右邊框 style.setBorderBottom((short) 1); style.setBorderTop((short) 1); style.setAlignment(CellStyle.ALIGN_CENTER); style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); style.setDataFormat(STRING_FORMAT); if (isWarp) { style.setWrapText(true); } return style; } @Override public CellStyle getTitleStyle(short color) { CellStyle titleStyle = workbook.createCellStyle(); titleStyle.setBorderLeft((short) 1); // 左邊框 titleStyle.setBorderRight((short) 1); // 右邊框 titleStyle.setBorderBottom((short) 1); titleStyle.setBorderTop((short) 1); titleStyle.setFillForegroundColor((short) 41); // 填充的背景顏色 titleStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND.getCode()); titleStyle.setAlignment(CellStyle.ALIGN_CENTER); titleStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); titleStyle.setWrapText(true); return titleStyle; } @Override public CellStyle stringSeptailStyle(Workbook workbook, boolean isWarp) { return isWarp ? stringNoneWrapStyle : stringNoneStyle; }
四、引用excel自定義樣式封裝成工具類
public class PoiUtil { public static void creatExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName, String directory) throws IOException { ExportParams exportParams = new ExportParams(title, sheetName); exportParams.setHeaderHeight((double)100); exportParams.setStyle(PoiExportStyle.class); Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list); if (workbook != null);{ //將Excel文件放到本地路徑下 String directory = "D:/data"; // 本地目錄 FileOutputStream os = new FileOutputStream(directory+fileName); workbook.write(os); os.close(); } } }
五、list數據生成excel文件---單個sheet和多個sheet
//單個sheet public String creatAssetsQuarterExcel() throws Exception { String title = "2020年第1季度資產清單"; List<AssetsStatistics> list = secAssetsService.selectListByTime();//從數據庫查詢數據 if(!CollectionUtils.isEmpty(list)) { Integer no = 1; for (AssetsStatistics v : list) { v.setId(String.valueOf(no++)); } } PoiUtil.creatExcel(list, title, title, AssetsStatistics.class, title+".xls", excelReport); return title+".xls"; }
//多sheet public String creatMonthExcelByAssets() throws Exception { Workbook workBook = null; try { // 主機資產表 List<AssetsStatistics> hostlist = countHostAssets(); Map<String, Object> hostExportMap = getSheetMap("主機資產表",hostlist); // 數據庫資產表 List<AssetsStatistics> dblist = countDbAssets(); Map<String, Object> dbExportMap = getSheetMap("數據庫資產表",dblist); // 將sheet1、sheet2使用得map進行包裝 List<Map<String, Object>> sheetsList = new ArrayList<>(); sheetsList.add(hostExportMap); sheetsList.add(dbExportMap); workBook = ExcelExportUtil.exportExcel(sheetsList, ExcelType.HSSF); // String fileName = URLEncoder.encode("資產表.xls", "UTF-8"); String fileName ="資產表.xls"; if (workBook != null){ //將Excel文件放到本地路徑下 FileOutputStream os = new FileOutputStream(excelReport+fileName); workBook.write(os); os.close(); } return fileName; }catch (Exception e){ e.printStackTrace(); return e.getMessage(); }finally { if(workBook != null) { try { workBook.close(); } catch (IOException e) { e.printStackTrace(); return e.getMessage(); } } } } /** * 設置excel 中 sheet 的參數 * @param sheetName sheet名稱 * @param list sheet 數據列表 * @return */ private Map<String, Object> getSheetMap(String sheetName,List<AssetsProblemStatistics> list) { // 創建參數對象(用來設定excel得sheet得內容等信息) ExportParams exportParams = new ExportParams(); exportParams.setHeaderHeight((double)100); exportParams.setStyle(PoiExportStyle.class); // 設置sheet得名稱 exportParams.setSheetName(sheetName); // 創建sheet1使用得map Map<String, Object> sheetExportMap = new HashMap<>(); // title的參數為ExportParams類型,目前僅僅在ExportParams中設置了sheetName sheetExportMap.put("title", exportParams); // 模版導出對應得實體類型 sheetExportMap.put("entity", AssetsProblemStatistics.class); // sheet中要填充得數據 sheetExportMap.put("data", list); return sheetExportMap; }
六、結果展示