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

我目前正在使用Excel文件(.xlsx)在其中创build数据透视表的Java应用程序。

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

在我的testing代码中,我只是在示例的结尾“}”之前添加以下代码:

// 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; } 

控制台显示:“不应该:行是空的”。 它似乎说“创build的数据透视表不存在”。 我不明白为什么会发生。 那么,有没有另外一种方法来获取/读取/更改POI API创build的数据透视表中的数据?

先谢谢了!

到目前为止,Java POI不支持从数据透视表读取数据或打开数据透视表。 您可以使用VB.NET创build.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