From 504cd6717902e0530256635dd0d672f32a3beb92 Mon Sep 17 00:00:00 2001 From: "1437892690@qq.com" <1437892690@qq.com> Date: Fri, 31 Oct 2025 21:28:58 +0800 Subject: [PATCH 1/2] =?UTF-8?q?[=E5=8A=9F=E8=83=BD]=20ExcelUtil=E5=B7=B2?= =?UTF-8?q?=E7=BB=8F=E6=A0=87=E8=AE=B0@Deprecated=EF=BC=8C=E9=9C=80?= =?UTF-8?q?=E8=A6=81=E5=88=A0=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 关联 #[1445009117642752]ExcelUtil已经标记@Deprecated,需要删除 http://192.168.0.96:8090/demo/rdm.html#/story-detail/939050947543040/939050947543042/1445009117642752 --- .../handler/CustomDataSourceHandler.java | 53 +++++++++++++++++-- .../handler/ExternalDataSourceHandler.java | 28 ++++++++-- 2 files changed, 71 insertions(+), 10 deletions(-) diff --git a/src/main/java/neatlogic/module/framework/matrix/handler/CustomDataSourceHandler.java b/src/main/java/neatlogic/module/framework/matrix/handler/CustomDataSourceHandler.java index 60ac9e10c..445963e2c 100644 --- a/src/main/java/neatlogic/module/framework/matrix/handler/CustomDataSourceHandler.java +++ b/src/main/java/neatlogic/module/framework/matrix/handler/CustomDataSourceHandler.java @@ -35,12 +35,17 @@ import neatlogic.framework.util.ExcelUtil; import neatlogic.framework.util.TableResultUtil; import neatlogic.framework.util.TimeUtil; import neatlogic.framework.util.UuidUtil; +import neatlogic.framework.util.excel.ExcelBuilder; import neatlogic.framework.util.excel.ExcelPagedRowIterator; +import neatlogic.framework.util.excel.SheetBuilder; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.MapUtils; import org.apache.commons.lang3.StringUtils; import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.ss.usermodel.*; +import org.apache.poi.ss.util.CellRangeAddressList; +import org.apache.poi.xssf.streaming.SXSSFWorkbook; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; @@ -276,7 +281,7 @@ public class CustomDataSourceHandler extends MatrixDataSourceHandlerBase { @Override protected Workbook myExportMatrix2Excel(MatrixVo matrixVo) { - HSSFWorkbook workbook = null; + Workbook workbook = null; List attributeVoList = attributeMapper.getMatrixAttributeByMatrixUuid(matrixVo.getUuid()); if (CollectionUtils.isNotEmpty(attributeVoList)) { List headerList = new ArrayList<>(); @@ -292,6 +297,46 @@ public class CustomDataSourceHandler extends MatrixDataSourceHandlerBase { decodeDataConfig(attributeVo, selectValueList); columnSelectValueList.add(selectValueList); } + ExcelBuilder builder = new ExcelBuilder(SXSSFWorkbook.class); + SheetBuilder sheetBuilder = builder.withBorderColor(HSSFColor.HSSFColorPredefined.GREY_40_PERCENT) + .withHeadFontColor(HSSFColor.HSSFColorPredefined.WHITE) + .withHeadBgColor(HSSFColor.HSSFColorPredefined.DARK_BLUE) + .withColumnWidth(30) + .addSheet("sheet01") + .withHeaderList(headerList) + .withColumnList(columnList); + workbook = builder.build(); + Sheet sheet = workbook.getSheet("sheet01"); + if (CollectionUtils.isNotEmpty(columnSelectValueList)) { + for (int i = 0; i < columnSelectValueList.size(); i++) { + List defaultValueList = columnSelectValueList.get(i); + //行添加下拉框 + if (CollectionUtils.isNotEmpty(defaultValueList)) { + // 1. 创建下拉值数组 + String[] values = new String[defaultValueList.size()]; + defaultValueList.toArray(values); + // 2. 设置下拉框作用范围(注意SXSSF的行限制) + CellRangeAddressList regions = new CellRangeAddressList( + 1, // 首行(从第2行开始) + SXSSFWorkbook.DEFAULT_WINDOW_SIZE - 1, // 末行(最大行数-1) + i, // 列号 + i // 同一列 + ); + // 3. 创建约束(SXSSF需用XSSFDataValidationHelper) + DataValidationHelper dvHelper = sheet.getDataValidationHelper(); + DataValidationConstraint constraint = dvHelper.createExplicitListConstraint(values); + + // 4. 创建并应用数据验证 + DataValidation validation = dvHelper.createValidation(constraint, regions); + + // 5. 设置Excel的兼容性选项 + validation.setSuppressDropDownArrow(true); // 是否显示下拉箭头 + validation.setShowErrorBox(true); // 输入错误时显示提示 + //将有效性验证添加到表单 + sheet.addValidationData(validation); + } + } + } MatrixDataVo dataVo = new MatrixDataVo(); dataVo.setMatrixUuid(matrixVo.getUuid()); dataVo.setColumnList(columnList); @@ -303,12 +348,11 @@ public class CustomDataSourceHandler extends MatrixDataSourceHandlerBase { while (currentPage <= pageCount) { dataVo.setCurrentPage(currentPage); dataVo.setStartNum(null); - List> dataMapList = new ArrayList<>(); List> dataList = matrixDataMapper.searchDynamicTableData(dataVo); /* 转换用户、分组、角色字段值为用户名、分组名、角色名 **/ if (CollectionUtils.isNotEmpty(dataList)) { for (Map dataMap : dataList) { - Map map = new HashMap<>(); + Map map = new HashMap<>(); map.put("uuid", dataMap.get("uuid").toString()); map.put("sort", dataMap.get("sort").toString()); for (MatrixAttributeVo attributeVo : attributeVoList) { @@ -322,10 +366,9 @@ public class CustomDataSourceHandler extends MatrixDataSourceHandlerBase { } } } - dataMapList.add(map); + sheetBuilder.addData(map); } } - workbook = ExcelUtil.createExcel(workbook, headerList, columnList, columnSelectValueList, dataMapList); currentPage++; } } diff --git a/src/main/java/neatlogic/module/framework/matrix/handler/ExternalDataSourceHandler.java b/src/main/java/neatlogic/module/framework/matrix/handler/ExternalDataSourceHandler.java index 08bd2f6df..2c646a7d5 100644 --- a/src/main/java/neatlogic/module/framework/matrix/handler/ExternalDataSourceHandler.java +++ b/src/main/java/neatlogic/module/framework/matrix/handler/ExternalDataSourceHandler.java @@ -36,14 +36,16 @@ import neatlogic.framework.matrix.core.MatrixDataSourceHandlerBase; import neatlogic.framework.matrix.dto.*; import neatlogic.framework.matrix.exception.MatrixExternalAccessException; import neatlogic.framework.matrix.exception.MatrixExternalNotFoundException; -import neatlogic.framework.util.ExcelUtil; +import neatlogic.framework.util.excel.ExcelBuilder; +import neatlogic.framework.util.excel.SheetBuilder; import neatlogic.framework.util.javascript.JavascriptUtil; import neatlogic.module.framework.integration.handler.FrameworkRequestFrom; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.MapUtils; import org.apache.commons.lang3.StringUtils; -import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.xssf.streaming.SXSSFWorkbook; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; @@ -114,7 +116,7 @@ public class ExternalDataSourceHandler extends MatrixDataSourceHandlerBase { @Override protected Workbook myExportMatrix2Excel(MatrixVo matrixVo) { - HSSFWorkbook workbook = null; + Workbook workbook = null; MatrixExternalVo externalVo = matrixMapper.getMatrixExternalByMatrixUuid(matrixVo.getUuid()); if (externalVo == null) { throw new MatrixExternalNotFoundException(matrixVo.getName()); @@ -142,8 +144,24 @@ public class ExternalDataSourceHandler extends MatrixDataSourceHandlerBase { columnList.add(obj.getString("key")); } } - List> dataMapList = (List>) transformedResult.get("tbodyList"); - workbook = ExcelUtil.createExcel(workbook, headerList, columnList, null, dataMapList); +// List> dataMapList = (List>) transformedResult.get("tbodyList"); +// workbook = ExcelUtil.createExcel(workbook, headerList, columnList, null, dataMapList); + ExcelBuilder builder = new ExcelBuilder(SXSSFWorkbook.class); + SheetBuilder sheetBuilder = builder.withBorderColor(HSSFColor.HSSFColorPredefined.GREY_40_PERCENT) + .withHeadFontColor(HSSFColor.HSSFColorPredefined.WHITE) + .withHeadBgColor(HSSFColor.HSSFColorPredefined.DARK_BLUE) + .withColumnWidth(30) + .addSheet("sheet01") + .withHeaderList(headerList) + .withColumnList(columnList); + workbook = builder.build(); + JSONArray tbodyList = transformedResult.getJSONArray("tbodyList"); + if (CollectionUtils.isNotEmpty(tbodyList)) { + for (int i = 0; i < tbodyList.size(); i++) { + JSONObject tbody = tbodyList.getJSONObject(i); + sheetBuilder.addData(tbody); + } + } } } return workbook; -- Gitee From 9cb841a3e22780679269eb9a0eca5e2da48b1d04 Mon Sep 17 00:00:00 2001 From: "1437892690@qq.com" <1437892690@qq.com> Date: Sat, 1 Nov 2025 11:31:31 +0800 Subject: [PATCH 2/2] =?UTF-8?q?[=E5=8A=9F=E8=83=BD]=20ExcelUtil=E5=B7=B2?= =?UTF-8?q?=E7=BB=8F=E6=A0=87=E8=AE=B0@Deprecated=EF=BC=8C=E9=9C=80?= =?UTF-8?q?=E8=A6=81=E5=88=A0=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 关联 #[1445009117642752]ExcelUtil已经标记@Deprecated,需要删除 http://192.168.0.96:8090/demo/rdm.html#/story-detail/939050947543040/939050947543042/1445009117642752 --- .../neatlogic/framework/util/ExcelUtil.java | 462 ------------------ .../handler/CustomDataSourceHandler.java | 2 - 2 files changed, 464 deletions(-) delete mode 100644 src/main/java/neatlogic/framework/util/ExcelUtil.java diff --git a/src/main/java/neatlogic/framework/util/ExcelUtil.java b/src/main/java/neatlogic/framework/util/ExcelUtil.java deleted file mode 100644 index 5d35a9b61..000000000 --- a/src/main/java/neatlogic/framework/util/ExcelUtil.java +++ /dev/null @@ -1,462 +0,0 @@ -/*Copyright (C) 2024 深圳极向量科技有限公司 All Rights Reserved. - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU Affero General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU Affero General Public License for more details. - -You should have received a copy of the GNU Affero General Public License -along with this program. If not, see .*/ - -package neatlogic.framework.util; - -import org.apache.commons.collections4.CollectionUtils; -import org.apache.poi.hssf.usermodel.*; -import org.apache.poi.hssf.util.HSSFColor.HSSFColorPredefined; -import org.apache.poi.ss.SpreadsheetVersion; -import org.apache.poi.ss.usermodel.*; -import org.apache.poi.ss.util.CellRangeAddressList; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.IOException; -import java.io.OutputStream; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * @program: neatlogic - * @description: - * @create: 2020-03-31 17:00 - **/ -@Deprecated -public class ExcelUtil { - - static Logger logger = LoggerFactory.getLogger(ExcelUtil.class); - - /** - * @Description: excel 导出 - * @Param: [headerList, columnList, columnSelectValueList, dataMapList, os] - * @return: void - */ - public static void exportExcel(List headerList, List columnList, List> columnSelectValueList, List> dataMapList, OutputStream os) { - @SuppressWarnings("resource") - HSSFWorkbook workbook = new HSSFWorkbook(); - workbook.createSheet("sheet01"); - - // 声明一个工作薄 生成一个表格 - HSSFSheet sheet = workbook.getSheet("sheet01"); - // 设置表格默认列宽度为15个字节 - sheet.setDefaultColumnWidth((short) 15); - // 生成一个样式 - HSSFCellStyle style = workbook.createCellStyle(); - // 设置这些样式 - style.setFillForegroundColor(HSSFColorPredefined.SKY_BLUE.getIndex()); - style.setFillPattern(FillPatternType.SOLID_FOREGROUND); - style.setBorderBottom(BorderStyle.THIN); - style.setBorderLeft(BorderStyle.THIN); - style.setBorderRight(BorderStyle.THIN); - style.setBorderTop(BorderStyle.THIN); - style.setAlignment(HorizontalAlignment.CENTER); - // 生成一个字体 - HSSFFont font = workbook.createFont(); - font.setColor(HSSFColorPredefined.VIOLET.getIndex()); - font.setFontHeightInPoints((short) 12); - font.setBold(true); - // 把字体应用到当前的样式 - style.setFont(font); - // 生成并设置另一个样式 - HSSFCellStyle style2 = workbook.createCellStyle(); - style2.setFillForegroundColor(HSSFColorPredefined.LIGHT_YELLOW.getIndex()); - style2.setFillPattern(FillPatternType.SOLID_FOREGROUND); - style2.setBorderBottom(BorderStyle.THIN); - style2.setBorderLeft(BorderStyle.THIN); - style2.setBorderRight(BorderStyle.THIN); - style2.setBorderTop(BorderStyle.THIN); - style2.setAlignment(HorizontalAlignment.CENTER); - style2.setVerticalAlignment(VerticalAlignment.CENTER); - // 生成另一个字体 - HSSFFont font2 = workbook.createFont(); - font2.setBold(true); - // 把字体应用到当前的样式 - style2.setFont(font2); - - HSSFFont font3 = workbook.createFont(); - font3.setColor(HSSFColorPredefined.BLUE.getIndex()); - -// // 声明一个画图的顶级管理器 -// HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); -// // 定义注释的大小和位置,详见文档 -// HSSFComment comment = patriarch.createComment(new HSSFClientAnchor(0, 0, 0, 0, (short) 4, 2, (short) 6, 5)); -// // 设置注释内容 -// comment.setString(new HSSFRichTextString("可以在POI中添加注释!")); -// // 设置注释作者,当鼠标移动到单元格上是可以在状态栏中看到该内容. -// comment.setAuthor("leno"); - - // 产生表格标题行 - HSSFRow row = sheet.createRow(0); - int i = 0; - if (CollectionUtils.isNotEmpty(headerList)) { - for (String header : headerList) { - HSSFCell cell = row.createCell(i); - cell.setAsActiveCell(); - cell.setCellStyle(style); - HSSFRichTextString text = new HSSFRichTextString(header); - cell.setCellValue(text); - if (CollectionUtils.isNotEmpty(columnSelectValueList)) { - List defaultValueList = columnSelectValueList.get(i); - //行添加下拉框 - if (CollectionUtils.isNotEmpty(defaultValueList)) { - String[] values = new String[defaultValueList.size()]; - defaultValueList.toArray(values); - CellRangeAddressList region = new CellRangeAddressList(); - region.addCellRangeAddress(1, i, SpreadsheetVersion.EXCEL97.getLastRowIndex(), i); - DVConstraint constraint = DVConstraint.createExplicitListConstraint(values); - HSSFDataValidation data_validation_list = new HSSFDataValidation(region, constraint); - //将有效性验证添加到表单 - sheet.addValidationData(data_validation_list); - } - } - i++; - } - } - - - if (CollectionUtils.isNotEmpty(dataMapList) && CollectionUtils.isNotEmpty(columnList)) { - int index = 0; - for (Map dataMap : dataMapList) { - index++; - row = sheet.createRow(index); - int j = 0; - for (String column : columnList) { - HSSFCell cell = row.createCell(j); - cell.setCellStyle(style2); - HSSFRichTextString richString = new HSSFRichTextString(dataMap.get(column)); - richString.applyFont(font3); - cell.setCellValue(richString); - j++; - } - } - } - try { - workbook.write(os); - } catch (IOException e) { - e.printStackTrace(); - } finally { - if (os != null) { - try { - os.flush(); - os.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - } - } - - /** - * @Description: 表头导出 - * @Param: [headerList, columnSelectValueList, os] - * @return: void - */ - public static void exportExcelHeaders(List headerList, List> columnSelectValueList, OutputStream os) { - exportExcel(headerList, null, columnSelectValueList, null, os); - } - - /** - * @Description: 不包含下拉框默认值导出 - * @Param: [headerList, columnList, dataMapList, os] - * @return: void - */ - public static void exportExcel(List headerList, List columnList, List> dataMapList, OutputStream os) { - exportExcel(headerList, columnList, null, dataMapList, os); - } - - - public static HSSFWorkbook createExcel(HSSFWorkbook workbook, List headerList, List columnList, List> columnSelectValueList, List> dataMapList) { - HSSFSheet sheet = null; - if (workbook == null) { - workbook = new HSSFWorkbook(); - workbook.createSheet("sheet01"); - - // 声明一个工作薄 生成一个表格 - sheet = workbook.getSheet("sheet01"); - // 设置表格默认列宽度为15个字节 - sheet.setDefaultColumnWidth((short) 15); - // 生成一个样式 - HSSFCellStyle style = workbook.createCellStyle(); - // 设置这些样式 - style.setFillForegroundColor(HSSFColorPredefined.SKY_BLUE.getIndex()); - style.setFillPattern(FillPatternType.SOLID_FOREGROUND); - style.setBorderBottom(BorderStyle.THIN); - style.setBorderLeft(BorderStyle.THIN); - style.setBorderRight(BorderStyle.THIN); - style.setBorderTop(BorderStyle.THIN); - style.setAlignment(HorizontalAlignment.CENTER); - // 生成一个字体 - HSSFFont font = workbook.createFont(); - font.setColor(HSSFColorPredefined.VIOLET.getIndex()); - font.setFontHeightInPoints((short) 12); - font.setBold(true); - // 把字体应用到当前的样式 - style.setFont(font); - - - // 产生表格标题行 - HSSFRow row = sheet.createRow(0); - if (CollectionUtils.isNotEmpty(headerList)) { - int i = 0; - for (String header : headerList) { - HSSFCell cell = row.createCell(i); - cell.setAsActiveCell(); - cell.setCellStyle(style); - HSSFRichTextString text = new HSSFRichTextString(header); - cell.setCellValue(text); - if (CollectionUtils.isNotEmpty(columnSelectValueList)) { - List defaultValueList = columnSelectValueList.get(i); - //行添加下拉框 - if (CollectionUtils.isNotEmpty(defaultValueList)) { - String[] values = new String[defaultValueList.size()]; - defaultValueList.toArray(values); - CellRangeAddressList region = new CellRangeAddressList(); - region.addCellRangeAddress(1, i, SpreadsheetVersion.EXCEL97.getLastRowIndex(), i); - DVConstraint constraint = DVConstraint.createExplicitListConstraint(values); - HSSFDataValidation data_validation_list = new HSSFDataValidation(region, constraint); - //将有效性验证添加到表单 - sheet.addValidationData(data_validation_list); - } - } - i++; - } - } - } else { - //sheet = workbook.getSheetAt(0); - sheet = workbook.getSheet("sheet01"); - } - - if (CollectionUtils.isNotEmpty(dataMapList) && CollectionUtils.isNotEmpty(columnList)) { - // 生成并设置另一个样式 - HSSFCellStyle style2 = workbook.createCellStyle(); - style2.setFillForegroundColor(HSSFColorPredefined.LIGHT_YELLOW.getIndex()); - style2.setFillPattern(FillPatternType.SOLID_FOREGROUND); - style2.setBorderBottom(BorderStyle.THIN); - style2.setBorderLeft(BorderStyle.THIN); - style2.setBorderRight(BorderStyle.THIN); - style2.setBorderTop(BorderStyle.THIN); - style2.setAlignment(HorizontalAlignment.CENTER); - style2.setVerticalAlignment(VerticalAlignment.CENTER); - // 生成另一个字体 - HSSFFont font2 = workbook.createFont(); - font2.setBold(true); - // 把字体应用到当前的样式 - style2.setFont(font2); - HSSFFont font3 = workbook.createFont(); - font3.setColor(HSSFColorPredefined.BLUE.getIndex()); - - int lastRowNum = sheet.getLastRowNum(); - for (Map dataMap : dataMapList) { - lastRowNum++; - HSSFRow row = sheet.createRow(lastRowNum); - int j = 0; - for (String column : columnList) { - HSSFCell cell = row.createCell(j); - cell.setCellStyle(style2); - HSSFRichTextString richString = new HSSFRichTextString(dataMap.get(column)); - richString.applyFont(font3); - cell.setCellValue(richString); - j++; - } - } - } - - return workbook; - } - - /** - * 此方法可传入poi-3.8的SXSSFWorkbook来创建excel - * 3.8以前的版本,单元格字符数有限 - * - * @param workbook 工作簿 - * @param headerList 头部 - * @param columnList 列 - * @param dataMapList 数据 - * @param columnWidth 列宽 - * @param num 序号 - * @return 工作簿 - */ - public static Workbook exportData(Workbook workbook, List headerList, List columnList, List> dataMapList, Integer columnWidth, int num) { - // 生成一个表格 - Sheet sheet = workbook.createSheet(); - // 设置sheet名字 - workbook.setSheetName(num, "sheet-" + num); - Map cellStyle = getRowCellStyle(workbook); - CellStyle firstRowCellStyle = cellStyle.get("firstRowCellStyle"); - CellStyle rowCellStyle = cellStyle.get("rowCellStyle"); - - //生成标题行与正文行 - createRows(headerList, columnList, dataMapList, columnWidth, sheet, firstRowCellStyle, rowCellStyle, 0); - return workbook; - } - - /** - * @param workbook 工作簿 - * @param sheet 工作区 - * @param headerList 头部 - * @param columnList 列 - * @param dataMapList 数据 - * @return 工作簿 - */ - public static Workbook exportData(Workbook workbook, Sheet sheet, List headerList, List columnList, List> dataMapList) { - Map cellStyle = getRowCellStyle(workbook); - CellStyle firstRowCellStyle = cellStyle.get("firstRowCellStyle"); - CellStyle rowCellStyle = cellStyle.get("rowCellStyle"); - createRows(headerList, columnList, dataMapList, null, sheet, firstRowCellStyle, rowCellStyle, 0); - return workbook; - } - - - /** - * 导出工单上报模版方法 - * - * @param workbook - * @param headerList - * @param columnList - * @param dataMapList - * @param channelData - * @param columnWidth - * @return - * @throws Exception - */ - public static Workbook exportProcessTaskTemplate(Workbook workbook, List headerList, List columnList, List> dataMapList, List channelData, Integer columnWidth) throws Exception { - // 生成一个表格 - Sheet sheet = workbook.createSheet(); - // 设置sheet名字 - workbook.setSheetName(0, "sheet"); - Map cellStyle = getRowCellStyle(workbook); - CellStyle firstRowcellStyle = cellStyle.get("firstRowcellStyle"); - CellStyle rowcellStyle = cellStyle.get("rowcellStyle"); - - /* 生成服务信息行 */ - Row channelRow = sheet.createRow(0); - for (int i = 0; i < channelData.size(); i++) { - Cell cell = channelRow.createCell(i); - cell.setCellValue(channelData.get(i)); - } - - createRows(headerList, columnList, dataMapList, columnWidth, sheet, firstRowcellStyle, rowcellStyle, 1); - return workbook; - } - - /** - * 生成标题行与正文行 - * - * @param headerList - * @param columnList - * @param dataMapList - * @param columnWidth - * @param sheet - * @param firstRowcellStyle - * @param rowcellStyle - * @param headRowNum 标题行开始行数 - */ - public static void createRows(List headerList, List columnList, List> dataMapList, Integer columnWidth, Sheet sheet, CellStyle firstRowCellStyle, CellStyle rowCellStyle, int headRowNum) { - //生成标题行 - Row headerRow = sheet.createRow(headRowNum); - if (CollectionUtils.isNotEmpty(headerList)) { - int i = 0; - for (String header : headerList) { - //设置列宽 - if (columnWidth != null && columnWidth > 0) { - sheet.setColumnWidth(i, columnWidth * 256); - } - Cell cell = headerRow.createCell(i); - cell.setCellStyle(firstRowCellStyle); - cell.setCellValue(header); - i++; - } - } - //生成数据行 - if (CollectionUtils.isNotEmpty(columnList) && CollectionUtils.isNotEmpty(dataMapList)) { - int lastRowNum = sheet.getLastRowNum(); - for (Map dataMap : dataMapList) { - lastRowNum++; - Row row = sheet.createRow(lastRowNum); - int j = 0; - for (String column : columnList) { - Cell cell = row.createCell(j); - cell.setCellStyle(rowCellStyle); - cell.setCellValue(dataMap.get(column) == null ? null : dataMap.get(column).toString()); - j++; - } - } - } - } - - public static String getCellContent(Cell cell) { - String cellContent = ""; - switch (cell.getCellType()) { - case NUMERIC: - cellContent = (int) cell.getNumericCellValue() + ""; - break; - case STRING: - cellContent = cell.getStringCellValue() + ""; - break; - case BOOLEAN: - cellContent = cell.getBooleanCellValue() + ""; - break; - case BLANK: - cellContent = "blank"; - break; - case FORMULA: - cellContent = cell.getCellFormula() + ""; - break; - case ERROR: - cellContent = "error"; - break; - default: - break; - } - return cellContent; - } - - public static Map getRowCellStyle(Workbook workbook) { - // 设置标题行样式 - CellStyle firstRowCellStyle = workbook.createCellStyle(); - firstRowCellStyle.setFillForegroundColor(HSSFColorPredefined.SKY_BLUE.getIndex());// 设置背景色 - firstRowCellStyle.setVerticalAlignment(VerticalAlignment.TOP);// 上下居中 - firstRowCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); - firstRowCellStyle.setAlignment(HorizontalAlignment.CENTER); - firstRowCellStyle.setBorderBottom(BorderStyle.THIN); - firstRowCellStyle.setBorderLeft(BorderStyle.THIN); - firstRowCellStyle.setBorderRight(BorderStyle.THIN); - firstRowCellStyle.setBorderTop(BorderStyle.THIN); - Font font = workbook.createFont(); - font.setFontName("宋体"); - font.setFontHeight((short) 220); - font.setBold(true); - firstRowCellStyle.setFont(font); - //设置正文行样式 - CellStyle rowCellStyle = workbook.createCellStyle(); - rowCellStyle.setFillForegroundColor(HSSFColorPredefined.LIGHT_YELLOW.getIndex());// 设置背景色 - rowCellStyle.setVerticalAlignment(VerticalAlignment.TOP);// 上下居中 - rowCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); - rowCellStyle.setAlignment(HorizontalAlignment.CENTER); - rowCellStyle.setBorderBottom(BorderStyle.THIN); - rowCellStyle.setBorderLeft(BorderStyle.THIN); - rowCellStyle.setBorderRight(BorderStyle.THIN); - rowCellStyle.setBorderTop(BorderStyle.THIN); - rowCellStyle.setFont(font); - Map cellStyleMap = new HashMap<>(2); - cellStyleMap.put("firstRowCellStyle", firstRowCellStyle); - cellStyleMap.put("rowCellStyle", rowCellStyle); - return cellStyleMap; - } - - -} diff --git a/src/main/java/neatlogic/module/framework/matrix/handler/CustomDataSourceHandler.java b/src/main/java/neatlogic/module/framework/matrix/handler/CustomDataSourceHandler.java index 445963e2c..4f1f3cf6d 100644 --- a/src/main/java/neatlogic/module/framework/matrix/handler/CustomDataSourceHandler.java +++ b/src/main/java/neatlogic/module/framework/matrix/handler/CustomDataSourceHandler.java @@ -31,7 +31,6 @@ import neatlogic.framework.matrix.dao.mapper.MatrixAttributeMapper; import neatlogic.framework.matrix.dao.mapper.MatrixDataMapper; import neatlogic.framework.matrix.dto.*; import neatlogic.framework.matrix.exception.*; -import neatlogic.framework.util.ExcelUtil; import neatlogic.framework.util.TableResultUtil; import neatlogic.framework.util.TimeUtil; import neatlogic.framework.util.UuidUtil; @@ -41,7 +40,6 @@ import neatlogic.framework.util.excel.SheetBuilder; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.MapUtils; import org.apache.commons.lang3.StringUtils; -import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.util.CellRangeAddressList; -- Gitee