到目前为止,这不是apache poi 提供的任何东西。如果知道Excel 在这种情况下存储了什么,就可以做到。
创建数据透视表的主要原理是相同的。不同之处在于数据透视缓存的工作表源附加包含一个rId,它指的是与源工作簿的外部关系。因此,额外的任务是创建与源工作簿的外部关系,并将 rId 设置为数据透视缓存的工作表源。
以下代码显示了如何做到这一点:
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.util.AreaReference;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.ss.usermodel.DataConsolidateFunction;
import org.apache.poi.openxml4j.opc.TargetMode;
import org.apache.poi.xssf.usermodel.*;
class CreatePivotTable {
public static void main(String[] args) throws Exception {
String[] headers = new String[] { "Company", "Status", "Counter" };
Object[][] sheetData = {
{"Company 1", "OK", "x"},
{"Company 1", "NOK", "x"},
{"Company 2", "NOK", "x"},
{"Company 1", "OK", "x"},
{"Company 3", "OK", "x"},
{"Company 1", "NOK", "x"},
};
//File sourceWorkbookFile = new File("C:/Users/axelr/Documents/PivotData.xlsx");
//File pivotWorkbookFile = new File("C:/Users/axelr/Documents/PivotTable.xlsx");
File sourceWorkbookFile = new File("PivotData.xlsx");
File pivotWorkbookFile = new File("PivotTable.xlsx");
// creating source workbook
try (XSSFWorkbook workbook = new XSSFWorkbook();
FileOutputStream fileout = new FileOutputStream(sourceWorkbookFile) ) {
XSSFSheet dataSheet = workbook.createSheet("Data");
XSSFRow row;
XSSFCell cell;
int r = 0;
row = dataSheet.createRow(r++);
int c = 0;
for (String header : headers) {
cell = row.createCell(c++);
cell.setCellValue(header);
}
for (Object[] dataRow : sheetData) {
row = dataSheet.createRow(r++);
c = 0;
for (Object value : dataRow) {
cell = row.createCell(c++);
if (value instanceof String) {
cell.setCellValue((String)value);
} //else if...
}
}
workbook.write(fileout);
}
// creating pivot workbook
try (XSSFWorkbook sourceWorkbook = new XSSFWorkbook(new FileInputStream(sourceWorkbookFile));
XSSFWorkbook pivotWorkbook = new XSSFWorkbook();
FileOutputStream fileout = new FileOutputStream(pivotWorkbookFile) ) {
XSSFSheet dataSheet = sourceWorkbook.getSheet("Data");
XSSFSheet pivotSheet = pivotWorkbook.createSheet("Pivot");
AreaReference areaReference = new AreaReference(
new CellReference(0, 0),
new CellReference(dataSheet.getLastRowNum(), dataSheet.getRow(0).getLastCellNum()-1),
SpreadsheetVersion.EXCEL2007);
XSSFPivotTable pivotTable = pivotSheet.createPivotTable(areaReference, new CellReference("A4"), dataSheet);
XSSFPivotCacheDefinition pivotCacheDefinition = pivotTable.getPivotCacheDefinition();
String rId = pivotCacheDefinition.getPackagePart().addRelationship(
sourceWorkbookFile.toURI(),
TargetMode.EXTERNAL,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/externalLinkPath")
.getId();
pivotCacheDefinition.getCTPivotCacheDefinition().getCacheSource().getWorksheetSource().setId(rId);
pivotTable.addRowLabel(0);
pivotTable.addRowLabel(1);
pivotTable.addColumnLabel(DataConsolidateFunction.COUNT, 2);
pivotWorkbook.write(fileout);
}
}
}
此代码已使用当前apache poi 版本4.1.2 和5.0.0 进行测试和工作。