Jelajahi Sumber

修改单导出

DLC 3 minggu lalu
induk
melakukan
fb461aea54

+ 28 - 2
qcs-common/src/main/java/com/rongwei/bscommon/sys/service/impl/QcsMdfFormServiceImpl.java

@@ -14,8 +14,7 @@ import com.rongwei.rwcommon.base.R;
 import com.rongwei.rwcommon.base.exception.CustomException;
 import com.rongwei.rwcommon.utils.SecurityUtil;
 import com.rongwei.rwcommon.utils.StringUtils;
-import org.apache.poi.ss.usermodel.Row;
-import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.*;
 import org.apache.poi.ss.util.CellRangeAddress;
 import org.apache.poi.xssf.usermodel.XSSFCell;
 import org.apache.poi.xssf.usermodel.XSSFRow;
@@ -871,6 +870,8 @@ public class QcsMdfFormServiceImpl extends ServiceImpl<QcsMdfFormDao, QcsMdfForm
             cellRangeAddress = new CellRangeAddress(lastRowIndex, lastRowIndex, 44, 57);
             sheet.addMergedRegion(cellRangeAddress);
 
+            autoAdjustRowHeight(sheet, 900);
+
             workbook.removeSheetAt(0);
 
             // 4. 导出文件
@@ -910,4 +911,29 @@ public class QcsMdfFormServiceImpl extends ServiceImpl<QcsMdfFormDao, QcsMdfForm
     private BigDecimal sumTotal(QcsMdfDetailDo mdfDetailDo) {
         return (mdfDetailDo.getManday() == null ? BigDecimal.valueOf(0) : mdfDetailDo.getManday()).add((mdfDetailDo.getMater() == null ? BigDecimal.valueOf(0) : mdfDetailDo.getMater())).add((mdfDetailDo.getEquip() == null ? BigDecimal.valueOf(0) : mdfDetailDo.getEquip())).add((mdfDetailDo.getOther() == null ? BigDecimal.valueOf(0) : mdfDetailDo.getOther()));
     }
+
+    // 在写入数据后调用此方法
+    public static void autoAdjustRowHeight(Sheet sheet, int maxColumnWidth) {
+        for (Row row : sheet) {
+            for (Cell cell : row) {
+                if (cell.getCellType() == CellType.STRING) {
+                    CellStyle style = cell.getCellStyle();
+                    if (style.getWrapText()) {
+                        // 计算文本需要的行数
+                        Font font = sheet.getWorkbook().getFontAt(style.getFontIndex());
+                        String text = cell.getStringCellValue();
+                        int textLength = text.length();
+                        double charWidth = font.getFontHeightInPoints() / 2.0; // 近似字符宽度
+                        int lineCount = (int) Math.ceil((textLength * charWidth) / maxColumnWidth);
+
+                        // 设置行高(每行约20个像素)
+                        row.setHeightInPoints(Math.max(
+                                row.getHeightInPoints(),
+                                lineCount * 20
+                        ));
+                    }
+                }
+            }
+        }
+    }
 }

TEMPAT SAMPAH
qcs-server/src/main/resources/template/修改单导出模板.xlsx