From ca4b62abd2560b6dcffbf3ff43da1b04427b494b Mon Sep 17 00:00:00 2001
From: fei <791364011@qq.com>
Date: 星期二, 20 一月 2026 12:36:47 +0800
Subject: [PATCH] 增加了签名和注解
---
ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtilManySheet.java | 246 +++++++++++++++++++++++++++++++++++++------------
1 files changed, 185 insertions(+), 61 deletions(-)
diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtilManySheet.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtilManySheet.java
index 04a1881..1a6397a 100644
--- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtilManySheet.java
+++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtilManySheet.java
@@ -527,6 +527,8 @@
public void fillExcelData(int index, Row row) {
// int startNo = index * sheetSize;
// int endNo = Math.min(startNo + sheetSize, list.size());
+
+ int startRow = (index == 1) ? 5 : 1;
for (int i = 0; i < list.size(); i++) {
if(index==1)
row = sheet.createRow(i + 5 );
@@ -536,6 +538,7 @@
// sheet.autoSizeRow(0);
// 绗簩姝ワ細鑾峰彇鑷姩璁$畻鍚庣殑琛岄珮
+ // row.setHeight((short) -1); // 鍏堣涓鸿嚜鍔紝鐢盿ddCell璁$畻鍚庤鐩�
// 寰楀埌瀵煎嚭瀵硅薄.
T vo = (T) list.get(i);
@@ -548,10 +551,180 @@
this.addCell(excel, row, vo, field, column++);
}
-
+ // 鎵归噺璋冩暣琛岄珮锛堟暟鎹~鍏呭畬鎴愬悗锛�
+ if (list.size() > 0) {
+ int lastRow = startRow + list.size() - 1;
+ batchAdjustRowHeights(sheet, startRow, lastRow);
+ }
}
}
+ /**
+ * 鎵归噺澶勭悊琛岄珮锛堝湪鏁版嵁濉厖瀹屾垚鍚庣粺涓�璁$畻锛�
+ */
+ /**
+ * 鎵归噺澶勭悊琛岄珮锛堝湪鏁版嵁濉厖瀹屾垚鍚庣粺涓�璁$畻锛�
+ */
+ private String getCellStringValue(Cell cell) {
+ if (cell == null) return null;
+ switch (cell.getCellType()) {
+ case STRING:
+ return cell.getStringCellValue();
+ case NUMERIC:
+ return String.valueOf(cell.getNumericCellValue());
+ case BOOLEAN:
+ return String.valueOf(cell.getBooleanCellValue());
+ case FORMULA:
+ try {
+ return cell.getStringCellValue();
+ } catch (Exception e) {
+ return String.valueOf(cell.getNumericCellValue());
+ }
+ default:
+ return null;
+ }
+ }
+ private void batchAdjustRowHeights(Sheet sheet, int startRow, int endRow) {
+ Workbook workbook = sheet.getWorkbook();
+
+
+ for (int rowNum = startRow; rowNum <= endRow; rowNum++) {
+ Row row = sheet.getRow(rowNum);
+ if (row == null) continue;
+
+ int maxLinesInRow = 1; // 璁板綍璇ヨ鎵�鏈夊崟鍏冩牸涓殑鏈�澶ц鏁�
+
+ // 绗竴姝ワ細閬嶅巻璇ヨ鐨勬墍鏈夊崟鍏冩牸锛屾壘鍒版渶澶ц鏁伴渶姹�
+ for (int colNum = 0; colNum < row.getLastCellNum(); colNum++) {
+ Cell cell = row.getCell(colNum);
+ if (cell == null) continue;
+
+ // 鑾峰彇鍗曞厓鏍煎唴瀹�
+ String content = getCellStringValue(cell);
+ if (content == null || content.isEmpty()) continue;
+
+ // 鑾峰彇鍒楀
+ int columnWidth = sheet.getColumnWidth(colNum) / 256;
+ if (columnWidth <= 0) columnWidth = 10; // 榛樿鍒楀
+
+ // 璁$畻璇ュ崟鍏冩牸鍐呭闇�瑕佺殑琛屾暟
+ int contentLines = calculateContentLines(content, columnWidth);
+
+ // 鏇存柊鏈�澶ц鏁�
+ maxLinesInRow = Math.max(maxLinesInRow, contentLines);
+ }
+
+ // 绗簩姝ワ細鏍规嵁鏈�澶ц鏁拌缃楂橈紙鍏抽敭淇锛�
+ if (maxLinesInRow > 1) {
+ // 鍩虹琛岄珮锛氫竴琛屾枃鏈殑楂樺害
+ int baseHeightPerLine = 400; // 20鐐� = 400鍗曚綅锛堝缓璁�硷級
+
+ // 璁$畻鎬婚珮搴� = 琛屾暟 脳 姣忚楂樺害
+ int newHeight = maxLinesInRow * baseHeightPerLine + 600;
+
+ // 闄愬埗鏈�灏忓拰鏈�澶ч珮搴�
+ int minHeight = 300; // 15鐐�
+ int maxHeight = 10000; // 500鐐�
+ newHeight = Math.max(minHeight, Math.min(newHeight, maxHeight));
+
+ // 璁剧疆琛岄珮
+ row.setHeight((short) newHeight);
+
+ // 鍙�夛細璁板綍璋冩暣淇℃伅鐢ㄤ簬璋冭瘯
+ log.debug("Row {} adjusted: maxLines={}, height={}",
+ rowNum, maxLinesInRow, newHeight);
+ }
+ }
+ }
+ /**
+ * 璁$畻鏂囨湰鐨勬湁鏁堝瓧绗﹀搴�
+ */
+ private double calculateEffectiveWidth(String text) {
+ if (text == null || text.isEmpty()) {
+ return 0;
+ }
+
+ double totalWidth = 0;
+
+ for (char c : text.toCharArray()) {
+ if (isChineseChar(c)) {
+ // 涓枃瀛楃锛�1.0瀹藉害
+ totalWidth += 2.0;
+ } else if (Character.isDigit(c)) {
+ // 鏁板瓧锛�0.6瀹藉害
+ totalWidth += 0.6;
+ } else if (Character.isUpperCase(c)) {
+ // 澶у啓瀛楁瘝锛�0.7瀹藉害
+ totalWidth += 0.7;
+ } else if (Character.isLowerCase(c)) {
+ // 灏忓啓瀛楁瘝锛�0.5瀹藉害
+ totalWidth += 0.5;
+ } else if (c == '.' || c == ',') {
+ // 鐐瑰彿銆侀�楀彿锛�0.3瀹藉害
+ totalWidth += 0.3;
+ } else if (c == '-' || c == '_') {
+ // 杩炲瓧绗︺�佷笅鍒掔嚎锛�0.35瀹藉害
+ totalWidth += 0.35;
+ } else if (c == ' ') {
+ // 绌烘牸锛�0.3瀹藉害
+ totalWidth += 0.3;
+ } else if (c == '\t') {
+ // 鍒惰〃绗︼細4.0瀹藉害
+ totalWidth += 4.0;
+ } else {
+ // 鍏朵粬瀛楃锛氶粯璁�0.6瀹藉害
+ totalWidth += 0.6;
+ }
+ }
+
+ return totalWidth;
+ }
+ /**
+ * 璁$畻鍗曞厓鏍煎唴瀹规墍闇�琛屾暟锛堟洿绮剧‘鐨勭増鏈級
+ */
+ private int calculateContentLines(String content, int columnWidthChars) {
+ if (content == null || content.isEmpty() || columnWidthChars <= 0) {
+ return 1;
+ }
+
+ int totalLines = 0;
+ String[] lines = content.split("\n");
+
+ for (String line : lines) {
+ if (line.trim().isEmpty()) {
+ totalLines++; // 绌鸿涔熺畻涓�琛�
+ continue;
+ }
+
+ // 璁$畻璇ヨ闇�瑕佺殑瀛楃瀹藉害
+ // 鑰冭檻涓嫳鏂囧瓧绗﹀搴﹀樊寮�
+ double effectiveLength = 0;
+
+
+ effectiveLength += calculateEffectiveWidth(line); // 涓枃瀛楃鍗�1涓搴�
+
+
+
+ // 璁$畻闇�瑕佺殑琛屾暟
+ int linesForText = (int) Math.ceil(effectiveLength / columnWidthChars);
+ totalLines += Math.max(1, linesForText);
+ }
+
+ return Math.max(totalLines, 1);
+ }
+
+ /**
+ * 鍒ゆ柇鏄惁涓轰腑鏂囧瓧绗�
+ */
+ private boolean isChineseChar(char c) {
+ Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
+ return ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
+ || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
+ || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
+ || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
+ || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
+ || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS;
+ }
/**
* 鍒涘缓琛ㄦ牸鏍峰紡
*
@@ -736,7 +909,7 @@
Cell cell = null;
try {
// 璁剧疆琛岄珮涓鸿嚜鍔ㄨ皟鏁�
- // row.setHeight((short) -1);
+ row.setHeight((short) -1);
// 鏍规嵁Excel涓缃儏鍐靛喅瀹氭槸鍚﹀鍑�,鏈変簺鎯呭喌闇�瑕佷繚鎸佷负绌�,甯屾湜鐢ㄦ埛濉啓杩欎竴鍒�.
if (attr.isExport()) {
// 鍒涘缓cell
@@ -751,6 +924,7 @@
} else if (align == HorizontalAlignment.RIGHT) {
styleKey = "data3";
}
+ System.out.println(styleKey);
// 鑾峰彇骞朵慨鏀规牱寮�
CellStyle style = styles.get(styleKey);
Workbook workbook = row.getSheet().getWorkbook();
@@ -775,8 +949,16 @@
setCellVo(value, attr, cell);
}
- adjustRowHeightAfterSetValue(row, cell, value);
+ System.out.println(row.getHeight());
+// // adjustRowHeightAfterSetValue(row, cell, value);
+// int defaultRowHeight = row.getHeight() / 20; // 鍘熷琛岄珮锛堣浆鎴愮偣鏁帮級
+// if(value==null)
+// value="";
+// System.out.println(sheet.getColumnWidth(column));
+// int contentLines = getContentLines(value.toString(), sheet.getColumnWidth(column), style); // 璁$畻鎹㈣鍚庣殑琛屾暟
+// int newHeight = defaultRowHeight * contentLines; // 鎸夎鏁拌皟鏁磋楂橈紙鍙姞灏戦噺鍐椾綑锛�
+// row.setHeightInPoints((short) (newHeight)); // 鎻愰珮琛岄珮锛�+2 鏄啑浣欙紝閬垮厤鍐呭琚埅鏂級
addStatisticsData(column, Convert.toStr(value), attr);
}
} catch (Exception e) {
@@ -784,65 +966,7 @@
}
return cell;
}
- /**
- * 璁剧疆鍊煎悗璋冩暣琛岄珮
- */
- private void adjustRowHeightAfterSetValue(Row row, Cell cell, Object value) {
- if (value == null) return;
- String text = value.toString();
- Sheet sheet = row.getSheet();
-
- // 1. 璁$畻鑷姩楂樺害锛堝熀浜庡唴瀹癸級
- short autoHeight = calculateSimpleAutoHeight(text, cell);
-
- // 2. 鑾峰彇褰撳墠琛岄珮
- short currentHeight = row.getHeight();
- if (currentHeight == -1) {
- currentHeight = sheet.getDefaultRowHeight();
- }
-
- // 3. 浣跨敤杈冨ぇ鐨勯珮搴︼紙鑷姩璁$畻鐨勯珮搴︽垨褰撳墠楂樺害锛�
- short baseHeight = (short) Math.max(currentHeight, autoHeight);
- System.out.println(baseHeight+"aaaaaaaaatttttttt");
- // 4. 鍦ㄥ熀纭�涓婂鍔犻澶栭珮搴︼紙100鍗曚綅 = 5鐐癸級
- short extraHeight = 80;
- short newHeight = (short) (baseHeight + extraHeight);
-
- // 5. 闄愬埗鏈�澶ч珮搴�
- short maxHeight = (short) 4000; // 100鐐�
- row.setHeight((short) Math.min(maxHeight,newHeight));
- }
-
- /**
- * 绠�鍖栫殑鑷姩楂樺害璁$畻
- */
- private short calculateSimpleAutoHeight(String text, Cell cell) {
- if (text == null || text.isEmpty()) return 0;
-
- Sheet sheet = cell.getSheet();
- int colIndex = cell.getColumnIndex();
-
- // 鑾峰彇鍒楀锛堝瓧绗︽暟锛�
- int colWidthChars = sheet.getColumnWidth(colIndex) / 256;
- if (colWidthChars <= 0) colWidthChars = 10;
-
- // 璁$畻鏂囨湰琛屾暟
- int lines = 1;
- if (text.contains("\n")) {
- // 鏈夋樉寮忔崲琛�
- String[] parts = text.split("\n");
- for (String part : parts) {
- lines += Math.max(1, (int) Math.ceil(part.length() * 1.5 / colWidthChars));
- }
- } else {
- // 鑷姩鎹㈣
- lines = (int) Math.ceil(text.length() * 1.5 / colWidthChars);
- }
-
- // 姣忚楂樺害锛氬亣璁�18鐐癸紙360 POI鍗曚綅锛�
- return (short) (lines * 360);
- }
/**
* 璁剧疆 POI HSSFSheet 鍗曞厓鏍兼彁绀�
*
--
Gitblit v1.9.1