详细的easyExcel填充数据填充图片及导出示例
生活随笔
收集整理的這篇文章主要介紹了
详细的easyExcel填充数据填充图片及导出示例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
添加依賴
<dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId><version>2.2.7</version> </dependency> <!--- easyexcel依賴poi ---> <dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>4.1.2</version> </dependency> <dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>4.1.2</version> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> <dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.75</version> </dependency>Excel模板
最終效果
代碼示例
模板上{.}與{前綴.}都是對應的是集合屬性,{}里面要與屬性字段名對應,如果模板里只有一個列表集合可用{.},多個列表可使用多個{前綴.}添加多個
OutputStream out = null;try {File templateFile = new File("D:\\template.xlsx");String excelType = templateFile.getName().substring(templateFile.getName().lastIndexOf(".") + 1);HttpServletResponse response = getResponse();out = response.getOutputStream();response.addHeader("Content-Disposition","attachment;filename=" + new String("填充后的template".getBytes(),"iso-8859-1") + "." + excelType);response.setContentType("application/msexcel;charset=UTF-8");ExcelWriterBuilder easyExcelFactory = EasyExcel.write(out);// easyExcey可以自動判斷Excel類型,如果報錯可以手動設置類型,個人測試如果模板不是本地文件,直接從網絡獲取的文件流的話,需要手動設置類型 // URL url = new URL("http://"); // HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); // if (httpURLConnection.getResponseCode() == 200) { // ExcelWriter excelWriter = easyExcelFactory.withTemplate(httpURLConnection.getInputStream()).build(); // } // if ("xls".equals(excelType)) { // easyExcelFactory.excelType(ExcelTypeEnum.XLS); // }ExcelWriter excelWriter = easyExcelFactory.withTemplate(templateFile).build();WriteSheet writeSheet = EasyExcel.writerSheet().registerWriteHandler(new MergeStrategyHandler()).build();// 寫入普通變量JSONObject jsonObject = new JSONObject();jsonObject.put("total", 5000);// 可以填充圖片 // ByteArrayOutputStream byteArrayOut = null; // try { // String imageUrlStr = "http://127.0.0.1:8080/image.jpg"; // URL url = new URL(imageUrlStr); // HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); // if (httpURLConnection.getResponseCode() == 200) { // BufferedImage bufferImg = ImageIO.read(url.openStream()); // byteArrayOut = new ByteArrayOutputStream(); // // 圖片后綴格式 // String sfx = imageUrlStr.substring(imageUrlStr.lastIndexOf(".") + 1); // ImageIO.write(bufferImg, sfx, byteArrayOut); // bufferImg.flush(); // jsonObject.put("image", byteArrayOut.toByteArray()); // } // } catch (IOException e) { // e.printStackTrace(); // } finally { // if (byteArrayOut != null) { // byteArrayOut.close(); // } // }excelWriter.fill(jsonObject, writeSheet);// 列表數據填充FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();// 如果有多個list 模板上必須有{前綴.} 這里的前綴使用表名,然后多個list必須用 FillWrapper包裹JSONArray jsonArray = new JSONArray();for (int i = 0; i < 3; i++) {JSONObject obj = new JSONObject();obj.put("field1", i + "_field1");obj.put("field2", i + "_field2");obj.put("field3", i + "_field3");obj.put("field4", i + "_field4");obj.put("field5", i + "_field5");jsonArray.add(obj);}excelWriter.fill(new FillWrapper("table", jsonArray), fillConfig, writeSheet);excelWriter.finish();} catch (Exception e) {e.printStackTrace();} finally {if (out != null) {try {out.close();} catch (IOException e) {e.printStackTrace();}}} /*** 模板中填充集合有合并單元格,第二行開始不會自動合并單元格,需要復制上一行的信息進行合并*/static class MergeStrategyHandler extends AbstractMergeStrategy {@Overrideprotected void merge(Sheet sheet, Cell cell, Head head, Integer relativeRowIndex) {if (relativeRowIndex == null || relativeRowIndex == 0) {return;}int rowIndex = cell.getRowIndex();int colIndex = cell.getColumnIndex();sheet = cell.getSheet();Row preRow = sheet.getRow(rowIndex - 1);Cell preCell = preRow.getCell(colIndex);CellStyle cs = cell.getCellStyle();cell.setCellStyle(cs);for (CellRangeAddress cellRangeAddress : sheet.getMergedRegions()) {if (cellRangeAddress.containsRow(preCell.getRowIndex()) && cellRangeAddress.containsColumn(preCell.getColumnIndex())) {int lastColIndex = cellRangeAddress.getLastColumn();int firstColIndex = cellRangeAddress.getFirstColumn();CellRangeAddress cra = new CellRangeAddress(cell.getRowIndex(), cell.getRowIndex(), firstColIndex, lastColIndex);sheet.addMergedRegion(cra);RegionUtil.setBorderBottom(BorderStyle.THIN, cra, sheet);RegionUtil.setBorderLeft(BorderStyle.THIN, cra, sheet);RegionUtil.setBorderRight(BorderStyle.THIN, cra, sheet);RegionUtil.setBorderTop(BorderStyle.THIN, cra, sheet);return;}}}}附上讀取模板變量名 https://blog.csdn.net/weixin_43686429/article/details/115792722
官方文檔地址 https://www.yuque.com/easyexcel/doc/fill
需要更強大的填充,可以使用easypoi,有更多的語法 http://easypoi.mydoc.io
總結
以上是生活随笔為你收集整理的详细的easyExcel填充数据填充图片及导出示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JS和JSP的区别?
- 下一篇: 汉诺塔的移动次数和移动过程