From d44f64535aecde7b731ecf1af2ffacff90882292 Mon Sep 17 00:00:00 2001 From: CD <1103614279@qq.com> Date: Sun, 9 Oct 2022 14:17:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E6=A1=A3=E5=AF=B9=E6=AF=94=20?= =?UTF-8?q?=E8=A1=A8=E6=A0=BC=E6=8B=86=E5=88=86=E5=87=BA=E6=9D=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SarFileCompareItemCommentServiceImpl.java | 236 ++++++++++-------- .../compare/utils/ConvertHtml2Excel.java | 231 +++++++++++++++++ .../compare/utils/CrossRangeCellMeta.java | 37 +++ 3 files changed, 399 insertions(+), 105 deletions(-) create mode 100644 jero-boot/jero-boot-modules/src/main/java/com/jero/modules/compare/utils/ConvertHtml2Excel.java create mode 100644 jero-boot/jero-boot-modules/src/main/java/com/jero/modules/compare/utils/CrossRangeCellMeta.java diff --git a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/compare/service/impl/SarFileCompareItemCommentServiceImpl.java b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/compare/service/impl/SarFileCompareItemCommentServiceImpl.java index 071f02deb..0d7fbcd94 100644 --- a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/compare/service/impl/SarFileCompareItemCommentServiceImpl.java +++ b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/compare/service/impl/SarFileCompareItemCommentServiceImpl.java @@ -11,11 +11,12 @@ import com.jero.modules.compare.mapper.SarFileCompareItemCommentMapper; import com.jero.modules.compare.service.ISarFileCompareInfoService; import com.jero.modules.compare.service.ISarFileCompareItemCommentService; import com.jero.modules.compare.service.ISarFileCompareItemService; +import com.jero.modules.compare.utils.ConvertHtml2Excel; import com.jero.modules.system.util.StringUtils; import org.apache.commons.lang3.ObjectUtils; -import org.apache.poi.hssf.usermodel.HSSFClientAnchor; -import org.apache.poi.hssf.usermodel.HSSFPatriarch; -import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.common.usermodel.HyperlinkType; +import org.apache.poi.hssf.usermodel.*; +import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.xssf.usermodel.XSSFWorkbook; @@ -253,6 +254,12 @@ public class SarFileCompareItemCommentServiceImpl extends ServiceImpl mergeCells = new ArrayList<>(); @@ -270,9 +277,7 @@ public class SarFileCompareItemCommentServiceImpl extends ServiceImpl", "").replace("

", "\r\n"); - addImgdata(text,request.getSession()); - String rowDataAndPath = (String) request.getSession().getAttribute("txtAndImg"); + String rowDataAndPath = getSplitContent(request, text); leftSplit = rowDataAndPath.split("---"); if (leftSplit.length > 1) { for (int j = 0; j < leftSplit.length; j++) { @@ -284,55 +289,9 @@ public class SarFileCompareItemCommentServiceImpl extends ServiceImpl", "").replace("

", "\r\n"); - addImgdata(text,request.getSession()); - String rowDataAndPath = (String) request.getSession().getAttribute("txtAndImg"); + String rowDataAndPath = getSplitContent(request, text); rightSplit = rowDataAndPath.split("---"); if (rightSplit.length > 1) { for (int j = 0; j < rightSplit.length; j++) { @@ -368,55 +325,9 @@ public class SarFileCompareItemCommentServiceImpl extends ServiceImpl") < 0) { + if (tableHtml.indexOf("") < 0) { + tableHtml = tableHtml.replaceFirst("", ""); + tableHtml = tableHtml.replaceFirst("", ""); + } else { + tableHtml = tableHtml.replaceFirst("", ""); + tableHtml = tableHtml.replaceFirst("", ""); + } + } + String cnt = "\n"; + tableHtml = tableHtml.replaceAll("<[\\s]*?br[^>]*?>|<[\\s]*?\\/[\\s]*?br[\\s]*?>", cnt); + String tableSheetName = "表格" + System.currentTimeMillis(); + ConvertHtml2Excel.table2Excel(tableHtml, (HSSFWorkbook) workbook, tableSheetName); + //添加超链接 + CreationHelper createHelper = workbook.getCreationHelper(); + Hyperlink hyperlink1 = createHelper.createHyperlink(HyperlinkType.DOCUMENT); + String tableName = "#\'" + tableSheetName + "\'!A1"; + hyperlink1.setAddress(tableName); + cell.setHyperlink(hyperlink1);// 链接 + cell.setCellStyle(cellStyleLink); + } + + /** + * 获取文字、图片、表格分割后的内容 + * @param request + * @param text + * @return + */ + private String getSplitContent(HttpServletRequest request, String text) { + text = text.replace("

", "").replace("

