From ccb345a4891a11d9ea08e775d9b3db850aa871ff Mon Sep 17 00:00:00 2001 From: yinhuaiwei Date: Wed, 5 Nov 2025 15:02:07 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E8=A1=A8=E6=A0=BC?= =?UTF-8?q?=E5=8D=95=E5=85=83=E6=A0=BC=E8=87=AA=E9=80=82=E5=BA=94=E8=AE=A1?= =?UTF-8?q?=E7=AE=97=E9=AB=98=E5=BA=A6=E6=96=B9=E6=B3=95,=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E5=B7=A1=E8=A7=86=E6=8A=A5=E5=91=8A=E5=8D=95=E5=85=83?= =?UTF-8?q?=E6=A0=BC=E6=96=87=E6=9C=AC=E5=B1=95=E7=A4=BA=E4=B8=8D=E5=85=A8?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../PatrolTaskResultMainController.java | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/inspect-main/inspect-main-task/src/main/java/com/inspect/resultmain/controller/PatrolTaskResultMainController.java b/inspect-main/inspect-main-task/src/main/java/com/inspect/resultmain/controller/PatrolTaskResultMainController.java index 2a5de5d..8206ebc 100644 --- a/inspect-main/inspect-main-task/src/main/java/com/inspect/resultmain/controller/PatrolTaskResultMainController.java +++ b/inspect-main/inspect-main-task/src/main/java/com/inspect/resultmain/controller/PatrolTaskResultMainController.java @@ -3293,12 +3293,14 @@ public class PatrolTaskResultMainController extends BaseController { Cell cell62 = row6.createCell(4); cell62.setCellValue(inspectionReport.getInspectionTaskName()); cell62.setCellStyle(colStyle2); + autoFitRowHeight(sheet, 4, 4, colStyle2); Cell cell63 = row6.createCell(8); cell63.setCellValue(MessageUtils.get("巡视统计")); cell63.setCellStyle(colStyle2); Cell cell64 = row6.createCell(12); cell64.setCellValue(inspectionReport.getPatrolStatistics()); cell64.setCellStyle(colStyle2); + autoFitRowHeight(sheet, 4, 12, colStyle2); for (int i = 0; i < 15; ++i) { if (i != 0 && i != 4 && i != 8 && i != 12) { @@ -3743,6 +3745,53 @@ public class PatrolTaskResultMainController extends BaseController { return outputStream.toByteArray(); } + /** + * 自适应行高(手动计算) + * @param sheet + * @param rowNum 行号 + * @param cellNum 列号 + * @param style 样式 + */ + public void autoFitRowHeight(Sheet sheet, int rowNum, int cellNum, CellStyle style) { + Row row = sheet.getRow(rowNum); + if (row == null) { + return; + } + Cell cell = row.getCell(cellNum); + if (cell == null) { + return; + } + + String text = cell.getStringCellValue(); + if (text == null || text.isEmpty()) { + return; + } + + int columnWidth = sheet.getColumnWidth(0); + double widthInChars = columnWidth / 256.0; + + // 获取字体 + Font font = sheet.getWorkbook().getFontAt(style.getFontIndex()); + double fontHeightInPoints = font.getFontHeightInPoints(); + // 计算每行可容纳字符数(近似公式) + // 粗略估计 + double charsPerLine = widthInChars * 1.0; + + // 按换行符分割 + 按列宽折行 + String[] lines = text.split("\n"); + int totalLines = 0; + + for (String line : lines) { + int lineChars = line.length(); + int linesNeeded = (int) Math.ceil(lineChars / charsPerLine); + totalLines += Math.max(1, linesNeeded); + } + + // 设置行高(1 行 ≈ 15 磅,POI 用 points) + double rowHeightInPoints = totalLines * fontHeightInPoints; + row.setHeightInPoints((float) Math.max(rowHeightInPoints, row.getHeightInPoints())); + } + @GetMapping({"/test"}) public AjaxResult test(int count) { String strs = "menglei/2.jpg,menglei/1123.jpg";