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 d22db7247..c13dd7034 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 @@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.jero.common.constant.enums.CutEnum; import com.jero.common.exception.JeroBootException; +import com.jero.common.util.oss.CosBootUtil; import com.jero.modules.compare.entity.*; import com.jero.modules.compare.mapper.SarFileCompareItemCommentMapper; import com.jero.modules.compare.service.ISarFileCompareInfoService; @@ -27,10 +28,10 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.awt.image.BufferedImage; -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.IOException; -import java.io.OutputStream; +import java.io.*; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.nio.file.StandardOpenOption; import java.text.SimpleDateFormat; import java.util.*; import java.util.regex.Matcher; @@ -210,6 +211,14 @@ public class SarFileCompareItemCommentServiceImpl extends ServiceImpl mergeCells = new ArrayList<>(); for (SarFileCompareItemComment itemComment : list) { + SarFileCompareExcelMergeCell sarFileCompareExcelMergeCell = new SarFileCompareExcelMergeCell(); + String[] leftSplit = null; + String[] rightSplit = null; Row row = sheet.createRow(i); row.createCell(0).setCellValue(sarFileCompareInfo == null ? "" : sarFileCompareInfo.getTitleLeft() + " " + sarFileCompareInfo.getFileNameLeft()); row.createCell(4).setCellValue(sarFileCompareInfo == null ? "" : sarFileCompareInfo.getTitleRight() + " " + sarFileCompareInfo.getFileNameRight()); @@ -260,26 +274,71 @@ public class SarFileCompareItemCommentServiceImpl extends ServiceImpl 1) { + for (int j = 0; j < leftSplit.length; j++) { + if (j > 0) { + Row row2 = sheet.createRow(j + i); + if (leftSplit[j].contains(".jpg") || leftSplit[j].contains(".png")) { + row2.setHeight((short) 5000); //设置行高 + //先将图片下载到本地 + String fileLastName = leftSplit[j].substring(leftSplit[j].lastIndexOf("/") + 1); + if (CosBootUtil.doesObjectExist(leftSplit[j])) { + try (InputStream in = CosBootUtil.download(leftSplit[j])) { + copyFile2(in, dir + File.separator + fileLastName); + } catch (IOException e) { + e.printStackTrace(); + log.error(e.getMessage(), e); + } + } + File file = new File(dir + File.separator + fileLastName); + if (file.exists()) { + bufferImg = ImageIO.read(file); + ImageIO.write(bufferImg, "jpg", byteArrayOut); + //anchor主要用于设置图片的属性 + HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 600, 200, (short) 3, j + i, (short) 3, j + i); + patriarch.createPicture(anchor, workbook.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG)); + } + } else { + row2.createCell(3).setCellValue(leftSplit[j]); + } + } else { + if (leftSplit[j].contains(".jpg") || leftSplit[j].contains(".png")) { + row.setHeight((short) 5000); //设置行高 + //先将图片下载到本地 + String fileLastName = leftSplit[j].substring(leftSplit[j].lastIndexOf("/") + 1); + if (CosBootUtil.doesObjectExist(leftSplit[j])) { + try (InputStream in = CosBootUtil.download(leftSplit[j])) { + copyFile2(in, dir + File.separator + fileLastName); + } catch (IOException e) { + e.printStackTrace(); + log.error(e.getMessage(), e); + } + } + File file = new File(dir + File.separator + fileLastName); + if (file.exists()) { + bufferImg = ImageIO.read(file); + ImageIO.write(bufferImg, "jpg", byteArrayOut); + //anchor主要用于设置图片的属性 + HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 600, 200, (short) 3, j + i, (short) 3, j + i); + patriarch.createPicture(anchor, workbook.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG)); + + } + } else { + row.createCell(3).setCellValue(leftSplit[j]); + } } - } else { - rowdata = rowdata + s; } + sarFileCompareExcelMergeCell.setLeftStart(i); + sarFileCompareExcelMergeCell.setLeftEnd(i + leftSplit.length - 1); + sarFileCompareExcelMergeCell.setRightStart(0); + sarFileCompareExcelMergeCell.setRightEnd(0); + mergeCells.add(sarFileCompareExcelMergeCell); + } else { + row.createCell(3).setCellValue(rowDataAndPath); } - - - row.createCell(3).setCellValue(rowdata); row.setRowStyle(cellStyle); } } @@ -291,7 +350,72 @@ public class SarFileCompareItemCommentServiceImpl extends ServiceImpl", "").replace("

", "\r\n"); - row.createCell(7).setCellValue(text); + BufferedImage bufferImg = null; + ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream(); + addImgdata(text,request.getSession()); + String rowDataAndPath = (String) request.getSession().getAttribute("txtAndImg"); + rightSplit = rowDataAndPath.split("---"); + if (rightSplit.length > 1) { + for (int j = 0; j < rightSplit.length; j++) { + if (j > 0) { + Row row2 = sheet.createRow(j + i); + if (rightSplit[j].contains(".jpg") || rightSplit[j].contains(".png")) { + row2.setHeight((short) 5000); //设置行高 + //先将图片下载到本地 + String fileLastName = rightSplit[j].substring(rightSplit[j].lastIndexOf("/") + 1); + if (CosBootUtil.doesObjectExist(rightSplit[j])) { + try (InputStream in = CosBootUtil.download(rightSplit[j])) { + copyFile2(in, dir + File.separator + fileLastName); + } catch (IOException e) { + e.printStackTrace(); + log.error(e.getMessage(), e); + } + } + File file = new File(dir + File.separator + fileLastName); + if (file.exists()) { + bufferImg = ImageIO.read(file); + ImageIO.write(bufferImg, "jpg", byteArrayOut); + //anchor主要用于设置图片的属性 + HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 600, 200, (short) 3, j + i, (short) 3, j + i); + patriarch.createPicture(anchor, workbook.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG)); + + } + } else { + row2.createCell(3).setCellValue(rightSplit[j]); + } + } else { + if (rightSplit[j].contains(".jpg") || rightSplit[j].contains(".png")) { + row.setHeight((short) 5000); //设置行高 + //先将图片下载到本地 + String fileLastName = rightSplit[j].substring(rightSplit[j].lastIndexOf("/") + 1); + if (CosBootUtil.doesObjectExist(rightSplit[j])) { + try (InputStream in = CosBootUtil.download(rightSplit[j])) { + copyFile2(in, dir + File.separator + fileLastName); + } catch (IOException e) { + e.printStackTrace(); + log.error(e.getMessage(), e); + } + } + File file = new File(dir + File.separator + fileLastName); + if (file.exists()) { + bufferImg = ImageIO.read(file); + ImageIO.write(bufferImg, "jpg", byteArrayOut); + //anchor主要用于设置图片的属性 + HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 600, 200, (short) 3, j + i, (short) 3, j + i); + patriarch.createPicture(anchor, workbook.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG)); + + } + } else { + row.createCell(7).setCellValue(rightSplit[j]); + } + } + } + sarFileCompareExcelMergeCell.setRightStart(i); + sarFileCompareExcelMergeCell.setRightEnd(i + rightSplit.length - 1); + mergeCells.add(sarFileCompareExcelMergeCell); + } else { + row.createCell(7).setCellValue(rowDataAndPath); + } row.setRowStyle(cellStyle); } } @@ -302,7 +426,7 @@ public class SarFileCompareItemCommentServiceImpl extends ServiceImpl= 0 ? mergeCell.getLeftStart() : mergeCell.getRightStart(); + Integer end = leftCount - rightCount >= 0 ? mergeCell.getLeftEnd() : mergeCell.getRightEnd(); + //合并 + CellRangeAddress region0 = new CellRangeAddress(start, end, 0, 0); + sheet.addMergedRegion(region0); + CellRangeAddress region1 = new CellRangeAddress(start, end, 1, 1); + sheet.addMergedRegion(region1); + CellRangeAddress region2 = new CellRangeAddress(start, end, 2, 2); + sheet.addMergedRegion(region2); + CellRangeAddress region4 = new CellRangeAddress(start, end, 4, 4); + sheet.addMergedRegion(region4); + CellRangeAddress region5 = new CellRangeAddress(start, end, 5, 5); + sheet.addMergedRegion(region5); + CellRangeAddress region6 = new CellRangeAddress(start, end, 6, 6); + sheet.addMergedRegion(region6); + CellRangeAddress region7 = new CellRangeAddress(start, end, 7, 7); + sheet.addMergedRegion(region7); + } + } + os = response.getOutputStream(); workbook.write(os); @@ -367,4 +515,28 @@ public class SarFileCompareItemCommentServiceImpl extends ServiceImpl 0) { + bos.write(bytes, 0, len); + } + // 关闭流 先开后关 后开先关 + // 先开后关,先开的输入流,再开的输出流,那么应该先关输出流,再关输入流 + // 先关外层,再关内层。如BufferedInputStream包装了一个FileInputStream,那么先关BufferedInputStream,再关FileInputStream。 + bos.close(); // 后开先关 + bis.close(); // 先开后关 + } + }