From aa638475e955207a5d5f76f9ec99415b5a18f5db Mon Sep 17 00:00:00 2001 From: 疯狂的狮子li <15040126243@163.com> Date: 星期四, 25 八月 2022 12:30:32 +0800 Subject: [PATCH] update 同步 ruoyi 新功能 --- ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java | 136 +++++++++++++++++++++++++++++++++++++++++---- 1 files changed, 124 insertions(+), 12 deletions(-) diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java index 6168a96..e6a67b2 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java @@ -1,30 +1,44 @@ package com.ruoyi.common.utils.poi; +import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.io.resource.ClassPathResource; import cn.hutool.core.util.IdUtil; import com.alibaba.excel.EasyExcel; +import com.alibaba.excel.ExcelWriter; +import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder; +import com.alibaba.excel.write.metadata.WriteSheet; +import com.alibaba.excel.write.metadata.fill.FillConfig; +import com.alibaba.excel.write.metadata.fill.FillWrapper; import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy; import com.ruoyi.common.convert.ExcelBigNumberConvert; +import com.ruoyi.common.excel.CellMergeStrategy; import com.ruoyi.common.excel.DefaultExcelListener; import com.ruoyi.common.excel.ExcelListener; import com.ruoyi.common.excel.ExcelResult; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.file.FileUtils; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.InputStream; +import java.io.UnsupportedEncodingException; +import java.util.Collection; import java.util.List; +import java.util.Map; /** * Excel鐩稿叧澶勭悊 * * @author Lion Li */ +@NoArgsConstructor(access = AccessLevel.PRIVATE) public class ExcelUtil { /** - * 鍚屾瀵煎叆 + * 鍚屾瀵煎叆(閫傜敤浜庡皬鏁版嵁閲�) * * @param is 杈撳叆娴� * @return 杞崲鍚庨泦鍚� @@ -35,7 +49,7 @@ /** - * 浣跨敤鏍¢獙鐩戝惉鍣ㄥ鐞嗗鍏� + * 浣跨敤鏍¢獙鐩戝惉鍣� 寮傛瀵煎叆 鍚屾杩斿洖 * * @param is 杈撳叆娴� * @param clazz 瀵硅薄绫诲瀷 @@ -49,7 +63,7 @@ } /** - * 浣跨敤鑷畾涔夌洃鍚櫒瀵煎叆 + * 浣跨敤鑷畾涔夌洃鍚櫒 寮傛瀵煎叆 鑷畾涔夎繑鍥� * * @param is 杈撳叆娴� * @param clazz 瀵硅薄绫诲瀷 @@ -66,25 +80,123 @@ * * @param list 瀵煎嚭鏁版嵁闆嗗悎 * @param sheetName 宸ヤ綔琛ㄧ殑鍚嶇О - * @return 缁撴灉 + * @param clazz 瀹炰綋绫� + * @param response 鍝嶅簲浣� */ public static <T> void exportExcel(List<T> list, String sheetName, Class<T> clazz, HttpServletResponse response) { + exportExcel(list, sheetName, clazz, false, response); + } + + /** + * 瀵煎嚭excel + * + * @param list 瀵煎嚭鏁版嵁闆嗗悎 + * @param sheetName 宸ヤ綔琛ㄧ殑鍚嶇О + * @param clazz 瀹炰綋绫� + * @param merge 鏄惁鍚堝苟鍗曞厓鏍� + * @param response 鍝嶅簲浣� + */ + public static <T> void exportExcel(List<T> list, String sheetName, Class<T> clazz, boolean merge, HttpServletResponse response) { try { - String filename = encodingFilename(sheetName); - response.reset(); - FileUtils.setAttachmentResponseHeader(response, filename); - response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8"); + resetResponse(sheetName, response); ServletOutputStream os = response.getOutputStream(); - EasyExcel.write(os, clazz) + ExcelWriterSheetBuilder builder = EasyExcel.write(os, clazz) .autoCloseStream(false) // 鑷姩閫傞厤 .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()) // 澶ф暟鍊艰嚜鍔ㄨ浆鎹� 闃叉澶辩湡 .registerConverter(new ExcelBigNumberConvert()) - .sheet(sheetName).doWrite(list); + .sheet(sheetName); + if (merge) { + // 鍚堝苟澶勭悊鍣� + builder.registerWriteHandler(new CellMergeStrategy(list, true)); + } + builder.doWrite(list); } catch (IOException e) { throw new RuntimeException("瀵煎嚭Excel寮傚父"); } + } + + /** + * 鍗曡〃澶氭暟鎹ā鏉垮鍑� 妯℃澘鏍煎紡涓� {.灞炴� + * + * @param filename 鏂囦欢鍚� + * @param templatePath 妯℃澘璺緞 resource 鐩綍涓嬬殑璺緞鍖呮嫭妯℃澘鏂囦欢鍚� + * 渚嬪: excel/temp.xlsx + * 閲嶇偣: 妯℃澘鏂囦欢蹇呴』鏀剧疆鍒板惎鍔ㄧ被瀵瑰簲鐨� resource 鐩綍涓� + * @param data 妯℃澘闇�瑕佺殑鏁版嵁 + */ + public static void exportTemplate(List<Object> data, String filename, String templatePath, HttpServletResponse response) { + try { + resetResponse(filename, response); + ClassPathResource templateResource = new ClassPathResource(templatePath); + ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()) + .withTemplate(templateResource.getStream()) + .autoCloseStream(false) + // 澶ф暟鍊艰嚜鍔ㄨ浆鎹� 闃叉澶辩湡 + .registerConverter(new ExcelBigNumberConvert()) + .build(); + WriteSheet writeSheet = EasyExcel.writerSheet().build(); + if (CollUtil.isEmpty(data)) { + throw new IllegalArgumentException("鏁版嵁涓虹┖"); + } + // 鍗曡〃澶氭暟鎹鍑� 妯℃澘鏍煎紡涓� {.灞炴� + for (Object d : data) { + excelWriter.fill(d, writeSheet); + } + excelWriter.finish(); + } catch (IOException e) { + throw new RuntimeException("瀵煎嚭Excel寮傚父"); + } + } + + /** + * 澶氳〃澶氭暟鎹ā鏉垮鍑� 妯℃澘鏍煎紡涓� {key.灞炴� + * + * @param filename 鏂囦欢鍚� + * @param templatePath 妯℃澘璺緞 resource 鐩綍涓嬬殑璺緞鍖呮嫭妯℃澘鏂囦欢鍚� + * 渚嬪: excel/temp.xlsx + * 閲嶇偣: 妯℃澘鏂囦欢蹇呴』鏀剧疆鍒板惎鍔ㄧ被瀵瑰簲鐨� resource 鐩綍涓� + * @param data 妯℃澘闇�瑕佺殑鏁版嵁 + */ + public static void exportTemplateMultiList(Map<String, Object> data, String filename, String templatePath, HttpServletResponse response) { + try { + resetResponse(filename, response); + ClassPathResource templateResource = new ClassPathResource(templatePath); + ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()) + .withTemplate(templateResource.getStream()) + .autoCloseStream(false) + // 澶ф暟鍊艰嚜鍔ㄨ浆鎹� 闃叉澶辩湡 + .registerConverter(new ExcelBigNumberConvert()) + .build(); + WriteSheet writeSheet = EasyExcel.writerSheet().build(); + if (CollUtil.isEmpty(data)) { + throw new IllegalArgumentException("鏁版嵁涓虹┖"); + } + for (Map.Entry<String, Object> map : data.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(); + } catch (IOException e) { + throw new RuntimeException("瀵煎嚭Excel寮傚父"); + } + } + + /** + * 閲嶇疆鍝嶅簲浣� + */ + private static void resetResponse(String sheetName, HttpServletResponse response) throws UnsupportedEncodingException { + String filename = encodingFilename(sheetName); + response.reset(); + FileUtils.setAttachmentResponseHeader(response, filename); + response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8"); } /** @@ -100,7 +212,7 @@ String[] convertSource = converterExp.split(","); for (String item : convertSource) { String[] itemArray = item.split("="); - if (StringUtils.containsAny(separator, propertyValue)) { + if (StringUtils.containsAny(propertyValue, separator)) { for (String value : propertyValue.split(separator)) { if (itemArray[0].equals(value)) { propertyString.append(itemArray[1] + separator); @@ -129,7 +241,7 @@ String[] convertSource = converterExp.split(","); for (String item : convertSource) { String[] itemArray = item.split("="); - if (StringUtils.containsAny(separator, propertyValue)) { + if (StringUtils.containsAny(propertyValue, separator)) { for (String value : propertyValue.split(separator)) { if (itemArray[1].equals(value)) { propertyString.append(itemArray[0] + separator); -- Gitblit v1.9.3