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/ExcelUtilManySheetSecond.java | 224 +++++++++++++++++++++++++++++++++++++++++++++++--------
1 files changed, 189 insertions(+), 35 deletions(-)
diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtilManySheetSecond.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtilManySheetSecond.java
index 287e7b0..909e88e 100644
--- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtilManySheetSecond.java
+++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtilManySheetSecond.java
@@ -14,7 +14,6 @@
import com.ruoyi.common.utils.reflect.ReflectUtils;
import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;
import org.apache.poi.ss.usermodel.*;
-import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
@@ -23,7 +22,9 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
+import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@@ -270,7 +271,30 @@
this.init(list, sheetName, Excel.Type.EXPORT);
exportExcel(response.getOutputStream());
}
+ private static double calculateScaleForCm(byte[] imageData, double widthCm, double heightCm) throws IOException {
+ // 璇诲彇鍥剧墖鑾峰彇鍘熷灏哄
+ BufferedImage img = ImageIO.read(new ByteArrayInputStream(imageData));
+ double originalWidthPx = img.getWidth();
+ double originalHeightPx = img.getHeight();
+ // Excel榛樿DPI锛�96锛�
+ double excelDpi = 96.0;
+ double pixelsPerCm = excelDpi / 2.54;
+
+ // 鐩爣鍍忕礌灏哄
+ double targetWidthPx = widthCm * pixelsPerCm;
+ double targetHeightPx = heightCm * pixelsPerCm;
+
+ // 璁$畻姣斾緥锛堝彇瀹介珮涓殑杈冨皬姣斾緥锛屼繚鎸佺旱妯瘮锛�
+ double scaleWidth = targetWidthPx / originalWidthPx;
+ double scaleHeight = targetHeightPx / originalHeightPx;
+ System.out.println("-=========67676");
+ System.out.println(originalHeightPx);
+ System.out.println(originalWidthPx);
+ System.out.println(targetWidthPx);
+ System.out.println(targetHeightPx);
+ return Math.min(scaleWidth, scaleHeight);
+ }
/**
* 瀵筶ist鏁版嵁婧愬皢鍏堕噷闈㈢殑鏁版嵁瀵煎叆鍒癳xcel琛ㄥ崟
*
@@ -294,13 +318,37 @@
if(index==1)
{
Row row = sheet.createRow(0);
- row.setHeightInPoints(20); // 璁剧疆琛岄珮涓�20纾�
+ // row.setHeightInPoints(20); // 璁剧疆琛岄珮涓�20纾�
+ row.setHeight((short) (12.75*20));
int column = 0;
- row = sheet.createRow(0);
+ row = sheet.createRow(1);
+ row.setHeight((short) (12.75*20));
sheet.addMergedRegion(new CellRangeAddress(2, 3, 0, 1));
row = sheet.createRow(2);
- row.setHeightInPoints(100); // 璁剧疆琛岄珮涓�20纾�
+ row.setHeight((short) (77.5*20));
+ row = sheet.createRow(3);
+ row.setHeight((short) (35*20));
+ // row.setHeightInPoints(100); // 璁剧疆琛岄珮涓�20纾�
+
+ // 椤甸潰甯冨眬璁剧疆
+ sheet.setAutobreaks(true);
+ sheet.setFitToPage(true);
+
+ // 鑾峰彇鎵撳嵃璁剧疆
+ PrintSetup printSetup = sheet.getPrintSetup();
+
+ // 璁剧疆涓�1椤靛搴�
+ printSetup.setFitWidth((short) 1); // 瀹藉害璋冩暣涓�1椤�
+ printSetup.setFitHeight((short) 0); // 楂樺害涓嶉檺鍒�
+
+ // 鍏朵粬鎵撳嵃璁剧疆
+ printSetup.setPaperSize(PrintSetup.A4_PAPERSIZE); // A4绾�
+ printSetup.setLandscape(false); // 绾靛悜鎵撳嵃
+ sheet.setMargin(Sheet.LeftMargin, 1.04);
+ sheet.setMargin(Sheet.RightMargin, 1.04);
+ sheet.setMargin(Sheet.TopMargin, 0.97);
+ sheet.setMargin(Sheet.BottomMargin, 0.97);
//鎷垮埌鍥剧墖
// 鍒涘缓鍗曞厓鏍煎苟娣诲姞鍥剧墖
Cell cell = row.createCell(0);
@@ -314,7 +362,9 @@
// 鍒涘缓鍗曞厓鏍�
Cell qrCell = sheet.createRow(2).createCell(1);
// 璁剧疆琛岄珮瓒冲楂樹互鏄剧ず浜岀淮鐮�
- sheet.getRow(2).setHeightInPoints(100);
+ sheet.getRow(2).setHeight((short) (77.5*20));
+ // sheet.getRow(3).setHeight((short) (35*20));
+ // sheet.getRow(2).setHeightInPoints(100);
// 浜岀淮鐮侀渶瑕佽緝澶х殑鍒楀鏉ユ樉绀猴紝璁剧疆涔嬪墠鍏堜繚瀛樺綋鍓嶅垪瀹�
int currentColumnWidth = sheet.getColumnWidth(1);
// 璁剧疆瓒冲瀹界殑鍒楀浠ユ樉绀轰簩缁寸爜
@@ -323,25 +373,40 @@
// 鍒涘缓鍥剧墖閿氱偣锛岃缃湪绗�2琛岀2鍒�
// 浜岀淮鐮佹樉绀哄湪鍙充笂瑙掍笖涓嶅~婊℃暣涓牸瀛�
// 缂╁皬鍥剧墖鑼冨洿锛氬彧鍗犵敤閮ㄥ垎鍗曞厓鏍肩┖闂�
- ClientAnchor anchor = new HSSFClientAnchor(700, 20, 1000, 220, (short) 1, 2, (short) 1, 2);
+
+ ClientAnchor anchor = new HSSFClientAnchor(699, 0, 1023, 97, (short) 1, 2, (short) 1, 3);
// 璁剧疆鍥剧墖浣嶇疆鍜屽ぇ灏�
anchor.setAnchorType(ClientAnchor.AnchorType.MOVE_DONT_RESIZE);
// 娣诲姞鍥剧墖鍒板伐浣滆〃
- getDrawingPatriarch(sheet).createPicture(anchor,
+ org.apache.poi.ss.usermodel.Picture picture = getDrawingPatriarch(sheet).createPicture(anchor,
wb.addPicture(data, getImageType(data)));
+
+
+ int pictureIdx = wb.addPicture(data, getImageType(data));
+ // Picture picture = drawing.createPicture(anchor, pictureIdx);
+
+// 鍏抽敭锛氳绠楀苟搴旂敤3.15cm鐨勭缉鏀�
+// double targetSizeCm = 3.15;
+//
+//// 鏂规硶1锛氫娇鐢ㄧ簿纭绠�
+// double scale = calculateScaleForCm(data, targetSizeCm, targetSizeCm);
+// System.out.println(scale);
+// picture.resize(scale);
+
// 浜岀淮鐮佹坊鍔犲畬鎴愬悗锛屾仮澶嶅師鏉ョ殑鍒楀璁剧疆
sheet.setColumnWidth(1, currentColumnWidth);
}
+
// 浠�"妗f棣� (瀹�) 鍙�:"涓哄熀鍑嗭紝缁熶竴鎵�鏈夋爣棰樼殑闀垮害鍜屾牸寮�
String [] tits = {
- "妗� 鍙�:",
- "妗f棣� (瀹�) 鍙�:",
- "缂� 寰� 鍙�:",
- "鍙� 鏂� 鍙�:",
+ "妗� 鍙�:",
+ "妗f棣� 锛堝锛� 鍙�:",
+ "缂� 寰� 鍙�:",
+ "鍙� 鏂� 鍙�:",
"妗� 鍗� 棰� 鍚�:",
"缂� 鍒� 鏃� 鏈�:",
"缂� 鍒� 鍗� 浣�:",
@@ -354,6 +419,8 @@
System.out.println(declaredFields.length);
int i = 0;
for (Field field : declaredFields) {
+ if(i==9)
+ break;
// 璁剧疆瀛楁鐨勮闂潈闄愶紝浠ヤ究浜庤闂鏈夊瓧娈�
field.setAccessible(true);
@@ -366,10 +433,13 @@
Object fieldValue = field1.get(obj);
if(fieldValue==null)
fieldValue="";
+
if(i <= 3)
row = sheet.createRow(i+4);
if(i > 3)
row = sheet.createRow(i+7);
+ if(i != 4)
+ row.setHeight((short) (35*20));
// row.setHeight((short) 30);
// 涓嶈缃浐瀹氳楂橈紝鍚庣画浣跨敤autoSizeRow鑷姩璋冩暣
cell = row.createCell(0);
@@ -377,11 +447,11 @@
// 绗竴鍒楀搴﹁缃负27
// 绗簩鍒楀搴﹁缃负51
// 浣跨敤涓巃ddCell鏂规硶鐩稿悓鐨勮绠楀叕寮忚缃垪瀹�
- sheet.setColumnWidth(0, (int) ((29 + 0.72) * 256)); // 绗竴鍒楋細瀹藉害27
- sheet.setColumnWidth(1, (int) ((52 + 0.72) * 256)); // 绗簩鍒楋細瀹藉害51
+ sheet.setColumnWidth(0, (int) ((27.86 + 0.72) * 256)); // 绗竴鍒楋細瀹藉害27
+ sheet.setColumnWidth(1, (int) ((51.57 + 0.72) * 256)); // 绗簩鍒楋細瀹藉害51
CellStyle style = wb.createCellStyle();
- style.setAlignment(HorizontalAlignment.RIGHT);
+ style.setAlignment(HorizontalAlignment.LEFT);
style.setVerticalAlignment(VerticalAlignment.BOTTOM); // 璁剧疆鍨傜洿涓嬪榻�
style.setWrapText(true); // 璁剧疆鑷姩鎹㈣
@@ -392,11 +462,20 @@
font.setFontHeightInPoints((short) 18);
style.setFont(font);
style.setWrapText(true); // 璁剧疆鑷姩鎹㈣
+
+ if(i==4)
+ {
+ style.setVerticalAlignment(VerticalAlignment.TOP); // 璁剧疆鍨傜洿涓嬪榻�
+
+ }
cell.setCellStyle(style);
cell.setCellValue(tits[i]);
+
+
Cell cel = row.createCell(1);
CellStyle style1 = wb.createCellStyle();
- style1.setBorderBottom(BorderStyle.THIN); // 娣诲姞涓嬪垝绾�
+ if(i!=4)
+ style1.setBorderBottom(BorderStyle.THIN); // 娣诲姞涓嬪垝绾�
style1.setWrapText(true); // 璁剧疆鑷姩鎹㈣
style1.setVerticalAlignment(VerticalAlignment.BOTTOM); // 璁剧疆鍨傜洿涓嬪榻�
@@ -416,8 +495,16 @@
{
// 纭繚妗� 鍗� 棰� 鍚�:杩欎竴琛岃兘鏍规嵁鍐呭闀垮害鑷姩鎹㈣
style1.setWrapText(true); // 鏄庣‘璁剧疆鑷姩鎹㈣
+ style1.setVerticalAlignment(VerticalAlignment.TOP); // 璁剧疆鍨傜洿涓嬪榻�
+
cel.setCellStyle(style1);
+ row.setHeight((short) (110*20));
+
+
+
+
}
+
// 澶勭悊鏃ユ湡鏍煎紡鍖栵紝鐗瑰埆鏄�"缂� 鍒� 鏃� 鏈�:"杩欎竴琛�
if (i == 5 && fieldValue instanceof Date) {
// 璁剧疆鏃ユ湡鏍煎紡涓簓yyy-MM-dd
@@ -428,29 +515,32 @@
}
// 瀵逛簬i==4鐨勬儏鍐碉紝纭繚琛岄珮鑳芥牴鎹唴瀹硅嚜鍔ㄨ皟鏁�
- if(i==4)
- {
- // 璁剧疆琛岄珮涓鸿嚜鍔ㄨ皟鏁达紙-1琛ㄧず鑷姩楂樺害锛�
- row.setHeight((short) -1);
- // 涓嶉檺鍒舵渶澶ц楂橈紝鍏佽鏍规嵁鍐呭鑷姩璋冩暣
- } else {
- // 浣跨敤setHeight鏂规硶璁剧疆琛岄珮涓鸿嚜鍔ㄨ皟鏁达紙-1琛ㄧず鑷姩楂樺害锛�
- row.setHeight((short) -1);
- // 纭繚琛岄珮鑷冲皯涓�40纾咃紝浣嗕笉闄愬埗鏈�澶ц楂�
- if (row.getHeightInPoints() < 40) {
- row.setHeightInPoints(40);
- }
- }
+// if(i==4)
+// {
+// // 璁剧疆琛岄珮涓鸿嚜鍔ㄨ皟鏁达紙-1琛ㄧず鑷姩楂樺害锛�
+// // row.setHeight((short) -1);
+// // 涓嶉檺鍒舵渶澶ц楂橈紝鍏佽鏍规嵁鍐呭鑷姩璋冩暣
+// } else if (i > 4) { // 鍙i>1鐨勮搴旂敤杩欎簺璁剧疆锛屼繚鐣檌<=1鐨勮楂樿缃�
+// // 浣跨敤setHeight鏂规硶璁剧疆琛岄珮涓鸿嚜鍔ㄨ皟鏁达紙-1琛ㄧず鑷姩楂樺害锛�
+// row.setHeight((short) -1);
+// // 纭繚琛岄珮鑷冲皯涓�40纾咃紝浣嗕笉闄愬埗鏈�澶ц楂�
+// if (row.getHeightInPoints() < 40) {
+// row.setHeightInPoints(40);
+// }
+// }
// sheet.setColumnWidth(0, 60 * 256);
i++;
if(i==3)
{
row = sheet.createRow(8);
+ row.setHeight((short) (35*20));
// 璁剧疆琛岄珮涓鸿嚜鍔ㄨ皟鏁�
- row.setHeight((short) -1);
+ // row.setHeight((short) -1);
// row.setHeight((short) 30);
- row.setHeightInPoints(20); // 璁剧疆琛岄珮涓�20纾�
+ // row.setHeightInPoints(20); // 璁剧疆琛岄珮涓�20纾�
row = sheet.createRow(9);
+ row.setHeight((short) (50*20));
+
//璁剧疆鏉″舰鐮�
// 璁剧疆鏉″舰鐮�
byte[] dat = list.get(index).getImgr();
@@ -458,7 +548,8 @@
// 鍒涘缓鍗曞厓鏍�
Cell barcodeCell = sheet.createRow(9).createCell(0);
// 璁剧疆琛岄珮瓒冲楂樹互鏄剧ず鏉″舰鐮�
- sheet.getRow(9).setHeightInPoints(40);
+ // sheet.getRow(9).setHeightInPoints(40);
+ sheet.getRow(9).setHeight((short) (50*20));
// 璁剧疆鍒楀瓒冲瀹戒互鏄剧ず鏉″舰鐮侊紝鍏堜繚瀛樺綋鍓嶅垪瀹�
int currentColumnWidth0 = sheet.getColumnWidth(0);
sheet.setColumnWidth(0, 40 * 256);
@@ -467,7 +558,7 @@
// 鏉″舰鐮佸眳涓笖涓嶈秴鍑虹浜屼釜鏍煎瓙
// 璋冩暣缁撴潫鍒楃储寮曞拰浣嶇疆鍙傛暟
// 澧炲姞dx1鍊硷紝浣挎潯褰㈢爜鏁翠綋鍚戝彸绉诲姩
- ClientAnchor anchor1 = new HSSFClientAnchor(660, 50, 900, 200, (short) 0, 9, (short) 1, 10);
+ ClientAnchor anchor1 = new HSSFClientAnchor(537, 0, 788, 245, (short) 0, 9, (short) 1, 9);
// 璁剧疆鍥剧墖浣嶇疆鍜屽ぇ灏�
anchor1.setAnchorType(ClientAnchor.AnchorType.MOVE_DONT_RESIZE);
@@ -482,9 +573,9 @@
- row.setHeightInPoints(70); // 璁剧疆琛岄珮涓�20纾�
+ //row.setHeightInPoints(70); // 璁剧疆琛岄珮涓�20纾�
row = sheet.createRow(10);
- row.setHeightInPoints(40); // 璁剧疆琛岄珮涓�20纾�
+ row.setHeight((short) (35*20));
}
// 鎵撳嵃瀛楁鍚嶇О鍜屽��
System.out.println(fieldName + ": " + fieldValue);
@@ -811,7 +902,7 @@
Cell cell = null;
try {
// 璁剧疆琛岄珮涓鸿嚜鍔ㄨ皟鏁�
- row.setHeight((short) -1);
+ // row.setHeight((short) -1);
// 鏍规嵁Excel涓缃儏鍐靛喅瀹氭槸鍚﹀鍑�,鏈変簺鎯呭喌闇�瑕佷繚鎸佷负绌�,甯屾湜鐢ㄦ埛濉啓杩欎竴鍒�.
if (attr.isExport()) {
// 鍒涘缓cell
@@ -855,6 +946,9 @@
// 璁剧疆鍒楃被鍨�
setCellVo(value, attr, cell);
}
+
+ adjustRowHeightAfterSetValue(row, cell, value);
+
addStatisticsData(column, Convert.toStr(value), attr);
}
} catch (Exception e) {
@@ -862,7 +956,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 鍗曞厓鏍兼彁绀�
*
@@ -1197,3 +1349,5 @@
+
+
--
Gitblit v1.9.1