文档对比加入图片
This commit is contained in:
+193
-21
@@ -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<SarFileCom
|
||||
|
||||
OutputStream os = null;
|
||||
Workbook workbook = new HSSFWorkbook();
|
||||
|
||||
//在本地创建图片文件夹
|
||||
String dir = filePath + File.separator + UUID.randomUUID().toString().replace("-", "");
|
||||
File dirFile = new File(dir);
|
||||
if (dirFile.exists()){
|
||||
dirFile.delete();
|
||||
}
|
||||
dirFile.mkdirs();
|
||||
try {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
String fileName = "导出对比结果 " + sdf.format(new Date()) + ".xlsx";
|
||||
@@ -245,7 +254,12 @@ public class SarFileCompareItemCommentServiceImpl extends ServiceImpl<SarFileCom
|
||||
}
|
||||
}
|
||||
int i = 1;
|
||||
//记录需要合并的单元格
|
||||
List<SarFileCompareExcelMergeCell> 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<SarFileCom
|
||||
BufferedImage bufferImg = null;
|
||||
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
|
||||
addImgdata(text,request.getSession());
|
||||
String rowdata = "";
|
||||
String rowDataAndPath = (String) request.getSession().getAttribute("txtAndImg");
|
||||
for (String s : rowDataAndPath.split("---")) {
|
||||
if (s.contains(".jpg") || s.contains(".png")) {
|
||||
File file = new File(filePath + s);
|
||||
if (file.exists()) {
|
||||
bufferImg = ImageIO.read(file);
|
||||
ImageIO.write(bufferImg, "jpg", byteArrayOut);
|
||||
//anchor主要用于设置图片的属性
|
||||
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 600, 200,(short)3, i, (short) 3, i);
|
||||
patriarch.createPicture(anchor, workbook.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
|
||||
leftSplit = rowDataAndPath.split("---");
|
||||
if (leftSplit.length > 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<SarFileCom
|
||||
row.createCell(6).setCellValue(itemRight.getItemsName());
|
||||
String text = itemRight.getItemsText();
|
||||
text = text.replace("<p>", "").replace("</p>", "\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<SarFileCom
|
||||
cell.setCellStyle(cellStyle);
|
||||
}
|
||||
}
|
||||
i++;
|
||||
i = i + leftSplit.length;
|
||||
}
|
||||
if (includeComments || StringUtils.isEmpty(selectIds)) {
|
||||
Row row = sheet.createRow(i);
|
||||
@@ -318,6 +442,30 @@ public class SarFileCompareItemCommentServiceImpl extends ServiceImpl<SarFileCom
|
||||
row.createCell(1).setCellValue(compareInfo.getComments());
|
||||
row.getCell(1).setCellStyle(cellStyle);
|
||||
}
|
||||
if (ObjectUtils.isNotEmpty(mergeCells)) {
|
||||
for (SarFileCompareExcelMergeCell mergeCell : mergeCells) {
|
||||
Integer leftCount = mergeCell.getLeftEnd() - mergeCell.getLeftStart() + 1;
|
||||
Integer rightCount = mergeCell.getRightEnd() - mergeCell.getRightStart() + 1;
|
||||
Integer start = leftCount - rightCount >= 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<SarFileCom
|
||||
SarFileCompareItemServiceImpl.updateReviewedById(sarFileCompareItemComment.getItemsIdRight());
|
||||
}
|
||||
|
||||
public void copyFile2(InputStream in, String destPath) throws IOException {
|
||||
BufferedInputStream bis = new BufferedInputStream(in);
|
||||
BufferedOutputStream bos =
|
||||
new BufferedOutputStream(Files.newOutputStream(Paths.get(destPath),
|
||||
StandardOpenOption.CREATE,
|
||||
StandardOpenOption.TRUNCATE_EXISTING,
|
||||
StandardOpenOption.WRITE));
|
||||
// 打开输入流
|
||||
// FileInputStream fis = new FileInputStream(srcPath);
|
||||
// 打开输出流
|
||||
// FileOutputStream fos = new FileOutputStream(destPath);
|
||||
// 读取和写入信息
|
||||
byte[] bytes = new byte[1024 * 1024];
|
||||
int len;
|
||||
while ((len = bis.read(bytes)) > 0) {
|
||||
bos.write(bytes, 0, len);
|
||||
}
|
||||
// 关闭流 先开后关 后开先关
|
||||
// 先开后关,先开的输入流,再开的输出流,那么应该先关输出流,再关输入流
|
||||
// 先关外层,再关内层。如BufferedInputStream包装了一个FileInputStream,那么先关BufferedInputStream,再关FileInputStream。
|
||||
bos.close(); // 后开先关
|
||||
bis.close(); // 先开后关
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user