无法使用POI 3.11 beta1从数据透视表中读取数据

时间:2014-10-19 05:51:57

标签: java excel apache-poi pivot-table

我目前正致力于使用excel文件(.xlsx)在其中创建数据透视表的Java应用程序。

从POI 3.11-beta1,我发现它支持创建数据透视表。这让我很兴奋,我尝试了创建这些表的示例,可以在https://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreatePivotTable.java获得。有用!但是当我尝试使用POI从创建的数据透视表中读取数据时,它无法正常工作。

在我的测试代码中,我只是在结束之前添加以下代码'}'在示例中:

// get the content of Cell "I7", should be "10"
    FileInputStream fileInputStream = new FileInputStream("ooxml-pivottable.xlsx");
    XSSFWorkbook xssfWorkbook = (XSSFWorkbook) WorkbookFactory.create(fileInputStream);
    xssfWorkbook.setForceFormulaRecalculation(true);
    Sheet sheet1 = xssfWorkbook.getSheetAt(0);
    // Cell I7
    Row row = sheet1.getRow(6);
    if (row == null) {
        System.out.println("Not expected: row is null");
        return;
    }
    Cell cellI7 = row.getCell(8);
    if (cellI7 != null && cellI7.getCellType() == Cell.CELL_TYPE_NUMERIC) {
        System.out.println("The content of Cell I7 = "+cellI7.getNumericCellValue());
    } else {
        System.out.println("Not expected: cell is null");
        return;
    }       

控制台显示:"不期望:行为空"。它似乎说"创建的数据透视表不存在"。我无法理解为什么会这样。 那么,是否有另一种方法可以使用POI api从创建的数据透视表中获取/读取/更改数据?

感谢高级!

1 个答案:

答案 0 :(得分:0)

到目前为止,Java POI不支持从数据透视表中读取数据或打开数据透视表。您可以使用VB.NET创建.exe文件,该文件在单独的工作表中打开数据透视表。

代码:

Module Module1

    Sub Main()
        Dim wb1 As Excel.Workbook
        Dim oExcelFile As Object
        Dim grandT As String
        Try
            oExcelFile = GetObject(, "Excel.Application")
        Catch
            oExcelFile = CreateObject("Excel.Application")
        End Try

        Dim mydir As DirectoryInfo = New DirectoryInfo("C:\ReconciliareFiles\ReconPlusFiles\PivotTable_temp")
        Dim f As FileInfo() = mydir.GetFiles()
        Dim file As FileInfo = Nothing
        For Each file In f
            Console.WriteLine("File Name: {0} Size: {1}  ", file.FullName, file.Length)
            Exit For
        Next file

        grandT = "Grand Total"
        Console.WriteLine("Grand Total is written as:{0}  ", grandT)
        wb1 = oExcelFile.Workbooks.Open(file.FullName)
        Dim sheet As Excel.Worksheet = wb1.Worksheets(1)
        For i As Integer = 1 To 100
            Console.WriteLine("cell are:{0}  ", sheet.Cells(i, 2).Value)
            If sheet.Cells(i, 2).Value = grandT Then
                Console.WriteLine("cell 13 2 is  :{0}", wb1.Worksheets(1).Cells(13, 2).Value)
                oExcelFile.Range(sheet.Cells(i, 3), sheet.Cells(i, 3)).ShowDetail = True
                oExcelFile.DisplayAlerts = False
                Exit For
            End If
        Next
        oExcelFile.ActiveWorkbook.Save()
        oExcelFile.ActiveWorkbook.close()
        oExcelFile.Workbooks.Close()
        ReleaseComObject(sheet)
        ReleaseComObject(wb1)


    End Sub

    Private Sub ReleaseComObject(ByVal obj As Object)
        Try
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
            obj = Nothing
        Catch ex As Exception
            obj = Nothing
        End Try
    End Sub

End Module