From 31bf7cfd99294b6ccd161afbffb926e6d5f987c6 Mon Sep 17 00:00:00 2001 From: sy Date: Thu, 30 Mar 2023 17:26:55 +0800 Subject: [PATCH] =?UTF-8?q?=E8=96=AA=E9=85=AC=E7=B3=BB=E7=BB=9F-=E8=96=AA?= =?UTF-8?q?=E8=B5=84=E6=A0=B8=E7=AE=97=E5=92=8C=E6=A1=A3=E6=A1=88=EF=BC=8C?= =?UTF-8?q?excel=E5=AF=BC=E5=87=BA=E8=A1=8C=E9=AB=98=E8=87=AA=E9=80=82?= =?UTF-8?q?=E5=BA=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../salary/util/excel/ExcelUtilPlus.java | 156 +++++++++++++++++- 1 file changed, 155 insertions(+), 1 deletion(-) diff --git a/src/com/engine/salary/util/excel/ExcelUtilPlus.java b/src/com/engine/salary/util/excel/ExcelUtilPlus.java index 939e6e78c..0fb1cdab8 100644 --- a/src/com/engine/salary/util/excel/ExcelUtilPlus.java +++ b/src/com/engine/salary/util/excel/ExcelUtilPlus.java @@ -7,7 +7,6 @@ import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.xssf.usermodel.*; -import java.awt.*; import java.awt.Color; import java.util.Date; import java.util.List; @@ -116,6 +115,9 @@ public class ExcelUtilPlus { for (int rowIndex = 0; rowIndex < rowList.size(); rowIndex++) { List infoList = rowList.get(rowIndex); XSSFRow row = sheet.createRow(rowIndex); + float height = 18; + float finalHeight = 18; + for (int cellIndex = 0; cellIndex < infoList.size(); cellIndex++) { XSSFCell cell = row.createCell(cellIndex); if (rowIndex == 0) { @@ -137,7 +139,12 @@ public class ExcelUtilPlus { cell.setCellType(CellType.STRING); cell.setCellValue(o == null ? "" : o.toString()); } + + //判断是否要调整高度 + int width = sheet.getColumnWidth(cellIndex) / 256; + finalHeight = getFinalHeight(o, width, finalHeight, height); } + row.setHeightInPoints(finalHeight); } return workbook; } @@ -192,6 +199,9 @@ public class ExcelUtilPlus { for (int rowIndex = 0; rowIndex < rowList.size(); rowIndex++) { List infoList = rowList.get(rowIndex); XSSFRow row = sheet.createRow(rowIndex); + float height = 18; + float finalHeight = 18; + for (int cellIndex = 0; cellIndex < infoList.size(); cellIndex++) { XSSFCell cell = row.createCell(cellIndex); if (rowIndex == 0) { @@ -213,7 +223,12 @@ public class ExcelUtilPlus { cell.setCellType(CellType.STRING); cell.setCellValue(String.valueOf(o)); } + + //判断是否要调整高度 + int width = sheet.getColumnWidth(cellIndex) / 256; + finalHeight = getFinalHeight(o, width, finalHeight, height); } + row.setHeightInPoints(finalHeight); } if (CollectionUtils.isNotEmpty(comments)) { @@ -226,6 +241,106 @@ public class ExcelUtilPlus { return workbook; } + public static XSSFWorkbook genWorkbookV2(List> rowList, String sheetName, boolean lastRowRed) { + XSSFWorkbook workbook = new XSSFWorkbook(); + + // 设置title样式 + XSSFCellStyle titleCellStyle = workbook.createCellStyle(); + XSSFFont titleFont = workbook.createFont(); + titleFont.setBold(true); + titleFont.setFontName("仿宋"); + titleFont.setFontHeightInPoints((short) 15); + titleCellStyle.setFont(titleFont); + titleCellStyle.setAlignment(HorizontalAlignment.CENTER); + titleCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());//背景色 + titleCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); + titleCellStyle.setVerticalAlignment(VerticalAlignment.CENTER); + titleCellStyle.setBorderLeft(BorderStyle.THIN); + titleCellStyle.setBorderRight(BorderStyle.THIN); + titleCellStyle.setBorderTop(BorderStyle.THIN); + titleCellStyle.setBorderBottom(BorderStyle.THIN); + + // 设置主体样式 + XSSFCellStyle cellStyle = workbook.createCellStyle(); + XSSFFont font = workbook.createFont(); + font.setFontName("宋体"); + font.setFontHeightInPoints((short) 10);// 设置字体大小 + cellStyle.setFont(font);// 选择需要用到的字体格式 + cellStyle.setWrapText(true); + + cellStyle.setBorderLeft(BorderStyle.THIN); + cellStyle.setBorderRight(BorderStyle.THIN); + cellStyle.setBorderTop(BorderStyle.THIN); + cellStyle.setBorderBottom(BorderStyle.THIN); + + XSSFCellStyle redCellStyle = workbook.createCellStyle(); + XSSFFont redFont = workbook.createFont(); + redFont.setFontName("宋体"); + redFont.setFontHeightInPoints((short) 10);// 设置字体大小 + redFont.setColor(new XSSFColor(new Color(0xFF3333), null)); + redFont.setBold(true); + redCellStyle.setWrapText(true); + redCellStyle.setFont(redFont);// 选择需要用到的字体格式 + + redCellStyle.setBorderLeft(BorderStyle.THIN); + redCellStyle.setBorderRight(BorderStyle.THIN); + redCellStyle.setBorderTop(BorderStyle.THIN); + redCellStyle.setBorderBottom(BorderStyle.THIN); + + XSSFSheet sheet = workbook.createSheet(sheetName); + //自适应宽度 + sheet.autoSizeColumn(0, true); + //默认列宽 + sheet.setDefaultColumnWidth(20); + //默认行高 + sheet.setDefaultRowHeightInPoints(18); + + //遍历设置列宽 + List header = rowList.get(0); + for (int i = 0; i < header.size(); i++) { + sheet.setColumnWidth(i,Math.max(12, header.get(i).toString().length()*4)*256); + } + + for (int rowIndex = 0; rowIndex < rowList.size(); rowIndex++) { + List infoList = rowList.get(rowIndex); + XSSFRow row = sheet.createRow(rowIndex); + float height = 18; + float finalHeight = 18; + + for (int cellIndex = 0; cellIndex < infoList.size(); cellIndex++) { + XSSFCell cell = row.createCell(cellIndex); + if (rowIndex == 0) { + cell.setCellStyle(titleCellStyle); + } else { + if (lastRowRed && rowIndex == rowList.size() - 1) { + cell.setCellStyle(redCellStyle); + } else { + cell.setCellStyle(cellStyle); + } + } + Object o = infoList.get(cellIndex); + if (o instanceof String) { + cell.setCellType(CellType.STRING); + cell.setCellValue(String.valueOf(o)); + } else if (o instanceof Boolean) { + cell.setCellType(CellType.BOOLEAN); + cell.setCellValue(String.valueOf(o)); + } else if (o instanceof Date) { + cell.setCellType(CellType.STRING); + cell.setCellValue(SalaryDateUtil.getFormatLocalDate((Date) o)); + } else { + cell.setCellType(CellType.STRING); + cell.setCellValue(o == null ? "" : o.toString()); + } + + //判断是否要调整高度 + int width = sheet.getColumnWidth(cellIndex) / 256; + finalHeight = getFinalHeight(o, width, finalHeight, height); + } + row.setHeightInPoints(finalHeight); + } + return workbook; + } public static XSSFWorkbook genWorkbookWithChildTitleColumn(List> rowList, String sheetName, boolean lastRowRed) { XSSFWorkbook workbook = new XSSFWorkbook(); @@ -345,6 +460,9 @@ public class ExcelUtilPlus { for (int rowIndex = 1; rowIndex < rowList.size(); rowIndex++) { List infoList = rowList.get(rowIndex); XSSFRow row = sheet.createRow(rowIndex + 1); + float height = 18; + float finalHeight = 18; + for (int cellIndex = 0; cellIndex < infoList.size(); cellIndex++) { XSSFCell cell = row.createCell(cellIndex); @@ -371,8 +489,44 @@ public class ExcelUtilPlus { cell.setCellType(CellType.STRING); cell.setCellValue(o == null ? "" : o.toString()); } + + //判断是否要调整高度 + int width = sheet.getColumnWidth(cellIndex) / 256; + finalHeight = getFinalHeight(o, width, finalHeight, height); } + row.setHeightInPoints(finalHeight); } return workbook; } + + public static float getFinalHeight(Object o, int width, float finalHeight, float height) { + if (o != null && getStrlength(o.toString()) > width) { + float remainder = getStrlength(o.toString()) % width; + int multiple = getStrlength(o.toString()) / width; + int finalMultiple = remainder > 0 ? (multiple + 1) : multiple; + float compareHeight = height * finalMultiple; + finalHeight = Math.max(finalHeight, compareHeight); + } + return finalHeight; + } + + public static int getStrlength(String str) { + int strLength = 0; + String chinese = "[\u0391-\uFFE5]"; + /* 获取字段值的长度,如果含中文字符,则每个中文字符长度为2,否则为1 */ + for (int i = 0; i < str.length(); i++) { + /* 从字符串中获取一个字符 */ + String temp = str.substring(i, i + 1); + /* 判断是否为中文字符 */ + if (temp.matches(chinese)) { + /* 中文字符长度为2 */ + strLength += 2; + } else { + /* 其他字符长度为1 */ + strLength += 1; + } + } + return strLength; + } + }