Explorar el Código

2023 08 30 bug修改

zhuwei hace 2 años
padre
commit
c3ebde7f5b

+ 13 - 7
blade-service/blade-manager/src/main/java/org/springblade/manager/controller/ExcelTabController.java

@@ -48,6 +48,7 @@ import org.springblade.manager.enums.ExecuteType;
 import org.springblade.manager.mapper.ExcelTabMapper;
 import org.springblade.manager.mapper.WbsTreePrivateMapper;
 import org.springblade.manager.service.*;
+import org.springblade.manager.utils.ExcelInfoUtils;
 import org.springblade.manager.utils.FileUtils;
 import org.springblade.manager.utils.RegularExpressionUtil;
 import org.springblade.manager.utils.WbsElementUtil;
@@ -294,13 +295,18 @@ 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");
+        //ExcelInfoUtils.excelInfo(file.getInputStream(),exceUrl,thmlUrl,"1");
         // 上传excel文件
-        BladeFile bladeFile = newIOSSClient.uploadFile(file.getOriginalFilename(),exceUrl);
+       // BladeFile bladeFile = newIOSSClient.uploadFile(file.getOriginalFilename(),exceUrl);
+        // 解析原始excel
+        Workbook wb = new Workbook();
+        wb.loadFromMHtml(file.getInputStream());
+        Worksheet sheet = wb.getWorksheets().get(0);
+        sheet.saveToHtml(thmlUrl);
+
+        BladeFile bladeFileR = newIOSSClient.uploadFileByInputStream(file);
         detail.setExtension(file.getOriginalFilename());
-        detail.setFileUrl(bladeFile.getLink());
+        detail.setFileUrl(bladeFileR.getLink());
         detail.setFileType(3); // 表示为清表信息  1 表示祖节点  2 表示为节点信息 3 表示清表
         detail.setHtmlUrl(thmlUrl);
         excelTabService.saveOrUpdate(detail);
@@ -840,7 +846,7 @@ public class ExcelTabController extends BladeController {
 
 
     // 上传解析 html
-    public void expailHtmlInfo(String thmlUrl, Long excelId) throws Exception {
+    public void expailHtmlInfo12231(String thmlUrl, Long excelId) throws Exception {
 
         // 读取
         File file1 = ResourceUtil.getFile(thmlUrl);
@@ -1149,7 +1155,7 @@ public class ExcelTabController extends BladeController {
     }
 
     // 上传解析 html
-    public void expailHtmlInfo1111111(String thmlUrl, Long excelId) throws Exception {
+    public void expailHtmlInfo(String thmlUrl, Long excelId) throws Exception {
 
         // 读取
         File file1 = ResourceUtil.getFile(thmlUrl);

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

@@ -58,6 +58,7 @@ import org.springblade.manager.formula.NodeTable;
 import org.springblade.manager.formula.impl.TableElementConverter;
 import org.springblade.manager.mapper.ExcelTabMapper;
 import org.springblade.manager.service.*;
+import org.springblade.manager.utils.ExcelInfoUtils;
 import org.springblade.manager.utils.FileUtils;
 import org.springblade.manager.vo.*;
 import org.springblade.resource.feign.NewIOSSClient;
@@ -619,7 +620,7 @@ public class ExcelTabServiceImpl extends BaseServiceImpl<ExcelTabMapper, ExcelTa
                 //上传新文件到文件服务器
                 //excel修改 同步修改html 对象
                 String thmlUrl = file_path + filecode + ".html";
-              //  FileUtils.excelInfo(inputStream,dataUrl,thmlUrl,"2");
+                ExcelInfoUtils.excelInfo(inputStream,dataUrl,thmlUrl,"2");
                 BladeFile bladeFile = newIOSSClient.uploadFile(excelTab.getExtension(), dataUrl);
                 //获取文件大小
                int size = connection.getContentLength() / 1024 / 1024; //单位M

+ 401 - 0
blade-service/blade-manager/src/main/java/org/springblade/manager/utils/ExcelInfoUtils.java

@@ -0,0 +1,401 @@
+package org.springblade.manager.utils;
+
+import com.spire.xls.CellRange;
+import com.spire.xls.FileFormat;
+import com.spire.xls.Workbook;
+import com.spire.xls.Worksheet;
+import com.spire.xls.core.IXLSRange;
+import com.spire.xls.core.spreadsheet.HTMLOptions;
+import org.apache.commons.lang.StringUtils;
+import org.jsoup.Jsoup;
+import org.jsoup.nodes.Document;
+import org.jsoup.nodes.Element;
+import org.jsoup.select.Elements;
+import org.springblade.common.utils.SnowFlakeUtil;
+import org.springblade.core.tool.utils.FileUtil;
+import org.springblade.core.tool.utils.Func;
+import org.springblade.core.tool.utils.IoUtil;
+import org.springblade.core.tool.utils.ResourceUtil;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Map;
+
+public class ExcelInfoUtils {
+
+    public static void main123(String[] args) throws FileNotFoundException {
+        String excelUrl = "/Users/hongchuangyanfa/Downloads/C10.28隧道注浆施工记录表(1).xlsx";
+        String old_html = "/Users/hongchuangyanfa/Desktop/pdf/old_html.html";
+        String old_xlsx = "/Users/hongchuangyanfa/Desktop/pdf/old_html.xlsx";
+        File file = new File(excelUrl);
+        InputStream fileInputStream = new FileInputStream(file);
+        excelInfo(fileInputStream,old_xlsx,old_html,"1");
+
+
+        // 解析原始excel
+        Workbook wb = new Workbook();
+        wb.loadFromMHtml(excelUrl);
+        Worksheet sheet = wb.getWorksheets().get(0);
+
+/*        CellRange[] mergedCells = sheet.getMergedCells();
+        for(int i=0;i<mergedCells.length;i++){
+            CellRange oldcell = mergedCells[i];
+            IXLSRange ixlsRange = sheet.get(oldcell.getRow(), oldcell.getColumn(), oldcell.getLastRow(), oldcell.getLastColumn());
+            System.out.println(ixlsRange.getDataValidation().getErrorMessage());
+        }
+
+
+        ArrayList<CellRange> cellList = sheet.getCellList();
+        for(int i=0;i<cellList.size();i++){
+            CellRange oldcell = cellList.get(i);
+            System.out.println(oldcell.getRow()+"---"+oldcell.getColumn()+"---="+oldcell.getText()+"---x="+oldcell.getDataValidation().getErrorMessage());
+        }*/
+
+
+
+        String new_html = "/Users/hongchuangyanfa/Desktop/pdf/new_html.html";
+        String new_xlsx = "/Users/hongchuangyanfa/Desktop/pdf/new_html.xlsx";
+       // excelInfo(old_xlsx,new_xlsx,new_html);
+    }
+
+    public static void excelInfo(InputStream inputStream, String excelURL, String htmlUrl,String type) {
+        try {
+            String file_path = "/Users/hongchuangyanfa/Desktop//pdf/";
+            String filecode = SnowFlakeUtil.getId() + "";
+            String thmlUrl2 = file_path + filecode + "123.html";
+
+            // 解析原始excel
+            Workbook wb = new Workbook();
+            wb.loadFromMHtml(inputStream);
+            Worksheet sheet = wb.getWorksheets().get(0);
+
+            HTMLOptions options = new HTMLOptions();
+            options.setImageEmbedded(true);
+
+            //复制一份
+            wb.saveToFile(excelURL,FileFormat.Version2013);
+            // 操作
+            Workbook wb2 = new Workbook();
+            wb2.loadFromMHtml(excelURL);
+            Worksheet sheet2 = wb2.getWorksheets().get(0);
+
+            // 坐标map
+            Map<String, Map<String, Integer>> xyList = new HashMap<>();
+            int j = 0;
+            int maxVal = 0;
+            if(type.equals("2")){
+                CellRange[] sheet2Cells = sheet2.getCells();
+                for (int i = 0; i < sheet2Cells.length; i++) {
+                    CellRange oldcell = sheet2Cells[i];
+                    String data = oldcell.getDataValidation().getErrorMessage();
+                    int k = 0;
+                    if(Func.isNumeric(data)){
+                        k = Func.toInt(data);
+                    }
+                    if (maxVal < k) {
+                        maxVal = k;
+                    }
+                }
+            }else if(type.equals("1")){
+                CellRange[] sheet2Cells = sheet.getCells();
+                for (int i = 0; i < sheet2Cells.length; i++) {
+                    CellRange oldcell = sheet2Cells[i];
+                    sheet.getCellRange(oldcell.getRow(),oldcell.getColumn()).getDataValidation().setErrorMessage("");
+                }
+            }
+
+            //合并单元格操作
+            CellRange[] mergedCells = sheet.getMergedCells();
+            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());
+                if(type.equals("2")){
+                    String data = mergedCell.getDataValidation().getErrorMessage();
+                    if (StringUtils.isEmpty(data)) {
+                        if(maxVal<=0){
+                            j = j + 1;
+                        }else{
+                            maxVal = maxVal+1;
+                            j=maxVal;
+                        }
+                    } else {
+                        j = Func.toInt(data);
+                    }
+                }else{
+                    j = j + 1;
+                }
+
+                // 目标表添加备注信息
+             //   mergedCell.getDataValidation().setErrorMessage(j+"");
+               // oldcell.getDataValidation().setErrorMessage(j+"");
+               // sheet2.getMergedCells()[i].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);
+            }
+
+            //变更最大值
+            if(maxVal<=0){
+                maxVal = j;
+            }
+
+            // 单个
+            CellRange[] onCell = sheet.getCells();
+            // 单个cell
+            for (int i = 0; i < onCell.length; i++) {
+                CellRange oldcell = onCell[i];
+                String data = oldcell.getDataValidation().getErrorMessage();
+                System.out.println(oldcell.getRow()+"---"+oldcell.getColumn()+"---="+oldcell.getText()+"---x="+data);
+                if (StringUtils.isEmpty(data)) {
+                    if(maxVal<=0){
+                        j = j + 1;
+                    }else{
+                        maxVal = maxVal+1;
+                        j=maxVal;
+                    }
+                } else {
+                    j = Func.toInt(data);
+                }
+                if(StringUtils.isEmpty(data)){
+                    oldcell.getDataValidation().setErrorMessage(j+"");
+                    sheet2.getCells()[i].setText(j+"");
+                }
+                if(!xyList.containsKey(j+"")){
+                    Map<String, Integer> dataMap = new HashMap<>();
+                    dataMap.put("y1", oldcell.getRow());
+                    dataMap.put("y2", oldcell.getLastRow());
+                    dataMap.put("x1", oldcell.getColumn());
+                    dataMap.put("x2", oldcell.getLastColumn());
+                    xyList.put(j + "", dataMap);
+                }
+            }
+
+            // 保存上传的excel
+            wb.saveToFile(excelURL,FileFormat.Version2013);
+            sheet.saveToHtml(htmlUrl, options);
+            sheet2.saveToHtml(thmlUrl2);
+
+            // html 转换值
+
+            // 组装坐标
+            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);
+
+            Document doc1 = Jsoup.parse(htmlString1);
+            Element table1 = doc1.select("table").first();
+            Elements trs1 = table1.select("tr");
+            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);
+                    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 操作
+     *
+     * @param inExcelUrl
+     * @param excelURL
+     * @param htmlUrl
+     * @throws Exception
+     */
+    public static void excelInfo222(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();
+        }
+    }
+
+}