有什么方法可以使用Java代码从已创建的数据透视表中读取数据?

时间:2019-05-02 07:02:51

标签: java apache-poi excel-pivot

我正在使用poi创建数据透视表并将其保存到另一张表中,但是在那之后,我想读取最近创建的数据透视表的数据,但是以某种方式我无法获取它 我正在使用以下方法创建Pivot:

    File file = new File(filepath);
    try (FileInputStream inputStream = new FileInputStream(file);)

    {

        XSSFWorkbook testData = null;
        testData = new XSSFWorkbook(inputStream);
        XSSFSheet testDataSheet = testData.getSheet(sheetname);
        XSSFSheet sheet2 = testData.createSheet("pivot");
        XSSFPivotTable pivotTable = sheet2.createPivotTable(new AreaReference(range, null), new CellReference("A1"), testDataSheet);

        pivotTable.addRowLabel(rownum);
        pivotTable.addColumnLabel(dataConsolidateFunction, value);

        FileOutputStream fileOut = new FileOutputStream(pivotsheetpath);
        testData.write(fileOut);
        fileOut.close();
        inputStream.close();
        testData.close();

    }

}

从excel获取数据

    String[][] data = null;
    File file = new File(filePath);

    try (FileInputStream inputStream = new FileInputStream(file);)
    {

        int row = 0;
        int column = 0;

        DataFormatter dataFormatter = new DataFormatter();

        XSSFWorkbook testData = null;
        String fileExtensionName = FilenameUtils.getExtension(filePath);
        if (fileExtensionName.equals("xlsx"))
            testData = new XSSFWorkbook(inputStream);

        XSSFSheet testDataSheet = testData.getSheet(sheetName);
        row = testDataSheet.getLastRowNum() - testDataSheet.getFirstRowNum();
        column = testDataSheet.getRow(0).getLastCellNum() - testDataSheet.getRow(0).getFirstCellNum();
        data = new String[row + 1][column];

        for (Row rows : testDataSheet)
            for (Cell cell : rows)
                data[rows.getRowNum()][cell.getColumnIndex()] = dataFormatter.formatCellValue(cell);
        testData.close();
        return data;
    }

0 个答案:

没有答案