From 5d91a329768a2a86e01e4b9b6bc6a2b939b87adb Mon Sep 17 00:00:00 2001
From: fei <791364011@qq.com>
Date: 星期五, 16 一月 2026 22:48:51 +0800
Subject: [PATCH] 修改了对应代码
---
ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtilManySheet.java | 127 ++++++++++++++++++++++++++++++++++-------
1 files changed, 104 insertions(+), 23 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 4631cea..04a1881 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
@@ -316,14 +316,16 @@
printSetup.setLandscape(false); // 绾靛悜鎵撳嵃
// 璁剧疆椤佃竟璺�
- sheet.setMargin(Sheet.LeftMargin, 0.5);
- sheet.setMargin(Sheet.RightMargin, 0.5);
+ sheet.setMargin(Sheet.LeftMargin, 0.65);
+ sheet.setMargin(Sheet.RightMargin, 0.65);
+ sheet.setMargin(Sheet.TopMargin,0.31);
+ sheet.setMargin(Sheet.BottomMargin, 0.31);
// 璁剧疆鎵撳嵃鍖哄煙锛屼粠绗�0琛屽埌绗�1000琛岋紝绗�0鍒楀埌绗�20鍒楋紙鏍规嵁瀹為檯鎯呭喌璋冩暣锛�
// sheet.setPrintArea(0, 0, 20, 0, 1000);
// 濡傛灉鏄痠ndex涓�1鐨剆heet锛岃缃《绔爣棰樿
// 灏嗙1琛岋紙绱㈠紩涓�0锛夎缃负姣忛〉閲嶅鐨勬爣棰樿
- sheet.setRepeatingRows(CellRangeAddress.valueOf("$1:$4"));
+ sheet.setRepeatingRows(CellRangeAddress.valueOf("$1:$5"));
row = sheet.createRow(0);
row.setHeight((short) (37*20));
@@ -334,17 +336,22 @@
// if (Files.exists(Paths.get(imagePath))) {
// byte[] imageBytes = Files.readAllBytes(Paths.get(imagePath));
// 灏哹yte鏁扮粍杞崲鎴恑nputstream锛屼互渚挎彃鍏ュ埌Excel涓�
- ClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 100, (short) cell.getColumnIndex(), cell.getRow().getRowNum(), (short) (cell.getColumnIndex() + 5),
- cell.getRow().getRowNum() + 1);
+ ClientAnchor anchor = new HSSFClientAnchor(813, 0, 787, 71, (short) 2, 0, (short) 4,
+ 1);
+
+
+
+// ClientAnchor anchor = new HSSFClientAnchor(813, 0, 787, 71, (short) 2, 0, (short) 4,
+// 2);
// 璁剧疆閿氱偣绫诲瀷涓篗OVE_AND_RESIZE锛屼娇鍥剧墖鍙互闅忓崟鍏冩牸璋冩暣澶у皬
anchor.setAnchorType(ClientAnchor.AnchorType.MOVE_AND_RESIZE);
// 璁$畻灞呬腑浣嶇疆
- int col1 = 3; // 涓棿鍒�
- int col2 = col1 + 2;
- anchor.setCol1(col1);
- anchor.setCol2(col2);
- anchor.setRow1(0);
- anchor.setRow2(1);
+// int col1 = 3; // 涓棿鍒�
+// int col2 = col1 + 2;
+// anchor.setCol1(col1);
+// anchor.setCol2(col2);
+// anchor.setRow1(0);
+// anchor.setRow2(1);
// anchor.set
// anchor.setHorizontallyCenter(true);
@@ -401,7 +408,7 @@
CellStyle underlineStyle = wb.createCellStyle();
Font underlineFont = wb.createFont();
underlineFont.setFontName("瀹嬩綋");
- underlineFont.setFontHeightInPoints((short) 11);
+ underlineFont.setFontHeightInPoints((short) 12);
underlineFont.setUnderline(Font.U_SINGLE); // 璁剧疆涓嬪垝绾�
underlineStyle.setFont(underlineFont);
recordIdCell.setCellStyle(underlineStyle);
@@ -525,6 +532,11 @@
row = sheet.createRow(i + 5 );
else
row = sheet.createRow(i + 1 );
+ // 绗竴姝ワ細璁㏄OI鑷姩璁$畻閫傞厤鍐呭鐨勮楂�
+ // sheet.autoSizeRow(0);
+ // 绗簩姝ワ細鑾峰彇鑷姩璁$畻鍚庣殑琛岄珮
+
+
// 寰楀埌瀵煎嚭瀵硅薄.
T vo = (T) list.get(i);
int column = 0;
@@ -534,7 +546,9 @@
// 璁剧疆瀹炰綋绫荤鏈夊睘鎬у彲璁块棶
field.setAccessible(true);
this.addCell(excel, row, vo, field, column++);
+
}
+
}
}
@@ -551,17 +565,17 @@
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
style.setBorderRight(BorderStyle.THIN);
- style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
+ style.setRightBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderLeft(BorderStyle.THIN);
- style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
+ style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderTop(BorderStyle.THIN);
- style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
+ style.setTopBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderBottom(BorderStyle.THIN);
- style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
+ style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
// 璁剧疆鑷姩鎹㈣
style.setWrapText(true);
Font dataFont = wb.createFont();
- dataFont.setFontName("Arial");
+ dataFont.setFontName("瀹嬩綋");
dataFont.setFontHeightInPoints((short) 12);
style.setFont(dataFont);
styles.put("data", style);
@@ -600,7 +614,7 @@
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
Font totalFont = wb.createFont();
- totalFont.setFontName("Arial");
+ totalFont.setFontName("瀹嬩綋");
totalFont.setFontHeightInPoints((short) 12);
style.setFont(totalFont);
// 鎬昏琛屼篃璁剧疆鑷姩鎹㈣
@@ -722,7 +736,7 @@
Cell cell = null;
try {
// 璁剧疆琛岄珮涓鸿嚜鍔ㄨ皟鏁�
- row.setHeight((short) -1);
+ // row.setHeight((short) -1);
// 鏍规嵁Excel涓缃儏鍐靛喅瀹氭槸鍚﹀鍑�,鏈変簺鎯呭喌闇�瑕佷繚鎸佷负绌�,甯屾湜鐢ㄦ埛濉啓杩欎竴鍒�.
if (attr.isExport()) {
// 鍒涘缓cell
@@ -737,7 +751,13 @@
} else if (align == HorizontalAlignment.RIGHT) {
styleKey = "data3";
}
- cell.setCellStyle(styles.get(styleKey));
+ // 鑾峰彇骞朵慨鏀规牱寮�
+ CellStyle style = styles.get(styleKey);
+ Workbook workbook = row.getSheet().getWorkbook();
+ CellStyle newStyle = workbook.createCellStyle();
+ newStyle.cloneStyleFrom(style);
+ newStyle.setWrapText(true); // 鍏抽敭锛氬惎鐢ㄨ嚜鍔ㄦ崲琛�
+ cell.setCellStyle(newStyle);
// 鐢ㄤ簬璇诲彇瀵硅薄涓殑灞炴��
Object value = getTargetValue(vo, field, attr);
@@ -754,6 +774,9 @@
// 璁剧疆鍒楃被鍨�
setCellVo(value, attr, cell);
}
+
+ adjustRowHeightAfterSetValue(row, cell, value);
+
addStatisticsData(column, Convert.toStr(value), attr);
}
} catch (Exception e) {
@@ -761,7 +784,65 @@
}
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 鍗曞厓鏍兼彁绀�
*
@@ -1090,11 +1171,11 @@
HSSFSheet hssfSheet = hssfWb.getSheetAt(index);
HSSFHeader header = hssfSheet.getHeader();
// 璁剧疆椤电湁瀛椾綋
- // header.setFontName("瀹嬩綋");
+ // header.setFontName("瀹嬩綋");4
// header.setFontSize((short) 10);
// 璁剧疆椤电湁鍐呭
- hssfSheet.setMargin(Sheet.HeaderMargin, 2.1);
- header.setRight("鍏� &N 椤� 绗� &P椤�");
+ hssfSheet.setMargin(Sheet.HeaderMargin, 1.43);
+ header.setRight("&\"瀹嬩綋,Bold\"鍏� &N 椤� 绗� &P 椤�");
}
// 濡傛灉鏄痠ndex涓�1鐨剆heet锛岃缃《绔爣棰樿
if (index == 1) {
--
Gitblit v1.9.1