刷新数据透视表EPPlus

时间:2016-08-29 15:12:23

标签: c# pivot-table epplus

我正在使用EPPlus在C#中编辑现有的电子表格。我正在改变第二个工作表上的原始数据,该工作表被用作第一个工作表上的数据透视表的数据源。我的编辑工作完全正常,我遇到的问题是,当我加载电子表格输出时,我必须通过单击Excel工具栏上的Refresh Data按钮手动更新数据透视表。

无论如何使用C#EPPlus进行此操作?

我试过了:

package.Workbook.FullCalcOnLoad = true;

package.Workbook.Calculate();

没有成功。

更新

我找不到在EPPlus中执行此操作的机制,所以仍然想知道是否有答案。但是,因为我正在编辑预先存在的Excel文件,所以我能够在Excel中编辑现有数据透视表的属性,并将设置更改为在首次加载时自动更新。

4 个答案:

答案 0 :(得分:7)

我找不到使用EPPlus实现此目的的方法。

但是,您可以在修改文件之前手动启用数据透视表上的“在打开文件时刷新数据”属性,以便在使用Excel打开文件时,将根据修改后的数据计算数据透视表的内容数据。您可以在数据透视表选项中的数据选项卡下找到此属性。

答案 1 :(得分:0)

数据透视表在Excel文件中有自己的计算上下文。您使用FullCalcOnLoad属性和Calculate()方法

来处理公式的计算

我打赌this code from here会帮助你。

foreach (Worksheet sheet in package.Workbook.Sheets)
{
    foreach (PivotTable pivotTable in sheet.PivotTables())
    {
        pivotTable.PivotCache().Refresh(); //could be some other method, but i hope you find right one
    }
}

答案 2 :(得分:0)

将此添加到ExcelPivotCacheDefinition.cs

public bool RefreshOnLoad
{
    get
    {
        return GetXmlNodeString("@refreshOnLoad") == "1";
    }
    set
    {
        SetXmlNodeString("@refreshOnLoad", value?"1":"0");
    }
}

答案 3 :(得分:0)

  1. 转到“数据透视表”选项,然后选中“打开文件时刷新数据”:https://www.extendoffice.com/documents/excel/1859-excel-refresh-pivot-table-on-open.html

Refresh data when opening the file

  1. (可选)如果需要,用户可以在其Excel安装中禁用“受保护的视图”:http://www.corporatefocus.com/support/how-to-disable-protected-view-in-microsoft-excel#:~:text=In%20Excel%20go%20to%20File,Enable%20All%20Macros%20by%20default

Disable 'Protected View'

相关问题