", "\r\n"); + //分割图片 + addImgdata(text, request.getSession()); + //分割表格 + String txtAndImg = (String) request.getSession().getAttribute("txtAndImg"); + txtAndImg = txtAndImg.replaceAll("table", "replaceTable"); + splitTable(txtAndImg, request.getSession()); + return (String) request.getSession().getAttribute("textAndTableAndImg"); + } + + /** + * 分割表格 + * @param textAndImg + * @param session + */ + private void splitTable(String textAndImg,HttpSession session) { + if (textAndImg.contains("") + 14); + String table = textAndImg.substring(textAndImg.indexOf("")+14); + table = table.replaceAll("replaceTable", "table"); + textAndImg = left + "---" + table + "---" + right; + splitTable(textAndImg, session); + } else { + session.setAttribute("textAndTableAndImg", textAndImg); + } + } + /** * 提取字符串中标签内容中的图片路径,并重新按顺序拼接 * @param text diff --git a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/compare/utils/ConvertHtml2Excel.java b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/compare/utils/ConvertHtml2Excel.java new file mode 100644 index 000000000..db29bbb5c --- /dev/null +++ b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/compare/utils/ConvertHtml2Excel.java @@ -0,0 +1,231 @@ +package com.jero.modules.compare.utils; +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.math.NumberUtils; +import org.apache.poi.hssf.usermodel.HSSFCell; +import org.apache.poi.hssf.usermodel.HSSFCellStyle; +import org.apache.poi.hssf.usermodel.HSSFFont; +import org.apache.poi.hssf.usermodel.HSSFRow; +import org.apache.poi.hssf.usermodel.HSSFSheet; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.hssf.util.HSSFCellUtil; +import org.apache.poi.hssf.util.HSSFColor; +import org.apache.poi.ss.util.CellRangeAddress; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.DocumentHelper; +import org.dom4j.Element; + + +/**** + * html转excel + * @author user + * + */ +public class ConvertHtml2Excel { + /** + * html表格转excel + * + * @param tableHtml 如 + * + * .. + *
+ * @return + */ + public static void table2Excel(String tableHtml, HSSFWorkbook wb,String tableSheetName) { + HSSFSheet sheet = wb.createSheet(tableSheetName); + List crossRowEleMetaLs = new ArrayList(); + int rowIndex = 0; + try { + Document data = DocumentHelper.parseText(tableHtml); + // 生成表头 + Element thead = data.getRootElement().element("thead"); + HSSFCellStyle titleStyle = getTitleStyle(wb); + int ls=0;//列数 + if (thead != null) { + List trLs = thead.elements("tr"); + for (Element trEle : trLs) { + HSSFRow row = sheet.createRow(rowIndex); + List thLs = trEle.elements("th"); + ls=thLs.size(); + makeRowCell(thLs, rowIndex, row, 0, titleStyle, crossRowEleMetaLs); + rowIndex++; + } + } + // 生成表体 + Element tbody = data.getRootElement().element("tbody"); + HSSFCellStyle contentStyle = getContentStyle(wb); + if (tbody != null) { + List trLs = tbody.elements("tr"); + for (Element trEle : trLs) { + HSSFRow row = sheet.createRow(rowIndex); + List thLs = trEle.elements("th"); + int cellIndex = makeRowCell(thLs, rowIndex, row, 0, titleStyle, crossRowEleMetaLs); + List tdLs = trEle.elements("td"); + makeRowCell(tdLs, rowIndex, row, cellIndex, contentStyle, crossRowEleMetaLs); + rowIndex++; + } + } + // 合并表头 + for (CrossRangeCellMeta crcm : crossRowEleMetaLs) { + sheet.addMergedRegion(new CellRangeAddress(crcm.getFirstRow(), crcm.getLastRow(), crcm.getFirstCol(), crcm.getLastCol())); + setRegionStyle(sheet, new CellRangeAddress(crcm.getFirstRow(), crcm.getLastRow(), crcm.getFirstCol(), crcm.getLastCol()),contentStyle); + } + for(int i=0;i tdLs, int rowIndex, HSSFRow row, int startCellIndex, HSSFCellStyle cellStyle, + List crossRowEleMetaLs) { + int i = startCellIndex; + for (int eleIndex = 0; eleIndex < tdLs.size(); i++, eleIndex++) { + int captureCellSize = getCaptureCellSize(rowIndex, i, crossRowEleMetaLs); + while (captureCellSize > 0) { + for (int j = 0; j < captureCellSize; j++) {// 当前行跨列处理(补单元格) + row.createCell(i); + i++; + } + captureCellSize = getCaptureCellSize(rowIndex, i, crossRowEleMetaLs); + } + Element thEle = tdLs.get(eleIndex); + String val = thEle.getTextTrim(); + if (StringUtils.isBlank(val)) { + Element e = thEle.element("a"); + if (e != null) { + val = e.getTextTrim(); + } + } + HSSFCell c = row.createCell(i); + if (NumberUtils.isNumber(val)) { + c.setCellValue(Double.parseDouble(val)); + c.setCellType(HSSFCell.CELL_TYPE_NUMERIC); + } else { + c.setCellValue(val); + } + int rowSpan = NumberUtils.toInt(thEle.attributeValue("rowspan"), 1); + int colSpan = NumberUtils.toInt(thEle.attributeValue("colspan"), 1); + c.setCellStyle(cellStyle); + if (rowSpan > 1 || colSpan > 1) { // 存在跨行或跨列 + crossRowEleMetaLs.add(new CrossRangeCellMeta(rowIndex, i, rowSpan, colSpan)); + } + if (colSpan > 1) {// 当前行跨列处理(补单元格) + for (int j = 1; j < colSpan; j++) { + i++; + row.createCell(i); + } + } + } + return i; + } + + /** + * 设置合并单元格的边框样式 + * + * @param sheet + * @param region + * @param cs + */ + public static void setRegionStyle(HSSFSheet sheet, CellRangeAddress region, HSSFCellStyle cs) { + for (int i = region.getFirstRow(); i <= region.getLastRow(); i++) { + HSSFRow row = HSSFCellUtil.getRow(i, sheet); + for (int j = region.getFirstColumn(); j <= region.getLastColumn(); j++) { + HSSFCell cell = HSSFCellUtil.getCell(row, (short) j); + cell.setCellStyle(cs); + } + } + } + + /** + * 获得因rowSpan占据的单元格 + * + * @param rowIndex 行号 + * @param colIndex 列号 + * @param crossRowEleMetaLs 跨行列元数据 + * @return 当前行在某列需要占据单元格 + */ + private static int getCaptureCellSize(int rowIndex, int colIndex, List crossRowEleMetaLs) { + int captureCellSize = 0; + for (CrossRangeCellMeta crossRangeCellMeta : crossRowEleMetaLs) { + if (crossRangeCellMeta.getFirstRow() < rowIndex && crossRangeCellMeta.getLastRow() >= rowIndex) { + if (crossRangeCellMeta.getFirstCol() <= colIndex && crossRangeCellMeta.getLastCol() >= colIndex) { + captureCellSize = crossRangeCellMeta.getLastCol() - colIndex + 1; + } + } + } + return captureCellSize; + } + + /** + * 获得标题样式 + * + * @param workbook + * @return + */ + private static HSSFCellStyle getTitleStyle(HSSFWorkbook workbook) { + short titlebackgroundcolor = HSSFColor.GREY_25_PERCENT.index; + short fontSize = 12; + String fontName = "宋体"; + HSSFCellStyle style = workbook.createCellStyle(); + style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); + style.setAlignment(HSSFCellStyle.ALIGN_CENTER); + style.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框 + style.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框 + style.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框 + style.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框 + style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); + style.setFillForegroundColor(titlebackgroundcolor);// 背景色 + + HSSFFont font = workbook.createFont(); + font.setFontName(fontName); + font.setFontHeightInPoints(fontSize); + font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); + style.setFont(font); + return style; + } + + /** + * 获得内容样式 + * + * @param wb + * @return + */ + private static HSSFCellStyle getContentStyle(HSSFWorkbook wb) { + short fontSize = 12; + String fontName = "宋体"; + HSSFCellStyle style = wb.createCellStyle(); + style.setBorderBottom((short) 1); + style.setBorderTop((short) 1); + style.setBorderLeft((short) 1); + style.setBorderRight((short) 1); + HSSFFont font = wb.createFont(); + font.setFontName(fontName); + font.setFontHeightInPoints(fontSize); + style.setFont(font); + style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中 + style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中 + + return style; + } +} + diff --git a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/compare/utils/CrossRangeCellMeta.java b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/compare/utils/CrossRangeCellMeta.java new file mode 100644 index 000000000..acee632aa --- /dev/null +++ b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/compare/utils/CrossRangeCellMeta.java @@ -0,0 +1,37 @@ +package com.jero.modules.compare.utils; + +public class CrossRangeCellMeta { + + public CrossRangeCellMeta(int firstRowIndex, int firstColIndex, int rowSpan, int colSpan) { + super(); + this.firstRowIndex = firstRowIndex; + this.firstColIndex = firstColIndex; + this.rowSpan = rowSpan; + this.colSpan = colSpan; + } + + private int firstRowIndex; + private int firstColIndex; + private int rowSpan;// 跨越行数 + private int colSpan;// 跨越列数 + + public int getFirstRow() { + return firstRowIndex; + } + + public int getLastRow() { + return firstRowIndex + rowSpan - 1; + } + + public int getFirstCol() { + return firstColIndex; + } + + public int getLastCol() { + return firstColIndex + colSpan - 1; + } + + public int getColSpan(){ + return colSpan; + } +}