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 | 488 +++++++++++++++++++++++++++++++++++++++++++++++------
1 files changed, 429 insertions(+), 59 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..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
@@ -14,12 +14,13 @@
import com.ruoyi.common.utils.file.ImageUtils;
import com.ruoyi.common.utils.reflect.ReflectUtils;
import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;
+import org.apache.poi.hssf.usermodel.*;
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.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 +59,14 @@
* 瀵煎嚭绫诲瀷锛圗XPORT:瀵煎嚭鏁版嵁锛汭MPORT锛氬鍏ユā鏉匡級
*/
private Excel.Type type;
+
+ public Workbook getWb() {
+ return wb;
+ }
+
+ public void setWb(Workbook wb) {
+ this.wb = wb;
+ }
/**
* 宸ヤ綔钖勫璞�
@@ -289,10 +298,36 @@
if(index == 1) {
// 鍚堝苟绗竴琛岀殑鍓�5涓崟鍏冩牸
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));
+ // 鑾峰彇椤甸潰璁剧疆
+ // 蹇呴』鍏堣缃负true
+ // 椤甸潰甯冨眬璁剧疆
+ 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, 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:$5"));
row = sheet.createRow(0);
- row.setHeight((short)(40 * 40));
+ row.setHeight((short) (37*20));
// 鍒涘缓鍗曞厓鏍煎苟娣诲姞鍥剧墖
Cell cell = row.createCell(0);
@@ -301,16 +336,22 @@
// 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),
- 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 = 1; // 涓棿鍒�
- int col2 = col1 + 5;
- anchor.setCol1(col1);
- anchor.setCol2(col2);
- anchor.setRow1(0);
- anchor.setRow2(1);
- // 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);
// anchor.set
// anchor.setHorizontallyCenter(true);
@@ -330,27 +371,54 @@
// log.error("娣诲姞鍥剧墖澶辫触: {}", e.getMessage());
// }
row = sheet.createRow(1);
+ row.setHeight((short) (32.5*20));
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);
row = sheet.createRow(2);
+ row.setHeight((short) (20*20));
//
- row.createCell(4).setCellValue("妗e彿:");
+ Cell dnumCell = row.createCell(4);
+ dnumCell.setCellValue("妗e彿:");
+ // 璁剧疆鍔犵矖銆佸畫浣撱��18鍙峰瓧浣撳拰灞呬腑鏍峰紡
+ CellStyle style1 = wb.createCellStyle();
+ Font font1 = wb.createFont();
+ font1.setBold(true);
+ font1.setFontName("瀹嬩綋");
+ font1.setFontHeightInPoints((short) 12);
+ style1.setFont(font1);
+ style1.setAlignment(HorizontalAlignment.RIGHT);
+ dnumCell.setCellStyle(style1);
// row.createCell(4).setCellValue("妗e彿:");
//鎷垮埌妗e彿
String recordId = list.get(index).getRecordId();
- row.createCell(5).setCellValue(recordId);
+ // 鍒涘缓甯︽湁涓嬪垝绾挎牱寮忕殑鍗曞厓鏍�
+ Cell recordIdCell = row.createCell(5);
+ recordIdCell.setCellValue(recordId);
+ // 鍒涘缓涓嬪垝绾挎牱寮�
+ CellStyle underlineStyle = wb.createCellStyle();
+ Font underlineFont = wb.createFont();
+ underlineFont.setFontName("瀹嬩綋");
+ underlineFont.setFontHeightInPoints((short) 12);
+ underlineFont.setUnderline(Font.U_SINGLE); // 璁剧疆涓嬪垝绾�
+ underlineStyle.setFont(underlineFont);
+ recordIdCell.setCellStyle(underlineStyle);
row = sheet.createRow(3);
+ row.setHeight((short) (17.5*20));
+ row = sheet.createRow(4);
+// // 娣诲姞鎬婚〉鏁板拰褰撳墠椤电爜淇℃伅
+// row.createCell(5).setCellValue("鎬婚〉鏁�: " + list.size() + ", 褰撳墠椤�: " + (index + 1));
+// row = sheet.createRow(4);
column = 0;
-
} else {
// 鏅�歴heet澶勭悊
// recordId = (DocumentMaterialsVo)list.get(0)
@@ -459,11 +527,19 @@
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 + 4 );
+ row = sheet.createRow(i + 5 );
else
row = sheet.createRow(i + 1 );
+ // 绗竴姝ワ細璁㏄OI鑷姩璁$畻閫傞厤鍐呭鐨勮楂�
+ // sheet.autoSizeRow(0);
+ // 绗簩姝ワ細鑾峰彇鑷姩璁$畻鍚庣殑琛岄珮
+
+ // row.setHeight((short) -1); // 鍏堣涓鸿嚜鍔紝鐢盿ddCell璁$畻鍚庤鐩�
+
// 寰楀埌瀵煎嚭瀵硅薄.
T vo = (T) list.get(i);
int column = 0;
@@ -473,10 +549,182 @@
// 璁剧疆瀹炰綋绫荤鏈夊睘鎬у彲璁块棶
field.setAccessible(true);
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;
+ }
/**
* 鍒涘缓琛ㄦ牸鏍峰紡
*
@@ -490,55 +738,81 @@
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.setFontHeightInPoints((short) 10);
+ dataFont.setFontName("瀹嬩綋");
+ dataFont.setFontHeightInPoints((short) 12);
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);
style.setVerticalAlignment(VerticalAlignment.CENTER);
Font totalFont = wb.createFont();
- totalFont.setFontName("Arial");
- totalFont.setFontHeightInPoints((short) 10);
+ totalFont.setFontName("瀹嬩綋");
+ totalFont.setFontHeightInPoints((short) 12);
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 +827,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 +846,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 +893,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 +908,30 @@
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";
+ }
+ System.out.println(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);
@@ -657,6 +948,17 @@
// 璁剧疆鍒楃被鍨�
setCellVo(value, attr, cell);
}
+
+
+ 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) {
@@ -666,7 +968,7 @@
}
/**
- * 璁剧疆 POI XSSFSheet 鍗曞厓鏍兼彁绀�
+ * 璁剧疆 POI HSSFSheet 鍗曞厓鏍兼彁绀�
*
* @param sheet 琛ㄥ崟
* @param promptTitle 鎻愮ず鏍囬
@@ -676,7 +978,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 +1000,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 +1009,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 +1208,7 @@
* 鍒涘缓涓�涓伐浣滅翱
*/
public void createWorkbook() {
- this.wb = new SXSSFWorkbook(500);
+ this.wb = new HSSFWorkbook();
}
/**
@@ -926,6 +1226,42 @@
} else {
wb.setSheetName(index, sheetName + index);
}
+ // 璁剧疆椤甸潰灞炴��
+ sheet.setFitToPage(true);
+ PrintSetup printSetup = sheet.getPrintSetup();
+ printSetup.setPaperSize(PrintSetup.A4_PAPERSIZE); // 璁剧疆涓篈4绾稿紶
+ printSetup.setLandscape(false); // 绾靛悜鎵撳嵃
+ printSetup.setFitWidth((short) 1); // 瀹藉害璋冩暣涓�1椤�
+ printSetup.setFitHeight((short) 0); // 0琛ㄧず楂樺害涓嶉檺鍒堕〉鏁帮紙鑷姩锛�
+ // 璁剧疆椤佃竟璺�
+ sheet.setMargin(Sheet.TopMargin, 1.0); // 澧炲姞椤堕儴杈硅窛浠ュ绾抽〉鐪�
+ sheet.setMargin(Sheet.BottomMargin, 0.5);
+ sheet.setMargin(Sheet.LeftMargin, 0.5);
+ sheet.setMargin(Sheet.RightMargin, 0.5);
+ sheet.setMargin(Sheet.HeaderMargin, 0.5); // 璁剧疆椤电湁杈硅窛
+ sheet.setMargin(Sheet.FooterMargin, 0.5); // 璁剧疆椤佃剼杈硅窛
+ // 璁剧疆鎵撳嵃鍖哄煙锛屼粠绗�0琛屽埌绗�1000琛岋紝绗�0鍒楀埌绗�20鍒楋紙鏍规嵁瀹為檯鎯呭喌璋冩暣锛�
+ //sheet.setPrintArea(0, 0, 20, 0, 1000);
+ // 璁剧疆椤电湁锛屾樉绀烘�婚〉鏁板拰褰撳墠椤电爜
+ if (wb instanceof HSSFWorkbook) {
+ HSSFWorkbook hssfWb = (HSSFWorkbook) wb;
+ HSSFSheet hssfSheet = hssfWb.getSheetAt(index);
+ HSSFHeader header = hssfSheet.getHeader();
+ // 璁剧疆椤电湁瀛椾綋
+ // header.setFontName("瀹嬩綋");
+ // header.setFontSize((short) 10);
+ // 璁剧疆椤电湁鍐呭
+ hssfSheet.setMargin(Sheet.HeaderMargin, 0.5);
+ header.setRight("鎬婚〉鏁�: &P, 褰撳墠椤�: &N");
+ // 璁剧疆椤电湁宸﹀榻愬拰鍙冲榻愬唴瀹癸紙鍙�夛級
+ header.setLeft("鏂囨。鏍囬");
+ header.setRight("&D &T"); // 鏃ユ湡鍜屾椂闂�
+ }
+ // 濡傛灉鏄痠ndex涓�1鐨剆heet锛岃缃《绔爣棰樿
+ if (index == 1) {
+ // 灏嗙1琛岋紙绱㈠紩涓�0锛夎缃负姣忛〉閲嶅鐨勬爣棰樿
+ sheet.setRepeatingRows(CellRangeAddress.valueOf("$1:$4"));
+ }
}
/**
* 鍒涘缓宸ヤ綔琛�
@@ -936,6 +1272,40 @@
this.sheet = wb.createSheet();
this.styles = createStyles(wb);
wb.setSheetName(index, sheetName);
+ // 鑾峰彇椤甸潰璁剧疆
+ // 蹇呴』鍏堣缃负true
+ sheet.setFitToPage(true);
+ PrintSetup printSetup = sheet.getPrintSetup();
+ printSetup.setPaperSize(PrintSetup.A4_PAPERSIZE); // 璁剧疆涓篈4绾稿紶
+ printSetup.setLandscape(false); // 绾靛悜鎵撳嵃
+ printSetup.setFitWidth((short) 1); // 瀹藉害璋冩暣涓�1椤�
+ printSetup.setFitHeight((short) 0); // 0琛ㄧず楂樺害涓嶉檺鍒堕〉鏁帮紙鑷姩锛�
+ // 璁剧疆椤佃竟璺�
+ sheet.setMargin(Sheet.TopMargin, 1.0); // 澧炲姞椤堕儴杈硅窛浠ュ绾抽〉鐪�
+ sheet.setMargin(Sheet.BottomMargin, 0.5);
+ sheet.setMargin(Sheet.LeftMargin, 0.5);
+ sheet.setMargin(Sheet.RightMargin, 0.5);
+ sheet.setMargin(Sheet.HeaderMargin, 0.5); // 璁剧疆椤电湁杈硅窛
+ sheet.setMargin(Sheet.FooterMargin, 0.5); // 璁剧疆椤佃剼杈硅窛
+ // 璁剧疆鎵撳嵃鍖哄煙锛屼粠绗�0琛屽埌绗�1000琛岋紝绗�0鍒楀埌绗�20鍒楋紙鏍规嵁瀹為檯鎯呭喌璋冩暣锛�
+ // sheet.setPrintArea(0, 0, 20, 0, 1000);
+ // 璁剧疆椤电湁锛屾樉绀烘�婚〉鏁板拰褰撳墠椤电爜
+ if (wb instanceof HSSFWorkbook) {
+ HSSFWorkbook hssfWb = (HSSFWorkbook) wb;
+ HSSFSheet hssfSheet = hssfWb.getSheetAt(index);
+ HSSFHeader header = hssfSheet.getHeader();
+ // 璁剧疆椤电湁瀛椾綋
+ // header.setFontName("瀹嬩綋");4
+ // header.setFontSize((short) 10);
+ // 璁剧疆椤电湁鍐呭
+ hssfSheet.setMargin(Sheet.HeaderMargin, 1.43);
+ header.setRight("&\"瀹嬩綋,Bold\"鍏� &N 椤� 绗� &P 椤�");
+ }
+ // 濡傛灉鏄痠ndex涓�1鐨剆heet锛岃缃《绔爣棰樿
+ if (index == 1) {
+ // 灏嗙1琛岋紙绱㈠紩涓�0锛夎缃负姣忛〉閲嶅鐨勬爣棰樿
+ sheet.setRepeatingRows(CellRangeAddress.valueOf("$1:$3"));
+ }
}
/**
* 鑾峰彇鍗曞厓鏍煎��
@@ -977,4 +1347,4 @@
}
return val;
}
-}
+}
\ No newline at end of file
--
Gitblit v1.9.1