JSD-8649 开源任务材料
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.

107 lines
3.8 KiB

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.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 java.util.HashMap;
import java.util.Map;
public class MyExcelWorkbookRowCreator implements WorkbookRowCreator {
Map<Integer, Sheet> sheetMap;
private Workbook workbook;
private String sheetName;
private int sheetMaxRow = 1048575;
private ExcelBuildMonitor monitor = new ExcelBuildMonitorImpl();
public MyExcelWorkbookRowCreator(Workbook workBook, String sheetName) {
this.workbook = workBook;
this.sheetName = sheetName;
this.init();
}
private void init() {
this.sheetMap = new HashMap();
}
private Sheet getExcelSheet(int rowIndex) {
int sheetIndex = this.getSheetIndex(rowIndex);
if (!this.sheetMap.containsKey(sheetIndex)) {
Sheet sheet;
if (sheetIndex == 0) {
sheet = this.workbook.createSheet(this.sheetName);
} else {
sheet = this.workbook.createSheet(this.sheetName + "_" + 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);
}
}