From 177249c76aeea0b4bf8d8816d4994e3b445b45ce Mon Sep 17 00:00:00 2001 From: huanghongfa <huanghongfa123456> Date: 星期四, 02 九月 2021 10:39:34 +0800 Subject: [PATCH] Merge remote-tracking branch 'origin/zzj' into zzj --- springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/ExcelUtil.java | 118 +++++++++++++++++++++++++++++++++++------------------------ 1 files changed, 70 insertions(+), 48 deletions(-) diff --git a/springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/ExcelUtil.java b/springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/ExcelUtil.java index 0c8e388..1045404 100644 --- a/springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/ExcelUtil.java +++ b/springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/ExcelUtil.java @@ -1,22 +1,24 @@ package com.panzhihua.common.utlis; -import cn.afterturn.easypoi.excel.ExcelExportUtil; -import cn.afterturn.easypoi.excel.ExcelImportUtil; -import cn.afterturn.easypoi.excel.entity.ExportParams; -import cn.afterturn.easypoi.excel.entity.ImportParams; -import cn.afterturn.easypoi.excel.entity.enmus.ExcelType; -import lombok.extern.slf4j.Slf4j; -import org.apache.commons.lang3.StringUtils; -import org.apache.poi.ss.usermodel.*; -import org.springframework.web.multipart.MultipartFile; - -import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.IOException; import java.net.URLEncoder; import java.util.Collections; import java.util.List; import java.util.Map; + +import javax.servlet.http.HttpServletResponse; + +import org.apache.commons.lang3.StringUtils; +import org.apache.poi.ss.usermodel.*; +import org.springframework.web.multipart.MultipartFile; + +import cn.afterturn.easypoi.excel.ExcelExportUtil; +import cn.afterturn.easypoi.excel.ExcelImportUtil; +import cn.afterturn.easypoi.excel.entity.ExportParams; +import cn.afterturn.easypoi.excel.entity.ImportParams; +import cn.afterturn.easypoi.excel.entity.enmus.ExcelType; +import lombok.extern.slf4j.Slf4j; /** * Excel工具类 @@ -33,59 +35,73 @@ /** * 导出 * - * @param list 数据列表 - * @param title 标题 - * @param sheetName sheet名称 - * @param pojoClass 元素类型 - * @param fileName 文件名 - * @param isCreateHeader 是否创建列头 + * @param list + * 数据列表 + * @param title + * 标题 + * @param sheetName + * sheet名称 + * @param pojoClass + * 元素类型 + * @param fileName + * 文件名 + * @param isCreateHeader + * 是否创建列头 * @param response * @throws IOException */ - public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName - , boolean isCreateHeader, HttpServletResponse response) throws IOException { + public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName, + boolean isCreateHeader, HttpServletResponse response) throws IOException { ExportParams exportParams = new ExportParams(title, sheetName); exportParams.setCreateHeadRows(isCreateHeader); defaultExport(list, pojoClass, fileName, response, exportParams); } - /** * 导出 * - * @param list 数据列表 - * @param title 标题 - * @param sheetName sheet名称 - * @param pojoClass 元素类型 - * @param fileName 文件名 + * @param list + * 数据列表 + * @param title + * 标题 + * @param sheetName + * sheet名称 + * @param pojoClass + * 元素类型 + * @param fileName + * 文件名 * @param response * @throws IOException */ - public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName - , HttpServletResponse response) throws IOException { + public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName, + HttpServletResponse response) throws IOException { defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName)); } /** * 导出excel 带下拉列表的 * - * @param list 数据列表 - * @param title 标题 - * @param sheetName sheet名称 - * @param pojoClass 元素类型 - * @param fileName 文件名 + * @param list + * 数据列表 + * @param title + * 标题 + * @param sheetName + * sheet名称 + * @param pojoClass + * 元素类型 + * @param fileName + * 文件名 * @param response * @throws IOException */ - public static void exportExcelForSelectList(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName - , List<ExcelSelectObject> selectListObject, HttpServletResponse response) throws IOException { + public static void exportExcelForSelectList(List<?> list, String title, String sheetName, Class<?> pojoClass, + String fileName, List<ExcelSelectObject> selectListObject, HttpServletResponse response) throws IOException { Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(title, sheetName), pojoClass, list); - //这里是自己加的 带下拉框的代码 - selectListObject.forEach(selectList -> - ExcelSelectListUtil.selectList(workbook, selectList.getFirstCol(), selectList.getLastCol(),selectList.getStrings()) - ); - for(int i=2;i<selectListObject.size();i++) { + // 这里是自己加的 带下拉框的代码 + selectListObject.forEach(selectList -> ExcelSelectListUtil.selectList(workbook, selectList.getFirstCol(), + selectList.getLastCol(), selectList.getStrings())); + for (int i = 2; i < selectListObject.size(); i++) { Sheet sheet = workbook.getSheetAt(0); Row row = sheet.createRow(i); CellStyle style = workbook.createCellStyle(); @@ -97,34 +113,40 @@ downLoadExcel(fileName, response, workbook); } } + /** * 导出 * - * @param list 数据列表(元素是Map) - * @param fileName 文件名 + * @param list + * 数据列表(元素是Map) + * @param fileName + * 文件名 * @param response * @throws IOException */ - public static void exportExcel(List<Map<String, Object>> list, String fileName, HttpServletResponse response) throws IOException { + public static void exportExcel(List<Map<String, Object>> list, String fileName, HttpServletResponse response) + throws IOException { defaultExport(list, fileName, response); } - private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName - , HttpServletResponse response, ExportParams exportParams) throws IOException { + private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response, + ExportParams exportParams) throws IOException { Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list); if (workbook != null) { downLoadExcel(fileName, response, workbook); } } - private static void defaultExport(List<Map<String, Object>> list, String fileName, HttpServletResponse response) throws IOException { + private static void defaultExport(List<Map<String, Object>> list, String fileName, HttpServletResponse response) + throws IOException { Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.XSSF); if (workbook != null) { downLoadExcel(fileName, response, workbook); } } - private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) throws IOException { + private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) + throws IOException { response.setCharacterEncoding("UTF-8"); response.setHeader("content-Type", "application/vnd.ms-excel"); response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8")); @@ -141,7 +163,8 @@ return ExcelImportUtil.importExcel(new File(filePath), pojoClass, params); } - public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass) throws Exception { + public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass) + throws Exception { if (file == null) { return Collections.emptyList(); } @@ -150,6 +173,5 @@ params.setHeadRows(headerRows); return ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params); } - } -- Gitblit v1.7.1