简体   繁体   English

使用现有文件的 EPPlus 更改数据透视表的范围

[英]Change the range of pivot table with EPPlus of an existing file

Is it possible to change the range of pivot table and then refresh the values with EPPlus of an existening file?是否可以更改数据透视表的范围,然后使用现有文件的 EPPlus 刷新值?

Now I use EPPlus in conjunction with Excel Interopt... EPPlus add new sheet with data in an existing xlsx with a well defined pivot table... then I refresh the data source with code like this:现在我将 EPPlus 与 Excel Interopt 结合使用... EPPlus 在现有 xlsx 中添加带有数据的新工作表,并带有一个定义良好的数据透视表...然后我使用如下代码刷新数据源:

private bool RefreshPivotTable(string file, string sheetName, string pivotTableName, string sourceData)
    {
        try
        {
            Type excelType = Type.GetTypeFromProgID("Excel.Application");

            dynamic excel = Activator.CreateInstance(excelType);
            dynamic workbook = excel.Workbooks.Open(file);
            dynamic sheet = workbook.Sheets[sheetName];
            dynamic pivotTable = sheet.PivotTables(pivotTableName);

            pivotTable.SourceData = sourceData;
            pivotTable.RefreshTable();

            workbook.Save();
            workbook.Close();
            excel.Application.Quit();

            return true;
        }
        catch(Exception ex)
        {
            return false;
        }
    }

But I Would like do all with EPPlus.但我想用 EPPlus 做这一切。 Thanks.谢谢。

With EPPlus:使用 EPPlus:

            var excelWorkbook = new FileInfo(excelWorkbookPath);
            using (var excelPackage = new ExcelPackage(excelWorkbook))
            {
                var rawDataWorksheet = excelPackage.Workbook.Worksheets[RawDataWorksheetName];

                var pivotTableWorksheet = excelPackage.Workbook.Worksheets[PivotTableWorksheetName];
                pivotTableWorksheet.PivotTables[0 /* Or whichever index */].CacheDefinition.SourceRange = rawDataWorksheet.Cells[rawDataWorksheet.Dimension.Start.Row, rawDataWorksheet.Dimension.Start.Column, rawDataWorksheet.Dimension.End.Row, rawDataWorksheet.Dimension.End.Column];
            }

And if you're using a template Excel workbook (as opposed to creating a brand new workbook), you should also do the following:如果您使用的是模板 Excel 工作簿(而不是创建全新的工作簿),您还应该执行以下操作:

  1. Go to Pivot Table options and check 'Refresh data when opening the file': https://www.extendoffice.com/documents/excel/1859-excel-refresh-pivot-table-on-open.html .转到数据透视表选项并选中“打开文件时刷新数据”: https : //www.extendoffice.com/documents/excel/1859-excel-refresh-pivot-table-on-open.html

打开文件时刷新数据

  1. (Optional) If desired, users can disable 'Protected View' inside of their Excel installation: http://www.corporatefocus.com/support/how-to-disable-protected-view-in-microsoft-excel#:~:text=In%20Excel%20go%20to%20File,Enable%20All%20Macros%20by%20default . (可选)如果需要,用户可以在其 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

禁用“受保护的视图”

I am afraid that is possible only in part.恐怕这只是部分可能。

Looking at the EPPlus documentation , you will find documentation about Pivot Tables and its Pivot Cache with Source Range and similar properties.查看EPPlus 文档,您将找到有关数据透视表及其具有源范围和类似属性的数据透视缓存的文档。 But no account is given about refreshing pivot tables.但是没有考虑刷新数据透视表。 This should be done upon opening the workbook in Excel - maybe that is soon enough for you?这应该在 Excel 中打开工作簿时完成 - 也许这对您来说已经足够了?

Anyway, my understanding of EPPlus is reading manipulating the structure of Excel files , not doing any calculations in them - that remains in the domain of the Excel application itself.无论如何,我对 EPPlus 的理解是阅读操纵 Excel文件的结构,而不是在其中进行任何计算——这仍然属于 Excel 应用程序本身的领域。

Amendment: I have to correct myself: general calculation is possible as of EPPlus Version 4 according to this tutorial page , you can run worksheet.Calculate() and other commands.修正:我必须更正自己:根据 本教程页面,从 EPPlus 版本 4 开始可以进行一般计算,您可以运行worksheet.Calculate()和其他命令。 But still no mentioning of pivot tables.但仍然没有提到数据透视表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM