|
@@ -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();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|