| | |
| | | } |
| | | |
| | | /** |
| | | * å¤sheet模æ¿å¯¼åº æ¨¡æ¿æ ¼å¼ä¸º {key.屿§} |
| | | * |
| | | * @param filename æä»¶å |
| | | * @param templatePath 模æ¿è·¯å¾ resource ç®å½ä¸çè·¯å¾å
æ¬æ¨¡æ¿æä»¶å |
| | | * ä¾å¦: excel/temp.xlsx |
| | | * éç¹: æ¨¡æ¿æä»¶å¿
é¡»æ¾ç½®å°å¯å¨ç±»å¯¹åºç resource ç®å½ä¸ |
| | | * @param data 模æ¿éè¦çæ°æ® |
| | | * @param response ååºä½ |
| | | */ |
| | | public static void exportTemplateMultiSheet(List<Map<String, Object>> data, String filename, String templatePath, HttpServletResponse response) { |
| | | try { |
| | | resetResponse(filename, response); |
| | | ServletOutputStream os = response.getOutputStream(); |
| | | exportTemplateMultiSheet(data, templatePath, os); |
| | | } catch (IOException e) { |
| | | throw new RuntimeException("导åºExcelå¼å¸¸"); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å¤è¡¨å¤æ°æ®æ¨¡æ¿å¯¼åº æ¨¡æ¿æ ¼å¼ä¸º {key.屿§} |
| | | * |
| | | * @param templatePath 模æ¿è·¯å¾ resource ç®å½ä¸çè·¯å¾å
æ¬æ¨¡æ¿æä»¶å |
| | |
| | | } |
| | | |
| | | /** |
| | | * å¤sheet模æ¿å¯¼åº æ¨¡æ¿æ ¼å¼ä¸º {key.屿§} |
| | | * |
| | | * @param templatePath 模æ¿è·¯å¾ resource ç®å½ä¸çè·¯å¾å
æ¬æ¨¡æ¿æä»¶å |
| | | * ä¾å¦: excel/temp.xlsx |
| | | * éç¹: æ¨¡æ¿æä»¶å¿
é¡»æ¾ç½®å°å¯å¨ç±»å¯¹åºç resource ç®å½ä¸ |
| | | * @param data 模æ¿éè¦çæ°æ® |
| | | * @param os è¾åºæµ |
| | | */ |
| | | public static void exportTemplateMultiSheet(List<Map<String, Object>> data, String templatePath, OutputStream os) { |
| | | ClassPathResource templateResource = new ClassPathResource(templatePath); |
| | | ExcelWriter excelWriter = EasyExcel.write(os) |
| | | .withTemplate(templateResource.getStream()) |
| | | .autoCloseStream(false) |
| | | // 大æ°å¼èªå¨è½¬æ¢ 鲿¢å¤±ç |
| | | .registerConverter(new ExcelBigNumberConvert()) |
| | | .build(); |
| | | if (CollUtil.isEmpty(data)) { |
| | | throw new IllegalArgumentException("æ°æ®ä¸ºç©º"); |
| | | } |
| | | for (int i = 0; i < data.size(); i++) { |
| | | WriteSheet writeSheet = EasyExcel.writerSheet(i).build(); |
| | | for (Map.Entry<String, Object> map : data.get(i).entrySet()) { |
| | | // 设置å表åç»è¿ææ°æ® |
| | | FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build(); |
| | | if (map.getValue() instanceof Collection) { |
| | | // å¤è¡¨å¯¼åºå¿
é¡»ä½¿ç¨ FillWrapper |
| | | excelWriter.fill(new FillWrapper(map.getKey(), (Collection<?>) map.getValue()), fillConfig, writeSheet); |
| | | } else { |
| | | excelWriter.fill(map.getValue(), writeSheet); |
| | | } |
| | | } |
| | | } |
| | | excelWriter.finish(); |
| | | } |
| | | |
| | | /** |
| | | * éç½®ååºä½ |
| | | */ |
| | | private static void resetResponse(String sheetName, HttpServletResponse response) throws UnsupportedEncodingException { |