[英]How to change the pivot table style from default blue to other colors using apache-poi
當我使用下面的代碼創建一個數據透視表時,它帶有一些默認的模板樣式(藍色)。 如何使用apache-poi庫更改此透視表的默認樣式
pivotTable = sheet2.createPivotTable(aref, new CellReference("A4"), sheet1);
XXSPivotTable
默認使用命名樣式PivotStyleLight16
。 請參見setDefaultPivotTableDefinition 。
XSSF
還沒有方法可以在高級XSSF
類中更改它。 卜我們可以得到下面的低級別CTPivotTableStyle
形成CTPivotTableDefinition
通過了XSSFPivotTable.getCTPivotTableDefinition 。 然后我們可以使用CTPivotTableStyle.setName
來設置另一個命名樣式:
pivotTable.getCTPivotTableDefinition().getPivotTableStyleInfo().setName("PivotStyleMedium7");
完整的例子:
import java.io.FileOutputStream;
import org.apache.poi.ss.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.*;
import org.apache.poi.xssf.usermodel.*;
class CreatePivotTableStyle {
public static void main(String[] args) throws Exception {
try (Workbook workbook = new XSSFWorkbook();
FileOutputStream fileout = new FileOutputStream("Excel.xlsx") ) {
Sheet pivotSheet = workbook.createSheet("Pivot");
Sheet dataSheet = workbook.createSheet("Data");
Row row;
Cell cell;
Object[][] data = new Object[][]{
new Object[]{"Name", "Count"},
new Object[]{"A", 2d},
new Object[]{"B", 4d},
new Object[]{"A", 1d},
new Object[]{"B", 7d}
};
for (int r = 0; r < data.length; r++) {
row = dataSheet.createRow(r);
Object[] rowData = data[r];
for (int c = 0; c < rowData.length; c++) {
cell = row.createCell(c);
if (rowData[c] instanceof String) {
cell.setCellValue((String)rowData[c]);
} else if (rowData[c] instanceof Double) {
cell.setCellValue((Double)rowData[c]);
}
}
}
AreaReference arerReference = new AreaReference("A1:B5", SpreadsheetVersion.EXCEL2007);
XSSFPivotTable pivotTable = ((XSSFSheet)pivotSheet).createPivotTable(arerReference, new CellReference("A4"), dataSheet);
pivotTable.addRowLabel(0);
pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 1, "Sum of count");
pivotTable.getCTPivotTableDefinition().getPivotTableStyleInfo().setName("PivotStyleMedium7");
workbook.write(fileout);
}
}
}
可以從PivotTable Tools
選項卡 - Design
Excel
GUI
中獲取可能的命名樣式的名稱。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.