Browse Source

客户端导入excel bug

liuyc 2 năm trước cách đây
mục cha
commit
68790e02a5

+ 25 - 4
blade-service/blade-manager/src/main/java/org/springblade/manager/controller/WbsTreeContractController.java

@@ -22,7 +22,6 @@ import org.springblade.manager.entity.WbsTreeContract;
 import org.springblade.manager.feign.ContractClient;
 import org.springblade.manager.service.IWbsTreeContractService;
 import org.springblade.manager.service.impl.WbsTreeContractServiceImpl;
-import org.springblade.manager.utils.CssSelectorEscaper;
 import org.springblade.manager.utils.ExcelParser;
 import org.springblade.manager.vo.*;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -249,8 +248,30 @@ public class WbsTreeContractController extends BladeController {
             if (excelTab != null) {
                 InputStream inputStream = CommonUtil.getOSSInputStream(excelTab.getFileUrl());
                 if (inputStream != null) {
-                    try (/*Workbook workbook = parseExcelFile(inputStream) 先不解析成文本格式,保留原始的excel表格格式*/
-                            Workbook workbook = WorkbookFactory.create(inputStream)) {
+                    response.setContentType("application/vnd.ms-excel;charset=UTF-8");
+                    response.setCharacterEncoding("UTF-8");
+                    response.setHeader("Content-disposition", ";filename=" + URLEncoder.encode(excelTab.getName().replace(" ", ""), "UTF-8") + ".xlsx");
+                    ServletOutputStream servletOutputStream = response.getOutputStream();
+                    byte[] buffer = new byte[4096];
+                    int bytesRead;
+                    while ((bytesRead = inputStream.read(buffer)) != -1) {
+                        servletOutputStream.write(buffer, 0, bytesRead);
+                    }
+                    servletOutputStream.flush();
+                    inputStream.close();
+                }
+            }
+        }
+    }
+
+    /*public void downloadExcel(@RequestParam String pKeyId, HttpServletResponse response) throws Exception {
+        WbsTreeContract tab = iWbsTreeContractService.getBaseMapper().selectOne(Wrappers.<WbsTreeContract>lambdaQuery().eq(WbsTreeContract::getPKeyId, pKeyId));
+        if (tab != null && Objects.nonNull(tab.getExcelId())) {
+            ExcelTab excelTab = jdbcTemplate.queryForObject("select file_url,name from m_excel_tab where id = " + tab.getExcelId(), new BeanPropertyRowMapper<>(ExcelTab.class));
+            if (excelTab != null) {
+                InputStream inputStream = CommonUtil.getOSSInputStream(excelTab.getFileUrl());
+                if (inputStream != null) {
+                    try (Workbook workbook = parseExcelFile(inputStream)) {
                         response.setContentType("application/vnd.ms-excel;charset=UTF-8");
                         response.setCharacterEncoding("UTF-8");
                         response.setHeader("Content-disposition", ";filename=" + URLEncoder.encode(excelTab.getName().replace(" ", ""), "UTF-8") + ".xlsx");
@@ -281,7 +302,7 @@ public class WbsTreeContractController extends BladeController {
             }
         }
         return workbook;
-    }
+    }*/
 
     @PostMapping("/import-excel")
     @ApiOperationSupport(order = 14)

+ 12 - 3
blade-service/blade-manager/src/main/java/org/springblade/manager/utils/ExcelParser.java

@@ -7,15 +7,17 @@ import org.springframework.web.multipart.MultipartFile;
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.text.DecimalFormat;
 import java.text.NumberFormat;
 import java.util.LinkedHashMap;
 import java.util.Map;
 
 public class ExcelParser {
 
-    private static NumberFormat numberFormat = NumberFormat.getNumberInstance();
+    private static final NumberFormat numberFormat = NumberFormat.getNumberInstance();
 
     static {
+        //格式化后不包含千位分隔符
         numberFormat.setGroupingUsed(false);
     }
 
@@ -48,9 +50,14 @@ public class ExcelParser {
             case BOOLEAN:
                 return String.valueOf(cell.getBooleanCellValue());
             case FORMULA:
+                //公式单元格
                 return evaluateFormulaCell(cell);
             case NUMERIC:
+                //浮点数单元格
                 double d = cell.getNumericCellValue();
+                if (d == 0.0) {
+                    return "";
+                }
                 value = numberFormat.format(d);
                 return value.toString();
             default:
@@ -64,14 +71,16 @@ public class ExcelParser {
         Object value;
         if (cellValue == null) return "";
         switch (cellValue.getCellTypeEnum()) {
+            //解决公式内部是浮点数问题
             case NUMERIC:
                 double d = cell.getNumericCellValue();
+                if (d == 0.0) {
+                    return "";
+                }
                 value = numberFormat.format(d);
                 return value.toString();
             case STRING:
                 return cell.getStringCellValue();
-            case BOOLEAN:
-                return String.valueOf(cell.getBooleanCellValue());
             default:
                 return "";
         }