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.
154 lines
6.3 KiB
154 lines
6.3 KiB
4 years ago
|
package com.fr.plugin.excel;
|
||
|
|
||
|
import com.finebi.dashboard.api.service.export.TableRangeAddress;
|
||
|
import com.finebi.dashboard.api.service.export.TableRow;
|
||
|
import com.finebi.dashboard.api.service.export.provider.WorkbookRowCreator;
|
||
|
import com.finebi.dashboard.impl.service.export.creator.sheet.ExcelTableRow;
|
||
|
import com.finebi.dashboard.impl.service.export.monitor.ExcelBuildMonitor;
|
||
|
import com.finebi.dashboard.impl.service.export.monitor.ExcelBuildMonitorImpl;
|
||
|
import com.fr.decision.webservice.bean.security.WatermarkBean;
|
||
|
import com.fr.log.FineLoggerFactory;
|
||
|
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.Row;
|
||
|
import com.fr.third.v2.org.apache.poi.ss.usermodel.Sheet;
|
||
|
import com.fr.third.v2.org.apache.poi.ss.usermodel.Workbook;
|
||
|
import com.fr.third.v2.org.apache.poi.ss.util.CellRangeAddress;
|
||
|
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.util.HashMap;
|
||
|
import java.util.Map;
|
||
|
|
||
|
public class NewExcelWorkbookRowCreator implements WorkbookRowCreator {
|
||
|
Map<Integer, Sheet> sheetMap;
|
||
|
private Workbook workbook;
|
||
|
private XSSFWorkbook xssfWorkbook;
|
||
|
private String sheetName;
|
||
|
private int sheetMaxRow = 1048575;
|
||
|
private ExcelBuildMonitor monitor = new ExcelBuildMonitorImpl();
|
||
|
private POIXMLDocumentPart poixmlDocumentPart;
|
||
|
private boolean isSxs;
|
||
|
|
||
|
public NewExcelWorkbookRowCreator(WatermarkBean parsedWatermarkBean, SXSSFWorkbook workBook, String sheetName) {
|
||
|
isSxs = true;
|
||
|
this.workbook = workBook;
|
||
|
this.xssfWorkbook = workBook.getXSSFWorkbook();
|
||
|
this.sheetName = sheetName;
|
||
|
this.init();
|
||
|
byte[] watterMarker = WsUtils.getWatterMarker(parsedWatermarkBean);
|
||
|
int pictureIdx = workbook.addPicture(watterMarker, Workbook.PICTURE_TYPE_PNG);
|
||
|
poixmlDocumentPart = xssfWorkbook.getAllPictures().get(pictureIdx);
|
||
|
}
|
||
|
|
||
|
public NewExcelWorkbookRowCreator(WatermarkBean parsedWatermarkBean, XSSFWorkbook workBook, String sheetName) {
|
||
|
isSxs = false;
|
||
|
this.workbook = workBook;
|
||
|
this.xssfWorkbook = workBook;
|
||
|
this.sheetName = sheetName;
|
||
|
this.init();
|
||
|
byte[] watterMarker = WsUtils.getWatterMarker(parsedWatermarkBean);
|
||
|
int pictureIdx = workbook.addPicture(watterMarker, Workbook.PICTURE_TYPE_PNG);
|
||
|
poixmlDocumentPart = xssfWorkbook.getAllPictures().get(pictureIdx);
|
||
|
}
|
||
|
|
||
|
private void init() {
|
||
|
this.sheetMap = new HashMap();
|
||
|
}
|
||
|
|
||
|
private Sheet getExcelSheet(int rowIndex) {
|
||
|
int sheetIndex = this.getSheetIndex(rowIndex);
|
||
|
if (!this.sheetMap.containsKey(sheetIndex)) {
|
||
|
Sheet sheet;
|
||
|
XSSFSheet wssheet;
|
||
|
String sheetName="";
|
||
|
if (sheetIndex == 0) {
|
||
|
sheetName= this.sheetName;
|
||
|
} else {
|
||
|
sheetName=this.sheetName + "_" + sheetIndex;
|
||
|
}
|
||
|
//先通过SXSSFWorkbook 创建这个sheet
|
||
|
sheet = this.workbook.createSheet(sheetName);
|
||
|
if (isSxs) {
|
||
|
//如果是SXSSFWorkbook表格,先拿他的XSSFWorkbook获取他真实的
|
||
|
wssheet=this.xssfWorkbook.getSheet(sheetName);
|
||
|
}else{
|
||
|
wssheet = (XSSFSheet) sheet;
|
||
|
}
|
||
|
//加水印
|
||
|
PackagePartName ppn = poixmlDocumentPart.getPackagePart().getPartName();
|
||
|
String relation = XSSFRelation.IMAGES.getRelation();
|
||
|
PackageRelationship pr = wssheet.getPackagePart().addRelationship(ppn, TargetMode.INTERNAL, relation, null);
|
||
|
wssheet.getCTWorksheet().addNewPicture().setId(pr.getId());
|
||
|
FineLoggerFactory.getLogger().error("添加sheet{}并 添加水印成功", sheetIndex);
|
||
|
//加水印结束
|
||
|
this.sheetMap.put(sheetIndex, sheet);
|
||
|
}
|
||
|
|
||
|
return (Sheet) this.sheetMap.get(sheetIndex);
|
||
|
}
|
||
|
|
||
|
private int getSheetIndex(int rowIndex) {
|
||
|
return rowIndex / this.sheetMaxRow;
|
||
|
}
|
||
|
|
||
|
private int getRealRowIndex(int rowIndex) {
|
||
|
return rowIndex % this.sheetMaxRow;
|
||
|
}
|
||
|
|
||
|
private Row createExcelRow(int rownum) {
|
||
|
Sheet sheet = this.getExcelSheet(rownum);
|
||
|
int rindex = this.getRealRowIndex(rownum);
|
||
|
Row row = sheet.getRow(rindex);
|
||
|
if (row != null) {
|
||
|
return row;
|
||
|
} else {
|
||
|
this.monitor.monitorAddRow();
|
||
|
return sheet.createRow(rindex);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private TableRow excelRowToTableRow(Row sxssfRow) {
|
||
|
return new ExcelTableRow(this.monitor, sxssfRow);
|
||
|
}
|
||
|
|
||
|
private void addMergedRegion(CellRangeAddress region) {
|
||
|
int firstRow = region.getFirstRow();
|
||
|
int lastRow = region.getLastRow();
|
||
|
int firstSheetIndex = this.getSheetIndex(firstRow);
|
||
|
int lastSheetIndex = this.getSheetIndex(lastRow);
|
||
|
if (firstSheetIndex == lastSheetIndex) {
|
||
|
Sheet sheet = this.getExcelSheet(firstRow);
|
||
|
CellRangeAddress rangeAddress = new CellRangeAddress(this.getRealRowIndex(firstRow), this.getRealRowIndex(lastRow), region.getFirstColumn(), region.getLastColumn());
|
||
|
sheet.addMergedRegion(rangeAddress);
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
public TableRow createRow(int rowNum) {
|
||
|
Row sxssfRow = this.createExcelRow(rowNum);
|
||
|
return this.excelRowToTableRow(sxssfRow);
|
||
|
}
|
||
|
|
||
|
public void addMergedRegion(TableRangeAddress region) {
|
||
|
CellRangeAddress address = new CellRangeAddress(region.getFirstRowIndex(), region.getLastRowIndex(), region.getFirstColIndex(), region.getLastColIndex());
|
||
|
this.addMergedRegion(address);
|
||
|
}
|
||
|
|
||
|
public TableRow getRow(int rowNum) {
|
||
|
Row row = this.getExcelRow(rowNum);
|
||
|
return row != null ? this.excelRowToTableRow(row) : null;
|
||
|
}
|
||
|
|
||
|
public Row getExcelRow(int rowNum) {
|
||
|
Sheet sheet = this.getExcelSheet(rowNum);
|
||
|
int realIndex = this.getRealRowIndex(rowNum);
|
||
|
return sheet.getRow(realIndex);
|
||
|
}
|
||
|
}
|