From f7abee8455d9e4f1a857bf620ae7d5ad1edb9e85 Mon Sep 17 00:00:00 2001 From: buaixuexideshitongxue <2936013465@qq.com> Date: Thu, 13 Aug 2026 16:52:12 +0800 Subject: [PATCH] =?UTF-8?q?fix=EF=BC=9A=20=E9=A2=9C=E8=89=B2=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...ModifiedVerifyContentCellWriteHandler.java | 39 +++++++++++++++++-- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/biutag/supervision/service/ModifiedVerifyContentCellWriteHandler.java b/src/main/java/com/biutag/supervision/service/ModifiedVerifyContentCellWriteHandler.java index 9d974e7..e99e97c 100644 --- a/src/main/java/com/biutag/supervision/service/ModifiedVerifyContentCellWriteHandler.java +++ b/src/main/java/com/biutag/supervision/service/ModifiedVerifyContentCellWriteHandler.java @@ -2,24 +2,37 @@ package com.biutag.supervision.service; import cn.hutool.core.util.StrUtil; import com.alibaba.excel.write.handler.CellWriteHandler; +import com.alibaba.excel.write.handler.RowWriteHandler; import com.alibaba.excel.write.handler.context.CellWriteHandlerContext; +import com.alibaba.excel.write.handler.context.RowWriteHandlerContext; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Font; +import org.apache.poi.ss.usermodel.HorizontalAlignment; import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.RichTextString; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.VerticalAlignment; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFColor; import org.apache.poi.xssf.usermodel.XSSFFont; -public class ModifiedVerifyContentCellWriteHandler implements CellWriteHandler { +public class ModifiedVerifyContentCellWriteHandler implements CellWriteHandler, RowWriteHandler { private static final int MODIFIED_VERIFY_CONTENT_COLUMN_INDEX = 6; + private static final int FINAL_WRITE_ORDER = 60000; + private static final int ESTIMATED_CHARACTERS_PER_LINE = 55; + private static final float ROW_HEIGHT_PER_LINE = 20F; private Font labelFont; private Font arrowFont; private CellStyle wrappedStyle; + @Override + public int order() { + return FINAL_WRITE_ORDER; + } + @Override public void afterCellDispose(CellWriteHandlerContext context) { if (Boolean.TRUE.equals(context.getHead()) @@ -47,11 +60,28 @@ public class ModifiedVerifyContentCellWriteHandler implements CellWriteHandler { } offset += line.length() + 1; } - float requiredHeight = Math.max(cell.getRow().getHeightInPoints(), lines.length * 18F); - cell.getRow().setHeightInPoints(requiredHeight); cell.setCellValue(richText); } + @Override + public void afterRowDispose(RowWriteHandlerContext context) { + if (Boolean.TRUE.equals(context.getHead())) { + return; + } + Row row = context.getRow(); + Cell contentCell = row.getCell(MODIFIED_VERIFY_CONTENT_COLUMN_INDEX); + if (contentCell == null || StrUtil.isBlank(contentCell.getStringCellValue())) { + return; + } + String[] lines = contentCell.getStringCellValue().split("\\n", -1); + int visualLineCount = 0; + for (String line : lines) { + visualLineCount += Math.max(1, (line.length() + ESTIMATED_CHARACTERS_PER_LINE - 1) + / ESTIMATED_CHARACTERS_PER_LINE); + } + row.setHeightInPoints(Math.max(row.getHeightInPoints(), visualLineCount * ROW_HEIGHT_PER_LINE)); + } + private Font getLabelFont(Workbook workbook) { if (labelFont == null) { labelFont = workbook.createFont(); @@ -79,7 +109,10 @@ public class ModifiedVerifyContentCellWriteHandler implements CellWriteHandler { wrappedStyle = workbook.createCellStyle(); wrappedStyle.cloneStyleFrom(cell.getCellStyle()); wrappedStyle.setWrapText(true); + wrappedStyle.setVerticalAlignment(VerticalAlignment.TOP); + wrappedStyle.setAlignment(HorizontalAlignment.LEFT); } cell.setCellStyle(wrappedStyle); } + }