From 4a9aafe8530e945bd52d8093944c4bcb1a89c28f Mon Sep 17 00:00:00 2001 From: 疯狂的狮子Li <15040126243@163.com> Date: 星期日, 25 六月 2023 11:13:05 +0800 Subject: [PATCH] add 增加 excel 导出下拉框功能 --- ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/utils/ExcelUtil.java | 83 ++++++++++++++++++++++++++++++++++------- 1 files changed, 69 insertions(+), 14 deletions(-) diff --git a/ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/utils/ExcelUtil.java b/ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/utils/ExcelUtil.java index 3605cf2..00a78ea 100644 --- a/ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/utils/ExcelUtil.java +++ b/ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/utils/ExcelUtil.java @@ -10,21 +10,19 @@ 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 org.dromara.common.core.utils.StringUtils; -import org.dromara.common.core.utils.file.FileUtils; -import org.dromara.common.excel.convert.ExcelBigNumberConvert; -import org.dromara.common.excel.core.CellMergeStrategy; -import org.dromara.common.excel.core.DefaultExcelListener; -import org.dromara.common.excel.core.ExcelListener; -import org.dromara.common.excel.core.ExcelResult; import jakarta.servlet.ServletOutputStream; import jakarta.servlet.http.HttpServletResponse; import lombok.AccessLevel; import lombok.NoArgsConstructor; +import org.dromara.common.core.utils.StringUtils; +import org.dromara.common.core.utils.file.FileUtils; +import org.dromara.common.excel.convert.ExcelBigNumberConvert; +import org.dromara.common.excel.core.*; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.io.UnsupportedEncodingException; import java.util.Collection; import java.util.List; import java.util.Map; @@ -87,7 +85,26 @@ try { resetResponse(sheetName, response); ServletOutputStream os = response.getOutputStream(); - exportExcel(list, sheetName, clazz, false, os); + exportExcel(list, sheetName, clazz, false, os, null); + } catch (IOException e) { + throw new RuntimeException("瀵煎嚭Excel寮傚父"); + } + } + + /** + * 瀵煎嚭excel + * + * @param list 瀵煎嚭鏁版嵁闆嗗悎 + * @param sheetName 宸ヤ綔琛ㄧ殑鍚嶇О + * @param clazz 瀹炰綋绫� + * @param response 鍝嶅簲浣� + * @param options 绾ц仈涓嬫媺閫� + */ + public static <T> void exportExcel(List<T> list, String sheetName, Class<T> clazz, HttpServletResponse response, List<DropDownOptions> options) { + try { + resetResponse(sheetName, response); + ServletOutputStream os = response.getOutputStream(); + exportExcel(list, sheetName, clazz, false, os, options); } catch (IOException e) { throw new RuntimeException("瀵煎嚭Excel寮傚父"); } @@ -106,7 +123,27 @@ try { resetResponse(sheetName, response); ServletOutputStream os = response.getOutputStream(); - exportExcel(list, sheetName, clazz, merge, os); + exportExcel(list, sheetName, clazz, merge, os, null); + } catch (IOException e) { + throw new RuntimeException("瀵煎嚭Excel寮傚父"); + } + } + + /** + * 瀵煎嚭excel + * + * @param list 瀵煎嚭鏁版嵁闆嗗悎 + * @param sheetName 宸ヤ綔琛ㄧ殑鍚嶇О + * @param clazz 瀹炰綋绫� + * @param merge 鏄惁鍚堝苟鍗曞厓鏍� + * @param response 鍝嶅簲浣� + * @param options 绾ц仈涓嬫媺閫� + */ + public static <T> void exportExcel(List<T> list, String sheetName, Class<T> clazz, boolean merge, HttpServletResponse response, List<DropDownOptions> options) { + try { + resetResponse(sheetName, response); + ServletOutputStream os = response.getOutputStream(); + exportExcel(list, sheetName, clazz, merge, os, options); } catch (IOException e) { throw new RuntimeException("瀵煎嚭Excel寮傚父"); } @@ -121,7 +158,20 @@ * @param os 杈撳嚭娴� */ public static <T> void exportExcel(List<T> list, String sheetName, Class<T> clazz, OutputStream os) { - exportExcel(list, sheetName, clazz, false, os); + exportExcel(list, sheetName, clazz, false, os, null); + } + + /** + * 瀵煎嚭excel + * + * @param list 瀵煎嚭鏁版嵁闆嗗悎 + * @param sheetName 宸ヤ綔琛ㄧ殑鍚嶇О + * @param clazz 瀹炰綋绫� + * @param os 杈撳嚭娴� + * @param options 绾ц仈涓嬫媺閫夊唴瀹� + */ + public static <T> void exportExcel(List<T> list, String sheetName, Class<T> clazz, OutputStream os, List<DropDownOptions> options) { + exportExcel(list, sheetName, clazz, false, os, options); } /** @@ -133,7 +183,8 @@ * @param merge 鏄惁鍚堝苟鍗曞厓鏍� * @param os 杈撳嚭娴� */ - public static <T> void exportExcel(List<T> list, String sheetName, Class<T> clazz, boolean merge, OutputStream os) { + public static <T> void exportExcel(List<T> list, String sheetName, Class<T> clazz, boolean merge, + OutputStream os, List<DropDownOptions> options) { ExcelWriterSheetBuilder builder = EasyExcel.write(os, clazz) .autoCloseStream(false) // 鑷姩閫傞厤 @@ -144,6 +195,10 @@ if (merge) { // 鍚堝苟澶勭悊鍣� builder.registerWriteHandler(new CellMergeStrategy(list, true)); + } + if (CollUtil.isNotEmpty(options)) { + // 娣诲姞涓嬫媺妗嗘搷浣� + builder.registerWriteHandler(new ExcelDownHandler(options)); } builder.doWrite(list); } @@ -253,7 +308,7 @@ /** * 閲嶇疆鍝嶅簲浣� */ - private static void resetResponse(String sheetName, HttpServletResponse response) { + private static void resetResponse(String sheetName, HttpServletResponse response) throws UnsupportedEncodingException { String filename = encodingFilename(sheetName); FileUtils.setAttachmentResponseHeader(response, filename); response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8"); @@ -275,7 +330,7 @@ if (StringUtils.containsAny(propertyValue, separator)) { for (String value : propertyValue.split(separator)) { if (itemArray[0].equals(value)) { - propertyString.append(itemArray[1]).append(separator); + propertyString.append(itemArray[1] + separator); break; } } @@ -304,7 +359,7 @@ if (StringUtils.containsAny(propertyValue, separator)) { for (String value : propertyValue.split(separator)) { if (itemArray[1].equals(value)) { - propertyString.append(itemArray[0]).append(separator); + propertyString.append(itemArray[0] + separator); break; } } -- Gitblit v1.9.3