[英]Refresh Pivot table using Apache POI
沒有/關於 Apache 站點中數據透視表的 Apache POI 的最少文檔讓我寫了這個。
我想使用 Apache POI 刷新工作簿中的數據透視表。
請讓我知道我可以在哪里獲得有關此的適當文檔和示例。
請按照我所做的以下操作。
OFFSET()
或Named Table
創建動態范圍公式作為數據透視表的源數據並繪制數據透視表。只需右鍵單擊您的數據透視表並選擇
pivotTable Options->Data-> Check Refresh Data when opening File
打開MyFileName.xlsx
文件並填寫數據。
這就是全部......每當您打開工作簿時,它都會刷新為當前數據。 :-)
注意:當您通過 POI 創建數據透視表時,這將不起作用。
codeMan 所指的鏈接有一些看起來非常特定於 Apache POI 和 Excel 的建議。 你會看到這里沒有很好的文檔(不支持): http : //poi.apache.org/spreadsheet/limitations.html
在 codeMans 鏈接中逐字引用 Solitudes 答案:
有可能的。 在 PivotCacheDefinition 中,有一個屬性 refreshOnLoad 可以設置為 true。 然后在打開工作簿時刷新緩存。 更多信息在這里。
> 在 POI 中,這可以通過調用方法 setRefreshOnLoad(boolean bool) 來完成,該方法將布爾值作為參數,在 CTPivotCacheDefinition 上
如果您需要在打開文件之前刷新數據透視表,(例如,然后在進一步計算中使用數據透視表計算出的數據並讓 POI 編寫此內容),那么我不確定這對於 POI 是否可行,並且使用 COM 解決方案可能連接到 excel 可能是要走的路。
除了限制之外,您還可以查看有關Package org.apache.poi.hssf.record.pivottable的一些信息。
雖然如果我必須這樣做,我會手動創建表格/圖表一次,並將使用 apache poi 更新圖表,就像我在這里所做的那樣
將帶有 PT 的文件另存為文件。 xlsm並插入 VBA 腳本 (ALT+F11):
' Create module and insert this:
Public Const pivotName1 As String = "myPivotName"
Public Const sourceSheetName As String = "source"
Public Const sourceColumnCount As Long = 23
' In "ThisWorkbook" chapter insert this:
Dim lRow As Long
Private Sub Workbook_Open()
Application.ScreenUpdating = False
ActiveWorkbook.Worksheets(sourceSheetName).Activate
' In file should preliminarily insert keyWord "firstOpenFlag" in CV1 cell (sheet sourceSheetName)
' It gona start actions below
If ActiveSheet.Cells(1, 100) = "firstOpenFlag"
Then
ActiveSheet.Cells(1, 100) = ""
lRow = getLastRowForFirstCol(sourceSheetName)
Call updateAllPTCache
ActiveWorkbook.Worksheets(sourceSheetName).Activate
ActiveSheet.Range("A1").Select
End If
Application.ScreenUpdating = True
End Sub
Private Function getLastRowForFirstCol(sourceSheetName As String) As Long
ActiveWorkbook.Worksheets(sourceSheetName).Activate
getLastRowForFirstCol = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
If getLastRowForFirstCol < 2 Then getLastRowForFirstCol = 2
End Function
Private Sub updateAllPTCache()
Dim pt As PivotTable
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
For Each pt In ws.PivotTables
pt.ChangePivotCache ActiveWorkbook.PivotCaches.Create( _
SourceType:=xlDatabase, _
SourceData:=sourceSheetName + "!R1C1:R" + CStr(lRow) + "C" + CStr(sourceColumnCount), _
Version:=xlPivotTableVersion14)
' xlPivotTableVersion14 - work in 2013, 2016 exlApp
' Downgrade xlPivotTableVersion for backward compatibility
pt.RefreshTable
Next pt
Next ws
End Sub
缺點:客戶端 xlsApp 應配置為啟用 VBA 腳本
另一種解決方案(WO VBA 腳本)
在tempalate.xlsx 中,在源記錄集的標題上創建 xlTable 對象。 將名稱設置為 xlTable,例如。 '我的源表'。 同樣在數據透視表的文件預設中:
在興趣點中:
private void updateXlTableSource() {
XSSFTable sourceTable = ((XSSFWorkbook)workbook).getTable("mySourceTable");
CTTable ctTable = sourceTable.getCTTable();
String sourceRef = getSourceDataRange().formatAsString();
ctTable.setRef(sourceRef);
ctTable.getAutoFilter().setRef(sourceRef);
}
private CellRangeAddress getSourceDataRange() {
XSSFSheet xssfSheet = (XSSFSheet) workbook.getSheet("sourceSheetName");
int uBoundSourceDataRow = findFirstEmptyRowFrom(xssfSheet) - 1;
if (uBoundSourceDataRow < 2) {
uBoundSourceDataRow = 2;
}
int uBoundSourceDataCol = findFirstEmptyColFromFirstRow(xssfSheet) - 1;
return new CellRangeAddress(0, uBoundSourceDataRow, 0, uBoundSourceDataCol);
}
注意:檢查您的tempalate.xlsx是否有未知查詢。 如果存在則刪除,否則將阻止 PT 更新
缺點:PT 的自動過濾器包含不存在的元素(來自 PT 模板的元素)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.