我正在创建数据透视表,之前我已经将其中一行值作为列工作正常,但现在当我添加了一个更多列标签在数据透视表,它变得除去如何在数据透视表中使用apache poi添加多列作为列标签
这里是我的代码
public class ApacheCreatePivotTable
{
public static void main(String[] args) throws Exception
{
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet();
//Create some data to build the pivot table on
setCellData(sheet);
int endclonum = wb.getSheetAt(0).getRow(0).getLastCellNum()-1;
int endrownum = wb.getSheetAt(0).getLastRowNum();
AreaReference source = new AreaReference(new CellReference(0,0),new CellReference(endrownum,endclonum));
XSSFSheet sheet2 = wb.createSheet("pivot");
CellReference position = new CellReference("A1"); //convertColStringToIndex
XSSFPivotTable pivotTable = sheet2.createPivotTable(source, position, sheet);
pivotTable.addRowLabel(0);
pivotTable.addRowLabel(1);
pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(0).setDefaultSubtotal(false);
pivotTable.addColumnLabel(DataConsolidateFunction.AVERAGE, 3);
pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 3);
pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(2).setAxis(
org.openxmlformats.schemas.spreadsheetml.x2006.main.STAxis.AXIS_COL);
pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(2).addNewItems();
pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(2).getItems().addNewItem().setT(
org.openxmlformats.schemas.spreadsheetml.x2006.main.STItemType.DEFAULT);
FileOutputStream fileOut = new FileOutputStream("output.xlsx");
wb.write(fileOut);
fileOut.close();
wb.close();
}
public static void setCellData(XSSFSheet sheet)
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Row row1 = sheet.createRow(0);
// Create a cell and put a value in it.
Cell cell11 = row1.createCell(0);
cell11.setCellValue("Names");
Cell cell12 = row1.createCell(1);
cell12.setCellValue("falility");
Cell cell13 = row1.createCell(2);
cell13.setCellValue("date");
Cell cell14 = row1.createCell(3);
cell14.setCellValue("cost");
Cell cell15 = row1.createCell(4);
cell15.setCellValue("growth");
Row row2 = sheet.createRow(1);
Cell cell21 = row2.createCell(0);
cell21.setCellValue("tom");
Cell cell22 = row2.createCell(1);
cell22.setCellValue("Nal stop");
Cell cell23 = row2.createCell(2);
Calendar cal = Calendar.getInstance();
cal.set(2017,07,18);
cell23.setCellValue(sdf.format(cal.getTime()));
Cell cell24 = row2.createCell(3);
cell24.setCellValue(10);
Cell cell25 = row2.createCell(4);
cell25.setCellValue(.18);
Row row3 = sheet.createRow(2);
Cell cell31 = row3.createCell(0);
cell31.setCellValue("Ram");
Cell cell32 = row3.createCell(1);
cell32.setCellValue("Vadgao");
Cell cell33 = row3.createCell(2);
cal.set(2017,07,19);
cell33.setCellValue(sdf.format(cal.getTime()));
Cell cell34 = row3.createCell(3);
cell34.setCellValue(12);
Cell cell35 = row3.createCell(4);
cell35.setCellValue(.12);
Row row4 = sheet.createRow(3);
Cell cell41 = row4.createCell(0);
cell41.setCellValue("Terk");
Cell cell42 = row4.createCell(1);
cell42.setCellValue("Deccan");
Cell cell43 = row4.createCell(2);
cal.set(2017,07,20);
cell43.setCellValue(sdf.format(cal.getTime()));
Cell cell44 = row4.createCell(3);
cell44.setCellValue(11);
Cell cell45 = row4.createCell(4);
cell45.setCellValue(.35);
Row row5 = sheet.createRow(4);
Cell cell51 = row5.createCell(0);
cell51.setCellValue("tom");
Cell cell52 = row5.createCell(1);
cell52.setCellValue("baner");
Cell cell53 = row5.createCell(2);
cal.set(2017,07,18);
cell53.setCellValue(sdf.format(cal.getTime()));
Cell cell54 = row5.createCell(3);
cell54.setCellValue(20);
Cell cell55 = row5.createCell(4);
cell55.setCellValue(.50);
}
}
请帮助我,