1. 引入依賴
<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>
2.Excel模板
3.具體業務代碼
FileOutputStream fileOut; BufferedImage bufferImg1; BufferedImage bufferImg2; Map resMap = new HashMap(); try {
//數據庫查出的對象 具體根據業務邏輯更改 EngineeringVo ev = engineeringExplorationMapper.findEngineeringVoByAssetsId(assetsId); TemplateExportParams params = new TemplateExportParams( "D:/excel/光交箱勘測表.xls");
//填充數據
//注意map中put的值 不要為空binocularOperation()自己寫的三目運算為空處理辦法
Map<String, Object> map = new HashMap<>(); map.put("projectName", binocularOperation(ev.getProjectName())); map.put("contactsTelephone", binocularOperation(ev.getContactsTelephone())); map.put("assetsCode", binocularOperation(ev.getAssetsCode())); map.put("address", binocularOperation(ev.getAddress())); map.put("boxSize", binocularOperation(ev.getBoxSize())); map.put("capacity", binocularOperation(ev.getCapacity())); map.put("operationSurfaces", binocularOperation(ev.getOperationSurfaces())); map.put("boxBaseSize", binocularOperation(ev.getBoxBaseSize())); map.put("adapters", binocularOperation(ev.getAdapters())); map.put("environmentDescription", binocularOperation(ev.getEnvironmentDescription())); map.put("expansionDirections", binocularOperation(ev.getExpansionDirections())); map.put("capacityExpansion", binocularOperation(ev.getCapacityExpansion())); map.put("description", binocularOperation(ev.getDescription())); map.put("userName", binocularOperation(ev.getUserName())); map.put("tel", binocularOperation(ev.getTel())); String time = " "; Date createTime = ev.getCreateTime(); SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd"); if (null != createTime) { time = sf.format(createTime); } map.put("createTime", time);
//這里需要強轉 插入圖片需要XSSFWorkbook對象
//Workbook不可以轉為HSSFWorkbook 具體邏輯調試看結果集 XSSFWorkbook workbook = (XSSFWorkbook) ExcelExportUtil.exportExcel(params, map); String time2 = " "; SimpleDateFormat sf2 = new SimpleDateFormat("yyyyMMddHHmmss"); if (null != createTime) { time2 = sf2.format(createTime); } String assetsPicture = ev.getAssetsPicture(); //插入圖片 if (!StringUtil.isEmpty(assetsPicture)) { String replace = assetsPicture.replace(ur, di).replace("[", "").replace("]", "").replace("\"", ""); String[] split = replace.split(","); if(split.length == 1){ String s = split[0]; String picturePath = s.replace(ur, di); ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream(); //加載圖片 bufferImg1 = ImageIO.read(new File(picturePath)); ImageIO.write(bufferImg1, "png", byteArrayOut); XSSFSheet sheet1 = workbook.getSheet("sheet1"); XSSFDrawing patriarch = sheet1.createDrawingPatriarch(); //圖片 1 3 11 7 18 意思為 橫軸3 縱軸11 橫軸3 縱軸18 XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, (short) 3, 11, (short) 7, 18); patriarch.createPicture(anchor, workbook.addPicture(byteArrayOut.toByteArray(), XSSFWorkbook.PICTURE_TYPE_PNG)); //圖片2 anchor = new XSSFClientAnchor(-200, 0, 0, 0, (short) 9, 11, (short) 12, 18); patriarch.createPicture(anchor, workbook.addPicture(byteArrayOut.toByteArray(), XSSFWorkbook.PICTURE_TYPE_PNG)); }else{ String s1 = split[0]; String picturePath1 = s1.replace(ur, di); String s2 = split[1]; String picturePath2 = s2.replace(ur, di); ByteArrayOutputStream byteArrayOut1 = new ByteArrayOutputStream(); ByteArrayOutputStream byteArrayOut2 = new ByteArrayOutputStream(); //加載圖片 bufferImg1 = ImageIO.read(new File(picturePath1)); bufferImg2 = ImageIO.read(new File(picturePath2)); log.info("加載圖片完成!!"); ImageIO.write(bufferImg1, "png", byteArrayOut1); ImageIO.write(bufferImg2, "png", byteArrayOut2); XSSFSheet sheet1 = workbook.getSheet("sheet1"); XSSFDrawing patriarch = sheet1.createDrawingPatriarch(); log.info("開始寫入圖片位置!!"); //圖片 1 XSSFClientAnchor anchor1 = new XSSFClientAnchor(0, 0, 0, 0, (short) 3, 11, (short) 7, 18); patriarch.createPicture(anchor1, workbook.addPicture(byteArrayOut1.toByteArray(), XSSFWorkbook.PICTURE_TYPE_PNG)); //圖片2 XSSFClientAnchor anchor2 = new XSSFClientAnchor(0, 0, 0, 0, (short) 9, 11, (short) 12, 18); patriarch.createPicture(anchor2, workbook.addPicture(byteArrayOut2.toByteArray(), XSSFWorkbook.PICTURE_TYPE_PNG)); } } String fileName = ev.getAssetsName() + "-" + time2 + ".xlsx"; fileOut = new FileOutputStream(fileDir + fileName); workbook.write(fileOut); fileOut.close(); resMap.put("url", fileUrl + fileName); } catch (IOException e) { e.printStackTrace(); }
4. 效果圖
5. 遍歷多行數據
fe的寫法 fe標志 冒號 list數據 單個元素數據(默認t,可以不寫) 第一個元素 {{$fe: maplist t t.id }}
上述獲取為list
填充數據
Map<String, Object> map = new HashMap<String, Object>(); List<Map<String, String>> listMap = new ArrayList<Map<String, String>>(); for (int i = 0; i < 4; i++) { Map<String, String> lm = new HashMap<String, String>(); lm.put("id", i + 1 + ""); lm.put("zijin", i * 10000 + ""); lm.put("bianma", "A001"); lm.put("mingcheng", "設計"); lm.put("xiangmumingcheng", "EasyPoi " + i + "期"); lm.put("quancheng", "開源項目"); lm.put("sqje", i * 10000 + ""); lm.put("hdje", i * 10000 + ""); listMap.add(lm); } map.put("maplist", listMap);
具體參考:http://easypoi.mydoc.io/ 中第三點
6.總結:
1) map中的值不能為null,不然會報空指針
2) workbook對象不能轉HSSFWorkbook,可以轉為XSSFWorkbook