|
@@ -1,11 +1,16 @@
|
|
|
package org.springblade.manager.utils;
|
|
|
|
|
|
+import com.aliyun.oss.common.utils.DateUtil;
|
|
|
import com.aspose.cells.SaveFormat;
|
|
|
import com.aspose.cells.Workbook;
|
|
|
import com.itextpdf.text.Document;
|
|
|
import com.itextpdf.text.pdf.PdfCopy;
|
|
|
import com.itextpdf.text.pdf.PdfReader;
|
|
|
import com.spire.xls.*;
|
|
|
+import com.spire.xls.CellRange;
|
|
|
+import com.spire.xls.collections.CommentsCollection;
|
|
|
+import com.spire.xls.core.INamedRange;
|
|
|
+import com.spire.xls.core.spreadsheet.HTMLOptions;
|
|
|
import com.sun.image.codec.jpeg.JPEGCodec;
|
|
|
import com.sun.image.codec.jpeg.JPEGImageEncoder;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
@@ -29,9 +34,7 @@ import org.springblade.common.utils.SnowFlakeUtil;
|
|
|
import org.springblade.common.utils.SystemUtils;
|
|
|
import org.springblade.common.vo.DataVO;
|
|
|
import org.springblade.core.tool.api.ResultCode;
|
|
|
-import org.springblade.core.tool.utils.FileUtil;
|
|
|
-import org.springblade.core.tool.utils.IoUtil;
|
|
|
-import org.springblade.core.tool.utils.ResourceUtil;
|
|
|
+import org.springblade.core.tool.utils.*;
|
|
|
import org.springblade.system.cache.ParamCache;
|
|
|
|
|
|
import javax.imageio.ImageIO;
|
|
@@ -40,6 +43,8 @@ import java.awt.image.BufferedImage;
|
|
|
import java.io.*;
|
|
|
import java.net.URL;
|
|
|
import java.net.URLEncoder;
|
|
|
+import java.text.DateFormat;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
import java.util.*;
|
|
|
import java.util.regex.Matcher;
|
|
|
import java.util.zip.ZipEntry;
|
|
@@ -305,12 +310,6 @@ public class FileUtils {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public static void main11(String[] args) {
|
|
|
- String excelPath="/Users/hongchuangyanfa/Downloads/A11 施工放样报验单 (1).xlsx";
|
|
|
- String pdfPath="/Users/hongchuangyanfa/Downloads/A11 施工放样报验单 (1).pdf";
|
|
|
-
|
|
|
- FileUtils.setExcelScaleToPdf(excelPath, pdfPath);
|
|
|
- }
|
|
|
|
|
|
/**
|
|
|
* excel设置 打印缩放比例
|
|
@@ -347,7 +346,7 @@ public class FileUtils {
|
|
|
sheet.setHorizontallyCenter(true);//设置打印页面为水平居中
|
|
|
sheet.setVerticallyCenter(true);
|
|
|
sheet.setAutobreaks(true);
|
|
|
- // printSetup.setLandscape(false);
|
|
|
+ // printSetup.setLandscape(false);
|
|
|
sheet.setMargin(XSSFSheet.BottomMargin, (double) 0.1);// 页边距(下)
|
|
|
sheet.setMargin(XSSFSheet.LeftMargin, (double) 0.7);// 页边距(左)
|
|
|
sheet.setMargin(XSSFSheet.RightMargin, (double) 0.7);// 页边距(右)
|
|
@@ -355,9 +354,9 @@ public class FileUtils {
|
|
|
printSetup.setScale((short) 100);//自定义缩放①,此处100为无缩放
|
|
|
System.out.print(sheet.getAutobreaks());
|
|
|
printSetup.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE);
|
|
|
- // printSetup.setFitHeight((short) 1);//设置高度为自动分页
|
|
|
- // printSetup.setFitWidth((short) 1);//设置宽度为一页
|
|
|
- // sheet.setFitToPage(true);
|
|
|
+ // printSetup.setFitHeight((short) 1);//设置高度为自动分页
|
|
|
+ // printSetup.setFitWidth((short) 1);//设置宽度为一页
|
|
|
+ // sheet.setFitToPage(true);
|
|
|
}
|
|
|
// Excel文件生成后存储的位置。
|
|
|
outReport = new ByteArrayOutputStream();
|
|
@@ -415,4 +414,189 @@ public class FileUtils {
|
|
|
return file_path;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ public static void main123(String[] args) throws Exception {
|
|
|
+ String excelUrl = "/Users/hongchuangyanfa/Downloads/C4.13路基压实度汇总表.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");
|
|
|
+
|
|
|
+ String new_html = "/Users/hongchuangyanfa/Desktop/pdf/new_html.html";
|
|
|
+ String new_xlsx = "/Users/hongchuangyanfa/Desktop/pdf/new_html.xlsx";
|
|
|
+
|
|
|
+ File data = new File(old_xlsx);
|
|
|
+ InputStream inputStream = new FileInputStream(data);
|
|
|
+ excelInfo(inputStream,new_xlsx,new_html,"2");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 在线编辑excel 操作
|
|
|
+ *
|
|
|
+ * @param fileInputStream
|
|
|
+ * @param excelURL
|
|
|
+ * @param htmlUrl
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static void excelInfo(InputStream fileInputStream, String excelURL, String htmlUrl, String type) {
|
|
|
+ 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(fileInputStream);
|
|
|
+
|
|
|
+ wb.saveToFile(excelURL, FileFormat.Version2013);
|
|
|
+ // 操作
|
|
|
+ com.spire.xls.Workbook wb2 = new com.spire.xls.Workbook();
|
|
|
+ wb2.loadFromMHtml(excelURL);
|
|
|
+ Worksheet sheet2 = wb2.getWorksheets().get(0);
|
|
|
+
|
|
|
+ HTMLOptions options = new HTMLOptions();
|
|
|
+ options.setImageEmbedded(true);
|
|
|
+ //获取工作表
|
|
|
+ Worksheet sheet = wb.getWorksheets().get(0);
|
|
|
+ sheet.saveToHtml(htmlUrl, options);
|
|
|
+
|
|
|
+ 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();
|
|
|
+ System.out.println(mergedCell.getValue()+"---"+data);
|
|
|
+ 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+"");
|
|
|
+ mergedCell.setValue(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);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 单个cell
|
|
|
+ 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();
|
|
|
+ Map<String, Integer> dataMap = new HashMap<>();
|
|
|
+ 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+"");
|
|
|
+ oldcell.getComment().getRichText().setText(j + "");
|
|
|
+ mergedCell.setValue(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);
|
|
|
+
|
|
|
+ // 上传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) {
|
|
|
+ cell1.empty();
|
|
|
+ }
|
|
|
+ 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();
|
|
|
+ }
|
|
|
+ fileInputStream.close();
|
|
|
+ wb2.dispose();
|
|
|
+ wb.dispose();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
}
|