|
@@ -0,0 +1,271 @@
|
|
|
+package org.springblade.manager.utils;
|
|
|
+
|
|
|
+import com.spire.pdf.PdfDocument;
|
|
|
+import com.spire.pdf.utilities.PdfTable;
|
|
|
+import com.spire.pdf.utilities.PdfTableExtractor;
|
|
|
+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.spreadsheet.HTMLOptions;
|
|
|
+import org.apache.commons.lang.StringUtils;
|
|
|
+import org.apache.poi.ss.usermodel.Cell;
|
|
|
+import org.apache.poi.ss.usermodel.Row;
|
|
|
+import org.apache.poi.ss.usermodel.Sheet;
|
|
|
+import org.apache.poi.ss.usermodel.WorkbookFactory;
|
|
|
+import org.jsoup.Jsoup;
|
|
|
+import org.jsoup.nodes.Document;
|
|
|
+import org.jsoup.nodes.Element;
|
|
|
+import org.jsoup.select.Elements;
|
|
|
+import org.springblade.common.utils.CommonUtil;
|
|
|
+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.manager.vo.DateFormat;
|
|
|
+
|
|
|
+import java.io.*;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+public class ExcelInfoUtils2 {
|
|
|
+
|
|
|
+
|
|
|
+ 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];
|
|
|
+ oldcell.getDataValidation().setErrorMessage("");
|
|
|
+ sheet.get(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) {
|
|
|
+ // td 添加坐标
|
|
|
+ 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);
|
|
|
+
|
|
|
+
|
|
|
+ // 为下面对象添加坐标
|
|
|
+ if(cell1.children().size()>=1){
|
|
|
+ Element element = cell1.children().get(0);
|
|
|
+ if(element.hasAttr("x1") && element.hasAttr("y1")){
|
|
|
+ element.removeAttr("x1");
|
|
|
+ element.removeAttr("x2");
|
|
|
+ element.removeAttr("y1");
|
|
|
+ element.removeAttr("y2");
|
|
|
+ element.attr("x1", xyMap.get("x1") + "");
|
|
|
+ element.attr("x2", xyMap.get("x2") + "");
|
|
|
+ element.attr("y1", xyMap.get("y1") + "");
|
|
|
+ element.attr("y2", xyMap.get("y2") + "");
|
|
|
+ }else{
|
|
|
+ if(element.children().size()>=1){
|
|
|
+ Element element2 = element.children().get(0);
|
|
|
+ if(element2.hasAttr("x1") && element2.hasAttr("y1")){
|
|
|
+ element2.removeAttr("x1");
|
|
|
+ element2.removeAttr("x2");
|
|
|
+ element2.removeAttr("y1");
|
|
|
+ element2.removeAttr("y2");
|
|
|
+ element2.attr("x1", xyMap.get("x1") + "");
|
|
|
+ element2.attr("x2", xyMap.get("x2") + "");
|
|
|
+ element2.attr("y1", xyMap.get("y1") + "");
|
|
|
+ element2.attr("y2", xyMap.get("y2") + "");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ FileUtil.writeToFile(html1, doc1.html(), Boolean.parseBoolean("UTF-8"));
|
|
|
+
|
|
|
+ if (html2.exists()) {
|
|
|
+ html2.delete();
|
|
|
+ }
|
|
|
+ wb2.dispose();
|
|
|
+ wb.dispose();
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main123(String[] args) throws Exception {
|
|
|
+
|
|
|
+ /* String html="/Users/hongchuangyanfa/Desktop/privateUrl/1701131518073634816.html";
|
|
|
+ File html1 = new File(html); // 原始html
|
|
|
+ InputStream inputStream1 = new FileInputStream(html1);
|
|
|
+
|
|
|
+ String htmlString1 = IoUtil.readToString(inputStream1);
|
|
|
+ Document doc1 = Jsoup.parse(htmlString1);
|
|
|
+ Element table1 = doc1.select("table").first();
|
|
|
+ Elements dqids = table1.getElementsByAttribute("dqid");
|
|
|
+ int x1=0;
|
|
|
+ int y1=0;
|
|
|
+ for (Element element : dqids){
|
|
|
+ if(element.hasAttr("x1") && element.hasAttr("y1")){
|
|
|
+ Elements x11 = element.getElementsByAttribute("x1");
|
|
|
+ Element element1 = x11.get(x11.size()-1);
|
|
|
+ x1 = Func.toInt(element1.attr("x1"));
|
|
|
+ y1 = Func.toInt(element1.attr("y1"));
|
|
|
+ }
|
|
|
+ }*/
|
|
|
+ String excelPath="/Users/hongchuangyanfa/Desktop/pdf/1234567.xlsx";
|
|
|
+ String data="https://bladex-chongqing-info.oss-cn-hangzhou.aliyuncs.com//upload/20230911/b63658909c268efeb753dcf673ce34ab.xlsx";
|
|
|
+ InputStream exceInp = CommonUtil.getOSSInputStream(data);
|
|
|
+ org.apache.poi.ss.usermodel.Workbook workbook = WorkbookFactory.create(exceInp);
|
|
|
+
|
|
|
+ //获取工作表
|
|
|
+ Sheet sheet = workbook.getSheetAt(0);
|
|
|
+ Row row = sheet.getRow(7-1);
|
|
|
+ Cell cell = row.getCell(5-1);
|
|
|
+ cell.setCellValue("123444");
|
|
|
+
|
|
|
+ FileOutputStream outputStream = new FileOutputStream(excelPath);
|
|
|
+ workbook.write(outputStream);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|