-->

Accessing Field Settings of Pivot Table using Apac

2020-07-24 03:26发布

问题:

I am creating a workbook with a sheet populated data from a data source then creating a second sheet with a pivot table view of that data. Everything works fine, but I can't seem to change the default look of the pivot table. I am trying to get the setting ( Row Labels-->Click one from the list-->Field Settings-->Subtotals-->None and Row Labels-->Click one from the list-->Field Settings-->Layout & Print-->'Show item labels in tabular form' ) checked while creating the pivot table but couldn't find the handle / flag in the POI. Tried finding something under pivotTable.getCTPivotTableDefinition() or pivotTable.getCTPivotTableDefinition().getPivotTableStyleInfo(), but no lock. Please advise if there is a way to set these settings using poi during pivot table creation, not after the fact following the steps mentioned in the parenthesis. Here is my pivot table code :

XSSFSheet sheet = (XSSFSheet)wb.createSheet("Data");
...
...
//filling data sheet, skipping this part as it's not relevant 
...
XSSFSheet pivotSheet = (XSSFSheet)wb.createSheet("Pivot Table");
AreaReference source = new AreaReference(sheet.getSheetName()+"!A$1:W$"+String.valueOf(sheet.getLastRowNum()));
CellReference position = new CellReference("A3");
XSSFPivotTable pivotTable = pivotSheet.createPivotTable(source, position);


        /* Add filters */
        pivotTable.addRowLabel(17);
        pivotTable.addRowLabel(20);
        pivotTable.addRowLabel(21);
        pivotTable.addRowLabel(22);
        pivotTable.addRowLabel(13);
        pivotTable.addRowLabel(19);
        pivotTable.addRowLabel(6);
        pivotTable.addRowLabel(0);
        pivotTable.addRowLabel(18);
        pivotTable.addRowLabel(1);
        pivotTable.addRowLabel(7);
        pivotTable.addRowLabel(9);

回答1:

Finally figured it out; lack of good documentation forced me to try a zillion things and finally was able to achieve what I wanted; here is the code :

for(CTPivotField ctPivotField:pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldList()){
            ctPivotField.setAutoShow(false);
            ctPivotField.setOutline(false);
            ctPivotField.setSubtotalTop(false);
            ctPivotField.setSubtotalCaption("");
        }


回答2:

instead of creating the pivot table every time, I created one template XLS file with all the desired styling and included that in the source, now I am opening that file filling the necessary data in the source tab, and saving the XLS file with the dynamic data with a different name; since the Pivot table tab is marked to refresh when opened, it does the work. Instead of going through the POI API with the limitations on Pivot Tables, creating a template and using it is much easier and flexible if you will generate the same styled pivot table for dynamic data.



回答3:

@ninjaxelite here how it goes :

       List<Object[]> resultSet = //get raw data


        XSSFWorkbook wb = null;
        try {
            wb = new XSSFWorkbook(new FileInputStream(this.getClass().getResource("/content/XLS_template.xlsx").getPath()));
        } catch (FileNotFoundException e1) {
            //error
        } catch (IOException e1) {
            //error
        }



        Map<String, CellStyle> styles = createStyles(wb); // some local function to get styles 
        XSSFSheet sheet = (XSSFSheet)wb.getSheetAt(0);

        XSSFRow row;
        XSSFCell cell;
        int rowNum = 0;
        for (Object[] aRow : resultSet) {
            rowNum++;
            row = sheet.createRow(rowNum);

            cell = row.createCell(0);
            cell.setCellValue((String)aRow[0]);
            cell.setCellStyle(styles.get("cell_normal_centered"));

             ...
             ..
             .