我想知道如何更新现有的数据透视表数据源.我正在使用Microsoft.Office.Interop.ExcelExcel 2010 来定位用户.
我目前能够刷新数据透视表,它工作正常,但是当添加更多行时,我想在数据透视表数据源中包含这些行.例如,在Excel中查看时的数据透视表数据源是,DataSourceWorksheet!$A$2:$U$26并且我希望DataSourceWorksheet!$A$2:$U$86在更新/刷新Excel文件代码运行后将其更改为(60多行).
这是我的代码的简化版本
Application excelApplication = new Application();
Workbooks workbooks = excelApplication.Workbooks;
wrkBook = workbooks.Open(EXCEL_DOCUMENT_PATH, Missing.Value, false, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, true, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value);
/* update data source worksheet code here (omitted for example) */
Worksheet pivotWorkSheet = (Worksheet)wrkBook.Sheets[PIVOT_SHEET_NAME];
PivotTable pivot = (PivotTable)pivotWorkSheet.PivotTables(pivotTableName);
/* I would like to update the pivot tables data source here...
I could use Range object from data source worksheet...
Basically just want to tell pivot table, use these cells now... */
pivot.RefreshTable();
/* cleanup code here (omitted for example) */
Run Code Online (Sandbox Code Playgroud)
可能有效的解决方案是使用再次重新生成数据透视表wrkBook.PivotTableWizard( ... ).但是,这对我的情况不起作用,因为用户更愿意通过更改选定的字段,行,列等来修改其数据透视表.只需在数据源工作表更改时进行更新.
小智 7
它很简单..
pivot.SourceData = "SheetName!R1C1:R18C18"
pivot.RefreshTable();
Run Code Online (Sandbox Code Playgroud)
就这样..
请记住,我们总是需要提供行和列格式.
例如
"SheetName!" + R + Rowstartingnumber + C + Column StartingNumber : R + Rowendnumber + C + Column Endnumber.
Run Code Online (Sandbox Code Playgroud)
就是
"SheetName!R1C1:R12C13"这样.
如果你这样给"SheetName!$A$1:$Q$18",它将无法正常工作.
| 归档时间: |
|
| 查看次数: |
11006 次 |
| 最近记录: |