You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
90 lines
4.4 KiB
90 lines
4.4 KiB
4 years ago
|
package com.fr.plugin.excel;
|
||
|
|
||
|
import com.fr.decision.webservice.bean.security.WatermarkBean;
|
||
|
import com.fr.io.exporter.AbstractAppExporter;
|
||
|
import com.fr.io.exporter.AppExporter;
|
||
|
import com.fr.log.FineLoggerFactory;
|
||
|
import com.fr.main.workbook.ResultWorkBook;
|
||
|
import com.fr.plugin.ws.wapper.WsUtils;
|
||
|
import com.fr.third.v2.org.apache.poi.POIXMLDocumentPart;
|
||
|
import com.fr.third.v2.org.apache.poi.openxml4j.opc.PackagePartName;
|
||
|
import com.fr.third.v2.org.apache.poi.openxml4j.opc.PackageRelationship;
|
||
|
import com.fr.third.v2.org.apache.poi.openxml4j.opc.TargetMode;
|
||
|
import com.fr.third.v2.org.apache.poi.ss.usermodel.Workbook;
|
||
|
import com.fr.third.v2.org.apache.poi.xssf.streaming.SXSSFWorkbook;
|
||
|
import com.fr.third.v2.org.apache.poi.xssf.usermodel.XSSFRelation;
|
||
|
import com.fr.third.v2.org.apache.poi.xssf.usermodel.XSSFSheet;
|
||
|
import com.fr.third.v2.org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||
|
|
||
|
|
||
|
import java.io.*;
|
||
|
|
||
|
public class ExAppExporter extends AbstractAppExporter {
|
||
|
AppExporter realExporter;
|
||
|
WatermarkBean parsedWatermarkBean;
|
||
|
|
||
|
|
||
|
public ExAppExporter(AppExporter realExporter, WatermarkBean parsedWatermarkBean) {
|
||
|
this.realExporter = realExporter;
|
||
|
this.parsedWatermarkBean = parsedWatermarkBean;
|
||
|
}
|
||
|
|
||
|
// @Override
|
||
|
// public void export(OutputStream orginOutputStream, ResultWorkBook resultWorkBook) throws Exception {
|
||
|
// //先用真的导出器把resultWorkBook 导出成excel 我们获取二进制再加工
|
||
|
// ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(4096);
|
||
|
// File tempFile = File.createTempFile("ExportExcel", ".xlsx");
|
||
|
// FileOutputStream fileOutputStream = new FileOutputStream(tempFile);
|
||
|
// realExporter.export(fileOutputStream, resultWorkBook);
|
||
|
// byte[] bytes = byteArrayOutputStream.toByteArray();
|
||
|
// ByteArrayInputStream in = new ByteArrayInputStream(bytes);
|
||
|
// //加水印
|
||
|
// XSSFWorkbook workbook = new XSSFWorkbook(in);
|
||
|
// byte[] watterMarker = WsUtils.getWatterMarker(parsedWatermarkBean);
|
||
|
// int pictureIdx = workbook.addPicture(watterMarker, Workbook.PICTURE_TYPE_PNG);
|
||
|
// POIXMLDocumentPart poixmlDocumentPart = workbook.getAllPictures().get(pictureIdx);
|
||
|
// for (int i = 0; i < workbook.getNumberOfSheets(); i++) {//获取每个Sheet表
|
||
|
// XSSFSheet sheet = workbook.getSheetAt(i);
|
||
|
// PackagePartName ppn = poixmlDocumentPart.getPackagePart().getPartName();
|
||
|
// String relType = XSSFRelation.IMAGES.getRelation();
|
||
|
// PackageRelationship pr = sheet.getPackagePart().addRelationship(ppn, TargetMode.INTERNAL, relType, null);
|
||
|
// sheet.getCTWorksheet().addNewPicture().setId(pr.getId());
|
||
|
// }
|
||
|
// workbook.write(orginOutputStream);
|
||
|
// FineLoggerFactory.getLogger().info("excel导出加水印成功");
|
||
|
// }
|
||
|
|
||
|
@Override
|
||
|
public void export(OutputStream orginOutputStream, ResultWorkBook resultWorkBook) throws Exception {
|
||
|
//先存到临时文件
|
||
|
File tempFile = File.createTempFile("ExportExcel", ".xlsx");
|
||
|
FileOutputStream fileOutputStream = new FileOutputStream(tempFile);
|
||
|
realExporter.export(fileOutputStream, resultWorkBook);
|
||
|
//用SXSSFWorkbook 读取出来处理
|
||
|
SXSSFWorkbook workbook = new SXSSFWorkbook(new XSSFWorkbook(tempFile));
|
||
|
//获取XSSFWorkbook来加水印
|
||
|
XSSFWorkbook xssfWorkbook = workbook.getXSSFWorkbook();
|
||
|
byte[] watterMarker = WsUtils.getWatterMarker(parsedWatermarkBean);
|
||
|
int pictureIdx = workbook.addPicture(watterMarker, Workbook.PICTURE_TYPE_PNG);
|
||
|
POIXMLDocumentPart poixmlDocumentPart = xssfWorkbook.getAllPictures().get(pictureIdx);
|
||
|
for (int i = 0; i < workbook.getNumberOfSheets(); i++) {//获取每个Sheet表
|
||
|
XSSFSheet sheet = xssfWorkbook.getSheetAt(i);
|
||
|
PackagePartName ppn = poixmlDocumentPart.getPackagePart().getPartName();
|
||
|
String relType = XSSFRelation.IMAGES.getRelation();
|
||
|
PackageRelationship pr = sheet.getPackagePart().addRelationship(ppn, TargetMode.INTERNAL, relType, null);
|
||
|
sheet.getCTWorksheet().addNewPicture().setId(pr.getId());
|
||
|
}
|
||
|
//写回原始导出流
|
||
|
workbook.write(orginOutputStream);
|
||
|
workbook.dispose();
|
||
|
workbook.close();
|
||
|
FineLoggerFactory.getLogger().info("--excel导出加水印成功");
|
||
|
//删除临时文件
|
||
|
if (tempFile.exists() &&tempFile.canWrite()) {
|
||
|
tempFile.delete();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|