From d4afabe6cb07b4129f995c82641189b2256b15a7 Mon Sep 17 00:00:00 2001
From: liding <756868258@qq.com>
Date: 星期一, 16 三月 2026 14:50:51 +0800
Subject: [PATCH] 1.耗材模块 2.出入库磅单
---
src/main/java/com/ruoyi/stock/word/WeighbridgeDocGenerator.java | 244 +++++++++++++++++++++++++++++++++++++++++-------
1 files changed, 206 insertions(+), 38 deletions(-)
diff --git a/src/main/java/com/ruoyi/stock/word/WeighbridgeDocGenerator.java b/src/main/java/com/ruoyi/stock/word/WeighbridgeDocGenerator.java
index feafdd0..f13a3d5 100644
--- a/src/main/java/com/ruoyi/stock/word/WeighbridgeDocGenerator.java
+++ b/src/main/java/com/ruoyi/stock/word/WeighbridgeDocGenerator.java
@@ -5,6 +5,7 @@
import com.ruoyi.stock.dto.StockInRecordDto;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.poi.xwpf.usermodel.XWPFTable.XWPFBorderType;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@@ -13,6 +14,7 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
+import java.math.BigInteger;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@@ -61,7 +63,7 @@
document.write(out);
out.flush();
- // 杩斿洖缁濆璺緞锛堢敤浜庢暟鎹簱瀛樺偍锛�
+ // 杩斿洖缁濆璺緞
return absolutePath;
} catch (Exception e) {
@@ -84,9 +86,22 @@
/**
* 鏋勫缓鏂囨。鍐呭
+ * @param document Word鏂囨。瀵硅薄
+ * @param dto 鍏ュ簱璁板綍DTO
*/
private void buildDocumentContent(XWPFDocument document, StockInRecordDto dto) {
- // 1. 娣诲姞鏍囬 "纾呯爜鍗�" - 浣跨敤榛戜綋
+
+ // 璁剧疆绾稿紶澶у皬涓篈5妯悜
+ CTSectPr sectPr = document.getDocument().getBody().isSetSectPr()
+ ? document.getDocument().getBody().getSectPr()
+ : document.getDocument().getBody().addNewSectPr();
+
+ CTPageSz pageSize = sectPr.isSetPgSz() ? sectPr.getPgSz() : sectPr.addNewPgSz();
+ pageSize.setW(BigInteger.valueOf(11906)); // A5妯悜瀹藉害 210mm
+ pageSize.setH(BigInteger.valueOf(8392)); // A5妯悜楂樺害 148mm
+ pageSize.setOrient(STPageOrientation.LANDSCAPE);
+
+ // 鏍囬 "纾呯爜鍗�"
XWPFParagraph titlePara = document.createParagraph();
titlePara.setAlignment(ParagraphAlignment.CENTER);
titlePara.setSpacingAfter(200);
@@ -97,22 +112,71 @@
titleRun.setFontSize(28);
titleRun.setFontFamily("榛戜綋");
- // 2. 绌轰竴琛�
- document.createParagraph();
+ // 瀹氫箟缁熶竴鐨勯〉闈㈠搴﹀�硷紙鎵�鏈夎〃鏍间娇鐢ㄧ浉鍚岀殑鎬诲搴︼級
+ int pageWidth = 9000; // 缁熶竴鐨勯〉闈㈠搴﹀��
- // 3. 澶撮儴淇℃伅锛堝勾鏈堟棩鍜岃閲忓崟浣嶅悎骞跺湪涓�琛岋級- 浣跨敤瀹嬩綋
- XWPFParagraph headerPara = document.createParagraph();
- headerPara.setSpacingAfter(200);
- XWPFRun headerRun = headerPara.createRun();
+ // 鍒涘缓涓�琛屼袱鍒楄〃鏍兼潵鏀剧疆鏃ユ湡鍜岃閲忓崟浣�
+ XWPFTable headerTable = document.createTable(1, 2);
+ headerTable.setWidth("100%");
- // 鏍煎紡鍖栨棩鏈熶负 yyyy-MM-dd
+ // 璁剧疆琛ㄦ牸鏃犺竟妗�
+ headerTable.setInsideHBorder(XWPFBorderType.NONE, 0, 0, "000000");
+ headerTable.setInsideVBorder(XWPFBorderType.NONE, 0, 0, "000000");
+ headerTable.setTopBorder(XWPFBorderType.NONE, 0, 0, "000000");
+ headerTable.setBottomBorder(XWPFBorderType.NONE, 0, 0, "000000");
+ headerTable.setLeftBorder(XWPFBorderType.NONE, 0, 0, "000000");
+ headerTable.setRightBorder(XWPFBorderType.NONE, 0, 0, "000000");
+
+ // 璁剧疆琛ㄥご琛ㄦ牸鍒楀锛氭棩鏈熷垪50%锛岃閲忓崟浣嶅垪50%
+ CTTbl headerTbl = headerTable.getCTTbl();
+ CTTblGrid headerTblGrid = headerTbl.addNewTblGrid();
+
+ // 浣跨敤缁熶竴鐨勯〉闈㈠搴�
+ int headerColWidth = pageWidth / 2; // 4500
+
+ CTTblGridCol col1 = headerTblGrid.addNewGridCol();
+ col1.setW(BigInteger.valueOf(headerColWidth));
+
+ CTTblGridCol col2 = headerTblGrid.addNewGridCol();
+ col2.setW(BigInteger.valueOf(headerColWidth));
+
+ // 璁剧疆琛岄珮
+ headerTable.getRow(0).setHeight(300);
+
+ // 涓鸿〃澶磋〃鏍肩殑姣忎釜鍗曞厓鏍艰缃搴�
+ for (int colIndex = 0; colIndex < 2; colIndex++) {
+ XWPFTableCell cell = headerTable.getRow(0).getCell(colIndex);
+ CTTcPr tcPr = cell.getCTTc().addNewTcPr();
+ CTTblWidth cellWidth = tcPr.addNewTcW();
+ cellWidth.setType(STTblWidth.DXA);
+ cellWidth.setW(BigInteger.valueOf(headerColWidth));
+ }
+
+ // 宸︿晶鍗曞厓鏍硷細鏃ユ湡锛堝乏瀵归綈锛�
+ XWPFTableCell dateCell = headerTable.getRow(0).getCell(0);
+ dateCell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
+
+ // 娓呴櫎鍗曞厓鏍间腑鍙兘瀛樺湪鐨勯粯璁ゆ钀�
+ for (int i = dateCell.getParagraphs().size() - 1; i > 0; i--) {
+ dateCell.removeParagraph(i);
+ }
+
+ XWPFParagraph datePara = dateCell.getParagraphs().get(0);
+ datePara.setAlignment(ParagraphAlignment.LEFT);
+
+ // 娓呴櫎娈佃惤涓彲鑳藉瓨鍦ㄧ殑榛樿Run
+ for (int i = datePara.getRuns().size() - 1; i > 0; i--) {
+ datePara.removeRun(i);
+ }
+
+ XWPFRun dateRun = datePara.createRun();
+
+ // 鏍煎紡鍖栨棩鏈�
String weighDate = "";
if (dto.getWeighingDate() != null) {
try {
- // 鍋囪 weighingDate 鏄� LocalDateTime 绫诲瀷
weighDate = dto.getWeighingDate().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
} catch (Exception e) {
- // 濡傛灉鏄瓧绗︿覆绫诲瀷锛屽皾璇曡浆鎹�
weighDate = dto.getWeighingDate().toString();
if (weighDate.length() > 10) {
weighDate = weighDate.substring(0, 10);
@@ -120,17 +184,37 @@
}
}
- headerRun.setText("骞存湀鏃ワ細" + weighDate + " 璁¢噺鍗曚綅锛氾紙鍚級");
- headerRun.setFontSize(12);
- headerRun.setFontFamily("瀹嬩綋");
+ dateRun.setText("鏃� 鏈燂細" + weighDate);
+ dateRun.setFontSize(12);
+ dateRun.setFontFamily("瀹嬩綋");
- // 4. 鍒涘缓涓昏〃鏍� - 4琛�6鍒楋紙琛ㄥご1琛�+鏁版嵁1琛�+鍚堣1琛�+绌鸿1琛岋級
- XWPFTable table = document.createTable(4, 6);
+ // 璁¢噺鍗曚綅锛堝彸瀵归綈锛�
+ XWPFTableCell unitCell = headerTable.getRow(0).getCell(1);
+ unitCell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
+
+ for (int i = unitCell.getParagraphs().size() - 1; i > 0; i--) {
+ unitCell.removeParagraph(i);
+ }
+
+ XWPFParagraph unitPara = unitCell.getParagraphs().get(0);
+ unitPara.setAlignment(ParagraphAlignment.RIGHT);
+
+ for (int i = unitPara.getRuns().size() - 1; i > 0; i--) {
+ unitPara.removeRun(i);
+ }
+
+ XWPFRun unitRun = unitPara.createRun();
+ unitRun.setText("璁¢噺鍗曚綅锛氾紙鍚級");
+ unitRun.setFontSize(12);
+ unitRun.setFontFamily("瀹嬩綋");
+
+ // 鍒涘缓涓昏〃鏍� - 3琛�6鍒�
+ XWPFTable table = document.createTable(3, 6);
// 璁剧疆琛ㄦ牸瀹藉害
table.setWidth("100%");
- // 璁剧疆琛ㄦ牸杈规鍔犵矖锛堣竟妗嗗搴﹁涓�2锛�
+ // 璁剧疆琛ㄦ牸杈规鍔犵矖
table.setInsideHBorder(XWPFBorderType.SINGLE, 2, 0, "000000");
table.setInsideVBorder(XWPFBorderType.SINGLE, 2, 0, "000000");
table.setTopBorder(XWPFBorderType.SINGLE, 2, 0, "000000");
@@ -142,7 +226,47 @@
table.getRow(0).setHeight(400);
table.getRow(1).setHeight(400);
table.getRow(2).setHeight(400);
- table.getRow(3).setHeight(400);
+
+ // 璁剧疆涓昏〃鏍煎垪瀹界綉鏍� - 浣跨敤鐩稿悓鐨勬�诲搴� pageWidth
+ CTTbl mainTbl = table.getCTTbl();
+ CTTblGrid mainTblGrid = mainTbl.addNewTblGrid();
+
+ // 鍒楀姣斾緥
+ int[] colWidths = {
+ (int)(pageWidth * 0.20),
+ (int)(pageWidth * 0.24),
+ (int)(pageWidth * 0.14),
+ (int)(pageWidth * 0.14),
+ (int)(pageWidth * 0.14),
+ (int)(pageWidth * 0.14)
+ };
+
+ // 纭繚鎬诲拰绛変簬pageWidth
+ int sum = 0;
+ for (int width : colWidths) {
+ sum += width;
+ }
+ // 濡傛灉鏈夎宸紝璋冩暣鏈�鍚庝竴鍒�
+ if (sum != pageWidth) {
+ colWidths[5] += (pageWidth - sum);
+ }
+
+ for (int width : colWidths) {
+ CTTblGridCol col = mainTblGrid.addNewGridCol();
+ col.setW(BigInteger.valueOf(width));
+ }
+
+ // 涓烘瘡涓�琛岀殑姣忎釜鍗曞厓鏍艰缃叿浣撶殑瀹藉害
+ for (int rowIndex = 0; rowIndex < 3; rowIndex++) {
+ XWPFTableRow row = table.getRow(rowIndex);
+ for (int colIndex = 0; colIndex < 6; colIndex++) {
+ XWPFTableCell cell = row.getCell(colIndex);
+ CTTcPr tcPr = cell.getCTTc().addNewTcPr();
+ CTTblWidth cellWidth = tcPr.addNewTcW();
+ cellWidth.setType(STTblWidth.DXA);
+ cellWidth.setW(BigInteger.valueOf(colWidths[colIndex]));
+ }
+ }
// 璁剧疆琛ㄥご - 浣跨敤榛戜綋鍔犵矖
String[] headers = {"杞﹀彿", "鍝佸悕", "姣涢噸", "鐨噸", "鍑�閲�", "澶囨敞"};
@@ -152,8 +276,18 @@
cell.setText(headers[i]);
cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
+ // 娓呴櫎鍙兘瀛樺湪鐨勫浣欐钀�
+ while (cell.getParagraphs().size() > 1) {
+ cell.removeParagraph(1);
+ }
+
XWPFParagraph cellPara = cell.getParagraphs().get(0);
cellPara.setAlignment(ParagraphAlignment.CENTER);
+
+ while (cellPara.getRuns().size() > 1) {
+ cellPara.removeRun(1);
+ }
+
XWPFRun cellRun = cellPara.getRuns().get(0);
cellRun.setBold(true);
cellRun.setFontSize(12);
@@ -180,8 +314,17 @@
cell.setText(data[i]);
cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
+ while (cell.getParagraphs().size() > 1) {
+ cell.removeParagraph(1);
+ }
+
XWPFParagraph cellPara = cell.getParagraphs().get(0);
cellPara.setAlignment(ParagraphAlignment.CENTER);
+
+ while (cellPara.getRuns().size() > 1) {
+ cellPara.removeRun(1);
+ }
+
XWPFRun cellRun = cellPara.getRuns().get(0);
cellRun.setFontSize(12);
cellRun.setFontFamily("瀹嬩綋");
@@ -194,43 +337,68 @@
XWPFTableCell totalLabelCell = totalRow.getCell(0);
totalLabelCell.setText("鍚堣锛堝ぇ鍐欙級");
totalLabelCell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
+
+ // 娓呴櫎鍙兘瀛樺湪鐨勫浣欐钀�
+ while (totalLabelCell.getParagraphs().size() > 1) {
+ totalLabelCell.removeParagraph(1);
+ }
+
XWPFParagraph totalLabelPara = totalLabelCell.getParagraphs().get(0);
totalLabelPara.setAlignment(ParagraphAlignment.CENTER);
+
+ // 娓呴櫎鍙兘瀛樺湪鐨勫浣橰un
+ while (totalLabelPara.getRuns().size() > 1) {
+ totalLabelPara.removeRun(1);
+ }
+
XWPFRun totalLabelRun = totalLabelPara.getRuns().get(0);
totalLabelRun.setBold(true);
totalLabelRun.setFontSize(12);
- totalLabelRun.setFontFamily("瀹嬩綋");
+ totalLabelRun.setFontFamily("榛戜綋");
-
- XWPFTableCell contentCell = totalRow.getCell(1);
+ // 灏嗗噣閲嶈浆鎹负澶у啓
String netWeightChinese = ChineseNumberUtil.numberToChinese(
dto.getNetWeight() != null ? dto.getNetWeight() : BigDecimal.ZERO
);
- contentCell.setText(netWeightChinese + "鍚�");
- contentCell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
- XWPFParagraph contentPara = contentCell.getParagraphs().get(0);
- contentPara.setAlignment(ParagraphAlignment.LEFT);
- XWPFRun contentRun = contentPara.getRuns().get(0);
- contentRun.setFontSize(12);
- contentRun.setFontFamily("瀹嬩綋");
- // 鍚堝苟绗�3-6鍒楀埌绗�2鍒�
- for (int i = 2; i < 6; i++) {
- XWPFTableCell cell = totalRow.getCell(i);
- // 璁剧疆杩欎簺鍗曞厓鏍间负鍚堝苟鐘舵�侊紙缁ф壙鑷2鍒楋級
- cell.getCTTc().addNewTcPr().addNewHMerge().setVal(org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge.CONTINUE);
+ XWPFTableCell startCell = totalRow.getCell(1);
+ startCell.setText(netWeightChinese + "鍚�");
+ startCell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
+
+ while (startCell.getParagraphs().size() > 1) {
+ startCell.removeParagraph(1);
}
- // 6. 鍒涘缓杩囩鍛樿锛堝崟鐙竴琛岋紝宸﹀榻愶級
+ XWPFParagraph startPara = startCell.getParagraphs().get(0);
+ startPara.setAlignment(ParagraphAlignment.CENTER);
+
+ while (startPara.getRuns().size() > 1) {
+ startPara.removeRun(1);
+ }
+
+ XWPFRun startRun = startPara.getRuns().get(0);
+ startRun.setFontSize(12);
+ startRun.setFontFamily("瀹嬩綋");
+
+ // 鏍囪绗�2鍒椾负鍚堝苟璧峰
+ startCell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
+
+ // 绗�3-6鍒楀悎骞�
+ for (int i = 2; i < 6; i++) {
+ XWPFTableCell cell = totalRow.getCell(i);
+ cell.setText("");
+ cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
+ }
+
+ // 鍒涘缓杩囩鍛樿锛堝崟鐙竴琛岋紝宸﹀榻愶級
XWPFParagraph operatorPara = document.createParagraph();
operatorPara.setSpacingAfter(100);
operatorPara.setAlignment(ParagraphAlignment.LEFT);
XWPFRun operatorRun = operatorPara.createRun();
String operator = dto.getWeighingOperator() != null ? dto.getWeighingOperator() : "";
operatorRun.setText("杩囩鍛橈細" + operator);
- operatorRun.setFontSize(8);
+ operatorRun.setFontSize(12);
operatorRun.setFontFamily("瀹嬩綋");
-
}
/**
@@ -242,10 +410,10 @@
String month = String.format("%02d", now.getMonthValue());
String day = String.format("%02d", now.getDayOfMonth());
- // 鏂囦欢鍚嶆牸寮忥細纾呭崟_杞︾墝鍙穇骞存湀鏃ユ椂鍒嗙.docx
+ // 鏂囦欢鍚嶆牸寮忥細纾呭崟_杞︾墝鍙穇骞存湀鏃�.docx
String fileName = String.format("纾呭崟_%s_%s.docx",
dto.getLicensePlateNo() != null ? dto.getLicensePlateNo() : "鏈煡杞︾墝",
- now.format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")));
+ now.format(DateTimeFormatter.ofPattern("yyyyMMdd")));
return uploadDir + File.separator + year + File.separator + month + File.separator + day + File.separator + fileName;
}
--
Gitblit v1.9.3