有没有办法使用apache poi将数据显示为数据透视表中总计的百分比?

时间:2019-09-12 09:45:40

标签: apache-poi

我想在数据透视表中添加一个新列,其值字段设置为“显示为总计的百分比”。

这是我得到的输出:

Row Labels  Sum

Jane             20
Tarzan            5
Terk             10
Grand Total  35

这是预期的输出:

Row Labels  Sum
Jane          57.14%
Tarzan        14.29%
Terk          28.57%
Grand Total   100.00%

这是代码:默认情况下,我们使用DataConsolidateFunction.SUM,但是我想通过POI将数据显示为总计的百分比,就像我们可以在excel中一样

import java.io.*;
import java.util.Iterator;
import java.util.List;

import org.apache.*;
import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataConsolidateFunction;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.AreaReference;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.usermodel.XSSFPivotTable;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPivotField;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPivotFields;


public class App 
{
    public static void main( String[] args )
    {

            XSSFWorkbook wb = new XSSFWorkbook();

            XSSFSheet sheet1 = wb.createSheet("1e");
            XSSFSheet sheet = wb.createSheet("1econtent");


            setCellData(sheet,wb);
            wb.getSheet("1econtent").setSelected(false);

            AreaReference source = new AreaReference("A1:D5");
            CellReference position = new CellReference(8,0);

            XSSFPivotTable pivotTable = sheet1.createPivotTable(source, position,wb.getSheet("1econtent"));
             pivotTable.addRowLabel(0);
             pivotTable.addRowLabel(3);

            pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 1);


            try (FileOutputStream fileOut = new FileOutputStream("ooxml-pivottablesa.xlsx")) {
            wb.write(fileOut);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    public static void setCellData(XSSFSheet sheet,XSSFWorkbook wb){
        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("Confinements");
        Cell cell13 = row1.createCell(2);
        cell13.setCellValue("ConfinementsAS");
        Cell cell14 = row1.createCell(3);
        cell14.setCellValue("Human");


        Row row2 = sheet.createRow(1);
        Cell cell21 = row2.createCell(0);
        cell21.setCellValue("Jane");
        Cell cell22 = row2.createCell(1);
        cell22.setCellValue(10);
        Cell cell23 = row2.createCell(2);
        cell23.setCellValue(100);
        Cell cell24 = row2.createCell(3);
        cell24.setCellValue("Yes");

        Row row3 = sheet.createRow(2);
        Cell cell31 = row3.createCell(0);
        cell31.setCellValue("Tarzan");
        Cell cell32 = row3.createCell(1);
        cell32.setCellValue(5);
        Cell cell33 = row3.createCell(2);
        cell33.setCellValue(90);
        Cell cell34 = row3.createCell(3);
        cell34.setCellValue("Yes");

        Row row4 = sheet.createRow(3);
        Cell cell41 = row4.createCell(0);
        cell41.setCellValue("Terk");
        Cell cell42 = row4.createCell(1);
        cell42.setCellValue(10);
        Cell cell43 = row4.createCell(2);
        cell43.setCellValue(90);
        Cell cell44 = row4.createCell(3);
        cell44.setCellValue("No");
        cell44.getCellStyle().setHidden(true);

        Row row5 = sheet.createRow(4);
        Cell cell211 = row5.createCell(0);
        cell211.setCellValue("Jane");
        Cell cell221 = row5.createCell(1);
        cell221.setCellValue(10);
        Cell cell231 = row5.createCell(2);
        cell231.setCellValue(60);
        Cell cell241 = row5.createCell(3);
        cell241.setCellValue("No");
        cell241.getCellStyle().setHidden(true);

    }

    }

1 个答案:

答案 0 :(得分:1)

XSSFPivotTable中尚未实现将值显示为列总数的功能。但是我们可以使用CT*的基础底层ooxml-schemas类进行设置。

示例:

...
   pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 1);
   pivotTable.getCTPivotTableDefinition().getDataFields().getDataFieldArray(0).setShowDataAs(
    org.openxmlformats.schemas.spreadsheetml.x2006.main.STShowDataAs.PERCENT_OF_COL);
   DataFormat dataformat = wb.createDataFormat();
   short numFmtId = dataformat.getFormat("0.00%");
   pivotTable.getCTPivotTableDefinition().getDataFields().getDataFieldArray(0).setNumFmtId(numFmtId);
...