From 07a2f9c762efb3be3a29e2e8cc7004d4164cc9d8 Mon Sep 17 00:00:00 2001
From: fei <791364011@qq.com>
Date: 星期一, 22 十二月 2025 20:15:58 +0800
Subject: [PATCH] 修改了对应代码
---
ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtilManySheet.java | 131 ++++++++++++++++++++++++++++++-------------
1 files changed, 90 insertions(+), 41 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 2415462..39c5d24 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
@@ -17,9 +17,12 @@
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellRangeAddressList;
-import org.apache.poi.xssf.streaming.SXSSFWorkbook;
-import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
-import org.apache.poi.xssf.usermodel.XSSFDataValidation;
+import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
+import org.apache.poi.hssf.usermodel.HSSFDataValidation;
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
+import org.apache.poi.hssf.usermodel.HSSFDataValidation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -58,6 +61,14 @@
* 瀵煎嚭绫诲瀷锛圗XPORT:瀵煎嚭鏁版嵁锛汭MPORT锛氬鍏ユā鏉匡級
*/
private Excel.Type type;
+
+ public Workbook getWb() {
+ return wb;
+ }
+
+ public void setWb(Workbook wb) {
+ this.wb = wb;
+ }
/**
* 宸ヤ綔钖勫璞�
@@ -292,7 +303,7 @@
// 鍒涘缓琛屽苟璁剧疆楂樺害
row = sheet.createRow(0);
- row.setHeight((short)(40 * 40));
+ row.setHeight((short)(30 *30));
// 鍒涘缓鍗曞厓鏍煎苟娣诲姞鍥剧墖
Cell cell = row.createCell(0);
@@ -301,11 +312,11 @@
// if (Files.exists(Paths.get(imagePath))) {
// byte[] imageBytes = Files.readAllBytes(Paths.get(imagePath));
// 灏哹yte鏁扮粍杞崲鎴恑nputstream锛屼互渚挎彃鍏ュ埌Excel涓�
- ClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, (short) cell.getColumnIndex(), cell.getRow().getRowNum(), (short) (cell.getColumnIndex() + 5),
+ ClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) cell.getColumnIndex(), cell.getRow().getRowNum(), (short) (cell.getColumnIndex() + 5),
cell.getRow().getRowNum() + 1);
// 璁$畻灞呬腑浣嶇疆
- int col1 = 1; // 涓棿鍒�
- int col2 = col1 + 5;
+ int col1 = 3; // 涓棿鍒�
+ int col2 = col1 + 2;
anchor.setCol1(col1);
anchor.setCol2(col2);
anchor.setRow1(0);
@@ -332,12 +343,13 @@
row = sheet.createRow(1);
sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 6));
Cell titleCell = row.createCell(0);
- titleCell.setCellValue("鍗峰唴鐩綍");
- // 璁剧疆鍔犵矖鍜屽眳涓牱寮�
+ titleCell.setCellValue("鍗� 鍐� 鐩� 褰�");
+ // 璁剧疆鍔犵矖銆佸畫浣撱��18鍙峰瓧浣撳拰灞呬腑鏍峰紡
CellStyle style = wb.createCellStyle();
Font font = wb.createFont();
font.setBold(true);
font.setFontName("瀹嬩綋");
+ font.setFontHeightInPoints((short) 18);
style.setFont(font);
style.setAlignment(HorizontalAlignment.CENTER);
titleCell.setCellStyle(style);
@@ -349,8 +361,11 @@
String recordId = list.get(index).getRecordId();
row.createCell(5).setCellValue(recordId);
row = sheet.createRow(3);
- column = 0;
+// // 娣诲姞鎬婚〉鏁板拰褰撳墠椤电爜淇℃伅
+// row.createCell(5).setCellValue("鎬婚〉鏁�: " + list.size() + ", 褰撳墠椤�: " + (index + 1));
+// row = sheet.createRow(4);
+ column = 0;
} else {
// 鏅�歴heet澶勭悊
// recordId = (DocumentMaterialsVo)list.get(0)
@@ -497,25 +512,43 @@
style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
style.setBorderBottom(BorderStyle.THIN);
style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
+ // 璁剧疆鑷姩鎹㈣
+ style.setWrapText(true);
Font dataFont = wb.createFont();
dataFont.setFontName("Arial");
dataFont.setFontHeightInPoints((short) 10);
style.setFont(dataFont);
styles.put("data", style);
- style = wb.createCellStyle();
- style.cloneStyleFrom(styles.get("data"));
- style.setAlignment(HorizontalAlignment.CENTER);
- style.setVerticalAlignment(VerticalAlignment.CENTER);
- style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
- style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
- Font headerFont = wb.createFont();
- headerFont.setFontName("Arial");
- headerFont.setFontHeightInPoints((short) 10);
- headerFont.setBold(true);
- headerFont.setColor(IndexedColors.WHITE.getIndex());
- style.setFont(headerFont);
- styles.put("header", style);
+ // 鍒涘缓琛ㄥご鏍峰紡鏄犲皠锛屾牴鎹瓻xcel娉ㄨВ鍔ㄦ�佺敓鎴�
+ Map<String, CellStyle> headerStyles = new HashMap<String, CellStyle>();
+ for (Object[] os : fields)
+ {
+ Excel excel = (Excel) os[1];
+ String key = StringUtils.format("header_{}_{}_{}_{}", excel.headerColor(), excel.headerBackgroundColor(), excel.headerFontName(), excel.headerFontBold());
+ if (!headerStyles.containsKey(key))
+ {
+ style = wb.createCellStyle();
+ style.cloneStyleFrom(styles.get("data"));
+ style.setAlignment(HorizontalAlignment.CENTER);
+ style.setVerticalAlignment(VerticalAlignment.CENTER);
+ // 鏍规嵁娉ㄨВ璁剧疆琛ㄥご鑳屾櫙鑹� - 宸叉敞閲婏紝琛ㄥご涓嶆樉绀鸿儗鏅壊
+ /*if (excel.headerBackgroundColor() != IndexedColors.WHITE) {
+ style.setFillForegroundColor(excel.headerBackgroundColor().index);
+ style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
+ }*/
+ // 璁剧疆琛ㄥご鑷姩鎹㈣
+ style.setWrapText(true);
+ Font headerFont = wb.createFont();
+ headerFont.setFontName(excel.headerFontName());
+ headerFont.setFontHeightInPoints((short) 12);
+ headerFont.setBold(excel.headerFontBold());
+ headerFont.setColor(excel.headerColor().index);
+ style.setFont(headerFont);
+ headerStyles.put(key, style);
+ }
+ }
+ styles.putAll(headerStyles);
style = wb.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);
@@ -524,21 +557,29 @@
totalFont.setFontName("Arial");
totalFont.setFontHeightInPoints((short) 10);
style.setFont(totalFont);
+ // 鎬昏琛屼篃璁剧疆鑷姩鎹㈣
+ style.setWrapText(true);
styles.put("total", style);
style = wb.createCellStyle();
style.cloneStyleFrom(styles.get("data"));
style.setAlignment(HorizontalAlignment.LEFT);
+ // 宸﹀榻愭牱寮忎篃璁剧疆鑷姩鎹㈣
+ style.setWrapText(true);
styles.put("data1", style);
style = wb.createCellStyle();
style.cloneStyleFrom(styles.get("data"));
style.setAlignment(HorizontalAlignment.CENTER);
+ // 灞呬腑瀵归綈鏍峰紡涔熻缃嚜鍔ㄦ崲琛�
+ style.setWrapText(true);
styles.put("data2", style);
style = wb.createCellStyle();
style.cloneStyleFrom(styles.get("data"));
style.setAlignment(HorizontalAlignment.RIGHT);
+ // 鍙冲榻愭牱寮忎篃璁剧疆鑷姩鎹㈣
+ style.setWrapText(true);
styles.put("data3", style);
return styles;
@@ -553,7 +594,9 @@
// 鍐欏叆鍒椾俊鎭�
cell.setCellValue(attr.name());
setDataValidation(attr, row, column);
- cell.setCellStyle(styles.get("header"));
+ // 鏍规嵁Excel娉ㄨВ鍔ㄦ�侀�夋嫨琛ㄥご鏍峰紡
+ String key = StringUtils.format("header_{}_{}_{}_{}", attr.headerColor(), attr.headerBackgroundColor(), attr.headerFontName(), attr.headerFontBold());
+ cell.setCellStyle(styles.get(key));
return cell;
}
@@ -570,7 +613,7 @@
} else if (Excel.ColumnType.NUMERIC == attr.cellType()) {
cell.setCellValue(StringUtils.contains(Convert.toStr(value), ".") ? Convert.toDouble(value) : Convert.toInt(value));
} else if (Excel.ColumnType.IMAGE == attr.cellType()) {
- ClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, (short) cell.getColumnIndex(), cell.getRow().getRowNum(), (short) (cell.getColumnIndex() + 1),
+ ClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) cell.getColumnIndex(), cell.getRow().getRowNum(), (short) (cell.getColumnIndex() + 1),
cell.getRow().getRowNum() + 1);
String imagePath = Convert.toStr(value);
if (StringUtils.isNotEmpty(imagePath)) {
@@ -617,12 +660,12 @@
// 濡傛灉璁剧疆浜嗘彁绀轰俊鎭垯榧犳爣鏀句笂鍘绘彁绀�.
if (StringUtils.isNotEmpty(attr.prompt())) {
// 杩欓噷榛樿璁句簡2-101鍒楁彁绀�.
- setXSSFPrompt(sheet, "", attr.prompt(), 1, 100, column, column);
+ setHSSFPrompt(sheet, "", attr.prompt(), 1, 100, column, column);
}
// 濡傛灉璁剧疆浜哻ombo灞炴�у垯鏈垪鍙兘閫夋嫨涓嶈兘杈撳叆
if (attr.combo().length > 0) {
// 杩欓噷榛樿璁句簡2-101鍒楀彧鑳介�夋嫨涓嶈兘杈撳叆.
- setXSSFValidation(sheet, attr.combo(), 1, 100, column, column);
+ setHSSFValidation(sheet, attr.combo(), 1, 100, column, column);
}
}
@@ -632,15 +675,23 @@
public Cell addCell(Excel attr, Row row, T vo, Field field, int column) {
Cell cell = null;
try {
- // 璁剧疆琛岄珮
- row.setHeight(maxHeight);
+ // 璁剧疆琛岄珮涓鸿嚜鍔ㄨ皟鏁�
+ row.setHeight((short) -1);
// 鏍规嵁Excel涓缃儏鍐靛喅瀹氭槸鍚﹀鍑�,鏈変簺鎯呭喌闇�瑕佷繚鎸佷负绌�,甯屾湜鐢ㄦ埛濉啓杩欎竴鍒�.
if (attr.isExport()) {
// 鍒涘缓cell
cell = row.createCell(column);
-// attr.align();
-// int align = attr.align().value;
-// cell.setCellStyle(styles.get("data" + (align >= 1 && align <= 3 ? align : "")));
+ // 璁剧疆鍗曞厓鏍兼牱寮�
+ HorizontalAlignment align = attr.align();
+ String styleKey = "data";
+ if (align == HorizontalAlignment.LEFT) {
+ styleKey = "data1";
+ } else if (align == HorizontalAlignment.CENTER) {
+ styleKey = "data2";
+ } else if (align == HorizontalAlignment.RIGHT) {
+ styleKey = "data3";
+ }
+ cell.setCellStyle(styles.get(styleKey));
// 鐢ㄤ簬璇诲彇瀵硅薄涓殑灞炴��
Object value = getTargetValue(vo, field, attr);
@@ -666,7 +717,7 @@
}
/**
- * 璁剧疆 POI XSSFSheet 鍗曞厓鏍兼彁绀�
+ * 璁剧疆 POI HSSFSheet 鍗曞厓鏍兼彁绀�
*
* @param sheet 琛ㄥ崟
* @param promptTitle 鎻愮ず鏍囬
@@ -676,7 +727,7 @@
* @param firstCol 寮�濮嬪垪
* @param endCol 缁撴潫鍒�
*/
- public void setXSSFPrompt(Sheet sheet, String promptTitle, String promptContent, int firstRow, int endRow,
+ public void setHSSFPrompt(Sheet sheet, String promptTitle, String promptContent, int firstRow, int endRow,
int firstCol, int endCol) {
DataValidationHelper helper = sheet.getDataValidationHelper();
DataValidationConstraint constraint = helper.createCustomConstraint("DD1");
@@ -698,7 +749,7 @@
* @param endCol 缁撴潫鍒�
* @return 璁剧疆濂界殑sheet.
*/
- public void setXSSFValidation(Sheet sheet, String[] textlist, int firstRow, int endRow, int firstCol, int endCol) {
+ public void setHSSFValidation(Sheet sheet, String[] textlist, int firstRow, int endRow, int firstCol, int endCol) {
DataValidationHelper helper = sheet.getDataValidationHelper();
// 鍔犺浇涓嬫媺鍒楄〃鍐呭
DataValidationConstraint constraint = helper.createExplicitListConstraint(textlist);
@@ -707,11 +758,9 @@
// 鏁版嵁鏈夋晥鎬у璞�
DataValidation dataValidation = helper.createValidation(constraint, regions);
// 澶勭悊Excel鍏煎鎬ч棶棰�
- if (dataValidation instanceof XSSFDataValidation) {
- dataValidation.setSuppressDropDownArrow(true);
- dataValidation.setShowErrorBox(true);
- } else {
+ if (dataValidation instanceof HSSFDataValidation) {
dataValidation.setSuppressDropDownArrow(false);
+ dataValidation.setShowErrorBox(true);
}
sheet.addValidationData(dataValidation);
@@ -908,7 +957,7 @@
* 鍒涘缓涓�涓伐浣滅翱
*/
public void createWorkbook() {
- this.wb = new SXSSFWorkbook(500);
+ this.wb = new HSSFWorkbook();
}
/**
@@ -977,4 +1026,4 @@
}
return val;
}
-}
+}
\ No newline at end of file
--
Gitblit v1.9.1