ソースを参照

2023 08 29 bug修改

zhuwei 2 年 前
コミット
0ae1c68976

+ 3 - 1
blade-service/blade-manager/src/main/java/org/springblade/manager/controller/ExcelTabController.java

@@ -294,7 +294,9 @@ public class ExcelTabController extends BladeController {
         String filecode = SnowFlakeUtil.getId() + "";
         String thmlUrl = file_path + filecode + ".html";
         String exceUrl = file_path + filecode + "123.xlsx";
-        FileUtils.excelInfo(file.getInputStream(),exceUrl,thmlUrl,"1");
+
+
+       // FileUtils.excelInfo(file.getInputStream(),exceUrl,thmlUrl,"1");
         // 上传excel文件
         BladeFile bladeFile = newIOSSClient.uploadFile(file.getOriginalFilename(),exceUrl);
         detail.setExtension(file.getOriginalFilename());

+ 1 - 1
blade-service/blade-manager/src/main/java/org/springblade/manager/service/impl/ExcelTabServiceImpl.java

@@ -619,7 +619,7 @@ public class ExcelTabServiceImpl extends BaseServiceImpl<ExcelTabMapper, ExcelTa
                 //上传新文件到文件服务器
                 //excel修改 同步修改html 对象
                 String thmlUrl = file_path + filecode + ".html";
-                FileUtils.excelInfo(inputStream,dataUrl,thmlUrl,"2");
+              //  FileUtils.excelInfo(inputStream,dataUrl,thmlUrl,"2");
                 BladeFile bladeFile = newIOSSClient.uploadFile(excelTab.getExtension(), dataUrl);
                 //获取文件大小
                int size = connection.getContentLength() / 1024 / 1024; //单位M

+ 198 - 20
blade-service/blade-manager/src/main/java/org/springblade/manager/utils/FileUtils.java

@@ -415,15 +415,22 @@ public class FileUtils {
     }
 
 
-    public static void main123(String[] args) throws Exception {
-       String excelUrl = "/Users/hongchuangyanfa/Downloads/C10.28隧道注浆施工记录表.xlsx";
+    public static void main(String[] args) throws Exception {
+       String excelUrl = "/Users/hongchuangyanfa/Downloads/D10.1表-隧道总体质量检验单 (1).xlsx";
         String old_html = "/Users/hongchuangyanfa/Desktop/pdf/old_html.html";
         String old_xlsx = "/Users/hongchuangyanfa/Desktop/pdf/old_html.xlsx";
 
-        File data = new File(excelUrl);
-        InputStream inputStream = new FileInputStream(data);
-        excelInfo(inputStream,old_xlsx,old_html,"1");
-
+   //     excelInfo2(excelUrl,old_xlsx,old_html);
+        com.spire.xls.Workbook wb = new com.spire.xls.Workbook();
+        wb.loadFromMHtml(old_xlsx);
+        Worksheet sheet = wb.getWorksheets().get(0);
+        CellRange[] cells = sheet.getCells();
+        for (int i = 0; i < cells.length; i++) {
+            CellRange oldcell = cells[i];
+            CellRange mergedCell = sheet.getCellRange(oldcell.getRow(), oldcell.getColumn());
+            String data = mergedCell.getDataValidation().getErrorMessage();
+            System.out.println(oldcell.getRow()+"--=--"+oldcell.getColumn()+"--=--"+data);
+        }
 
 /*        String new_html = "/Users/hongchuangyanfa/Desktop/pdf/new_html.html";
         String new_xlsx = "/Users/hongchuangyanfa/Desktop/pdf/new_html.xlsx";
@@ -434,6 +441,177 @@ public class FileUtils {
     }
 
 
+    /**
+     * 在线编辑excel 操作
+     *
+     * @param inExcelUrl
+     * @param excelURL
+     * @param htmlUrl
+     * @throws Exception
+     */
+    public static void excelInfo2(String inExcelUrl, String excelURL, String htmlUrl) {
+        try {
+            String file_path = FileUtils.getSysLocalFileUrl() + "/pdf/";
+            String filecode = SnowFlakeUtil.getId() + "";
+            String thmlUrl2 = file_path + filecode + "123.html";
+
+            // 解析原始excel
+            com.spire.xls.Workbook wb = new com.spire.xls.Workbook();
+            wb.loadFromMHtml(inExcelUrl);
+            // 操作
+            com.spire.xls.Workbook wb2 = new com.spire.xls.Workbook();
+            wb2.loadFromMHtml(inExcelUrl);
+
+            //获取工作表
+            Worksheet sheet = wb.getWorksheets().get(0);
+            Worksheet sheet2 = wb2.getWorksheets().get(0);
+            HTMLOptions options = new HTMLOptions();
+            options.setImageEmbedded(true);
+
+            sheet.saveToHtml(htmlUrl, options);
+            wb.saveToFile(excelURL, FileFormat.Version2013);
+
+
+            CellRange[] mergedCells = sheet.getMergedCells();
+            Map<String, Map<String, Integer>> xyList = new HashMap<>();
+
+            CellRange[] cellRanges = sheet.getCells();
+
+            int j = 0;
+            int maxVal = 0;
+
+            for (int i = 0; i < cellRanges.length; i++) {
+                CellRange oldcell = cellRanges[i];
+                CellRange mergedCell = sheet.getCellRange(oldcell.getRow(), oldcell.getColumn());
+                String data = mergedCell.getDataValidation().getErrorMessage();
+                int k = 0;
+                if(Func.isNumeric(data)){
+                    k = Func.toInt(data);
+                }
+                if (maxVal < k) {
+                        maxVal = k;
+                }
+            }
+
+            for (int i = 0; i < mergedCells.length; i++) {
+                Map<String, Integer> dataMap = new HashMap<>();
+                CellRange oldcell = mergedCells[i];
+                CellRange mergedCell = sheet.getCellRange(oldcell.getRow(), oldcell.getColumn());
+                String data = mergedCell.getDataValidation().getErrorMessage();
+                if (StringUtils.isEmpty(data)) {
+                    if(maxVal<=0){
+                        j = j + 1;
+                    }else{
+                        maxVal = maxVal+1;
+                        j=maxVal;
+                    }
+                } else {
+                    if(Func.isNumeric(data)){
+                        j = Func.toInt(data);
+                    }else {
+                        j = Func.toInt((data.trim().replaceAll("\r|\n", "")).split(":")[1] + "");
+                    }
+                }
+                // 目标表添加备注信息
+                sheet2.getCellRange(oldcell.getRow(), oldcell.getColumn()).getDataValidation().setErrorMessage(j+"");
+                mergedCell.getDataValidation().setErrorMessage(j+"");
+                oldcell.getDataValidation().setErrorMessage(j+"");
+                mergedCell.setText(j + "");
+                dataMap.put("y1", oldcell.getRow());
+                dataMap.put("y2", oldcell.getLastRow());
+                dataMap.put("x1", oldcell.getColumn());
+                dataMap.put("x2", oldcell.getLastColumn());
+                xyList.put(j + "", dataMap);
+            }
+
+
+            CellRange[] onCell = sheet.getCells();
+            // 单个cell
+           /* for (int i = 0; i < onCell.length; i++) {
+                CellRange oldcell = onCell[i];
+                CellRange mergedCell = sheet.getCellRange(oldcell.getRow(), oldcell.getColumn());
+                String data = mergedCell.getDataValidation().getErrorMessage();
+                Map<String, Integer> dataMap = new HashMap<>();
+                if (StringUtils.isEmpty(data)) {
+                    if(maxVal<=0){
+                        j = j + 1;
+                    }else{
+                        maxVal = maxVal+1;
+                        j=maxVal;
+                    }
+                    // null 需要添加坐标
+                    dataMap.put("y1", oldcell.getRow());
+                    dataMap.put("y2", oldcell.getLastRow());
+                    dataMap.put("x1", oldcell.getColumn());
+                    dataMap.put("x2", oldcell.getLastColumn());
+                    xyList.put(j + "", dataMap);
+                } else {
+                    if(Func.isNumeric(data)){
+                        j = Func.toInt(data);
+                    }else {
+                        j = Func.toInt((data.trim().replaceAll("\r|\n", "")).split(":")[1] + "");
+                    }
+                }
+                sheet2.getCellRange(oldcell.getRow(), oldcell.getColumn()).getDataValidation().setErrorMessage(j+"");
+                mergedCell.setText(j + "");
+            }*/
+            sheet.saveToHtml(thmlUrl2, options);
+
+            // 上传excel文件
+            wb2.saveToFile(excelURL, FileFormat.Version2013);
+
+            // 组装坐标
+            File html1 = new File(htmlUrl);  // 原始html
+            File html2 = new File(thmlUrl2); // 坐标html
+            InputStream inputStream1 = new FileInputStream(html1);
+            InputStream inputStream2 = new FileInputStream(html2);
+            String htmlString1 = IoUtil.readToString(inputStream1);
+            String htmlString2 = IoUtil.readToString(inputStream2);
+
+            org.jsoup.nodes.Document doc1 = Jsoup.parse(htmlString1);
+            Element table1 = doc1.select("table").first();
+            Elements trs1 = table1.select("tr");
+            org.jsoup.nodes.Document doc2 = Jsoup.parse(htmlString2);
+            Element table2 = doc2.select("table").first();
+            Elements trs2 = table2.select("tr");
+
+            for (int i = 0; i < trs1.size(); i++) {
+                Elements td1 = trs1.get(i).select("td");
+                Elements td2 = trs2.get(i).select("td");
+                for (int x = 0; x < td1.size(); x++) {
+                    Element cell1 = td1.get(x);
+                    /*if (cell1.children().size() >= 1) {
+                        String data = cell1.text();
+                        cell1.empty();
+                        cell1.text(data);
+                    }*/
+                    Element cell2 = td2.get(x);
+                    String html = cell2.text();
+                    Map<String, Integer> xyMap = xyList.get(html);
+                    if (xyMap != null) {
+                        cell1.attr("x1", xyMap.get("x1") + "");
+                        cell1.attr("x2", xyMap.get("x2") + "");
+                        cell1.attr("y1", xyMap.get("y1") + "");
+                        cell1.attr("y2", xyMap.get("y2") + "");
+                        cell1.attr("exceVal",html);
+                    }
+                }
+            }
+
+            File writeFile = new File(htmlUrl);
+            FileUtil.writeToFile(writeFile, doc1.html(), Boolean.parseBoolean("UTF-8"));
+            if (html2.exists()) {
+             //   html2.delete();
+            }
+            wb2.dispose();
+            wb.dispose();
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+
+
     /**
      * 在线编辑excel 操作
      *
@@ -442,7 +620,7 @@ public class FileUtils {
      * @param htmlUrl
      * @throws Exception
      */
-    public static void excelInfo(InputStream fileInputStream, String excelURL, String htmlUrl, String type) {
+    public static void excelInfo22222(InputStream fileInputStream, String excelURL, String htmlUrl, String type) {
         try {
             String file_path = FileUtils.getSysLocalFileUrl() + "/pdf/";
             String filecode = SnowFlakeUtil.getId() + "";
@@ -481,7 +659,7 @@ public class FileUtils {
                     k = Func.toInt(data);
                 }
                 if (maxVal < k) {
-                        maxVal = k;
+                    maxVal = k;
                 }
             }
 
@@ -508,10 +686,10 @@ public class FileUtils {
                 sheet2.getCellRange(oldcell.getRow(), oldcell.getColumn()).getDataValidation().setErrorMessage(j+"");
                 mergedCell.getDataValidation().setErrorMessage(j+"");
                 mergedCell.setText(j + "");
-                dataMap.put("x1", mergedCell.getRow());
-                dataMap.put("x2", mergedCell.getLastRow());
-                dataMap.put("y1", mergedCell.getColumn());
-                dataMap.put("y2", mergedCell.getLastColumn());
+                dataMap.put("y1", oldcell.getRow());
+                dataMap.put("y2", oldcell.getLastRow());
+                dataMap.put("x1", oldcell.getColumn());
+                dataMap.put("x2", oldcell.getLastColumn());
                 xyList.put(j + "", dataMap);
             }
 
@@ -528,6 +706,12 @@ public class FileUtils {
                         maxVal = maxVal+1;
                         j=maxVal;
                     }
+                    // null 需要添加坐标
+                    dataMap.put("y1", oldcell.getRow());
+                    dataMap.put("y2", oldcell.getLastRow());
+                    dataMap.put("x1", oldcell.getColumn());
+                    dataMap.put("x2", oldcell.getLastColumn());
+                    xyList.put(j + "", dataMap);
                 } else {
                     if(Func.isNumeric(data)){
                         j = Func.toInt(data);
@@ -536,13 +720,8 @@ public class FileUtils {
                     }
                 }
                 sheet2.getCellRange(oldcell.getRow(), oldcell.getColumn()).getDataValidation().setErrorMessage(j+"");
-                oldcell.getComment().getRichText().setText(j + "");
                 mergedCell.setText(j + "");
-                dataMap.put("x1", mergedCell.getRow());
-                dataMap.put("x2", mergedCell.getLastRow());
-                dataMap.put("y1", mergedCell.getColumn());
-                dataMap.put("y2", mergedCell.getLastColumn());
-                xyList.put(j + "", dataMap);
+
             }
             sheet.saveToHtml(thmlUrl2, options);
 
@@ -590,7 +769,7 @@ public class FileUtils {
             File writeFile = new File(htmlUrl);
             FileUtil.writeToFile(writeFile, doc1.html(), Boolean.parseBoolean("UTF-8"));
             if (html2.exists()) {
-               // html2.delete();
+                html2.delete();
             }
             fileInputStream.close();
             wb2.dispose();
@@ -600,5 +779,4 @@ public class FileUtils {
         }
     }
 
-
 }