cr 1 місяць тому
батько
коміт
85322d1251

+ 2 - 1
blade-service/blade-manager/src/main/java/org/springblade/manager/controller/WbsTreeContractController.java

@@ -51,6 +51,7 @@ import org.springblade.manager.service.impl.WbsTreeContractServiceImpl;
 import org.springblade.manager.utils.FileUtils;
 import org.springblade.manager.utils.RandomNumberHolder;
 import org.springblade.manager.vo.*;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.core.io.ByteArrayResource;
 import org.springframework.core.io.Resource;
 import org.springframework.http.HttpHeaders;
@@ -163,7 +164,7 @@ public class WbsTreeContractController extends BladeController {
     @PostMapping("/importPartitionCode")
     @ApiOperation("导入划分编码")
     @ApiOperationSupport(order = 31)
-    private R importPartitionCode(@RequestParam("file") MultipartFile file){
+    public R importPartitionCode(@RequestParam("file") MultipartFile file){
         return iWbsTreeContractService.importPartitionCode(file);
     }
 

+ 55 - 8
blade-service/blade-manager/src/main/java/org/springblade/manager/service/impl/WbsTreeContractServiceImpl.java

@@ -4473,7 +4473,6 @@ public class WbsTreeContractServiceImpl extends BaseServiceImpl<WbsTreeContractM
 
     @Override
     public ResponseEntity<Resource> exportTree(Long contractId, HttpServletResponse response) throws IOException, InvalidFormatException {
-
         String templatePath = "/mnt/sdc/Users/hongchuangyanfa/Desktop/excel/gcdcTemplate.xlsx";
         //String templatePath = "C:\\upload\\excel\\gcdc.xlsx";
         // 查询数据
@@ -4488,10 +4487,12 @@ public class WbsTreeContractServiceImpl extends BaseServiceImpl<WbsTreeContractM
         // 移除默认的Sheet1
         workbook.removeSheetAt(0);
 
-        // 创建居中的单元格样式
-        CellStyle centerStyle = workbook.createCellStyle();
-        centerStyle.setAlignment(HorizontalAlignment.CENTER);
-        centerStyle.setVerticalAlignment(VerticalAlignment.CENTER);
+        // 创建数据格式
+        DataFormat format = workbook.createDataFormat();
+
+        // 创建居中的单元格样式,并设置为文本格式
+        CellStyle centerStyle = createTextCellStyle(workbook, format);
+
 
         // 按单位工程分组
         Map<Long, List<WbsTreeContract>> unitProjectMap = list.stream()
@@ -4536,11 +4537,14 @@ public class WbsTreeContractServiceImpl extends BaseServiceImpl<WbsTreeContractM
 
             // 只隐藏pkeyId列(不保护,允许用户取消隐藏)
             hidePkeyIdColumnsOnly(sheet);
+
+            // 设置所有单元格为文本格式
+            setAllCellsAsText(sheet, centerStyle);
         }
 
-        // 保存文件到本地(本地测试放开,正式环境不需要)
-        // String outputPath = "C:\\upload\\excel\\111.xlsx";
-        //saveWorkbookToFile(workbook, outputPath);
+         //保存文件到本地(本地测试放开,正式环境不需要)
+//         String outputPath = "C:\\upload\\excel\\111.xlsx";
+//        saveWorkbookToFile(workbook, outputPath);
 
         // 同时返回给浏览器下载
         ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
@@ -4668,6 +4672,49 @@ public class WbsTreeContractServiceImpl extends BaseServiceImpl<WbsTreeContractM
         // 特别注意:不调用 sheet.protectSheet() 方法
         // 这样用户就可以自由调整列宽和取消隐藏列
     }
+    // 创建文本格式的单元格样式
+    private CellStyle createTextCellStyle(Workbook workbook, DataFormat format) {
+        CellStyle style = workbook.createCellStyle();
+        style.setAlignment(HorizontalAlignment.CENTER);
+        style.setVerticalAlignment(VerticalAlignment.CENTER);
+        style.setDataFormat(format.getFormat("@")); // 设置为文本格式
+        return style;
+    }
+
+    // 设置整个sheet的所有单元格为文本格式
+    private void setAllCellsAsText(Sheet sheet, CellStyle textStyle) {
+        // 遍历所有行
+        for (int rowNum = 0; rowNum <= sheet.getLastRowNum(); rowNum++) {
+            Row row = sheet.getRow(rowNum);
+            if (row == null) {
+                continue;
+            }
+
+            // 遍历行中的所有单元格
+            for (int colNum = 0; colNum < 15; colNum++) { // 假设有15列
+                Cell cell = row.getCell(colNum);
+                if (cell == null) {
+                    // 如果单元格不存在,创建它并设置文本格式
+                    cell = row.createCell(colNum);
+                    cell.setCellStyle(textStyle);
+                } else {
+                    // 如果单元格已存在,确保它有文本格式
+                    cell.setCellStyle(textStyle);
+
+                    // 如果单元格包含数字,确保它以文本格式存储
+                    if (cell.getCellType() == CellType.NUMERIC.getCode()) {
+                        double numericValue = cell.getNumericCellValue();
+                        // 检查是否为整数
+                        if (numericValue == Math.floor(numericValue)) {
+                            cell.setCellValue(String.valueOf((long) numericValue));
+                        } else {
+                            cell.setCellValue(String.valueOf(numericValue));
+                        }
+                    }
+                }
+            }
+        }
+    }
 
     // 创建表头行(两行)- 添加居中样式
     private void createHeaderRows(Sheet sheet, Sheet templateSheet, CellStyle centerStyle) {