You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Microsoft.Office.Interop.Excel实现饼图切片点击过滤数据透视表及事件问题

实现Excel饼图切片点击过滤数据透视表的正确代码

你之前的问题核心原因有两个:

  1. EPPlus是离线操作Excel文件的类库,无法触发或处理Excel的UI交互事件,必须使用Microsoft.Office.Interop.Excel操作运行中的Excel实例;
  2. 事件绑定的对象如果是局部变量,会被.NET垃圾回收器回收,导致事件无法触发,程序直接退出。

以下是完整可运行的C#实现代码:

using Microsoft.Office.Interop.Excel;
using System;

namespace ExcelChartFilterPivot
{
    class Program
    {
        // 必须声明为类级别的变量,防止被GC回收
        private static Application _excelApp;
        private static Workbook _workbook;
        private static Chart _pieChart;
        private static PivotTable _pivotTable;

        static void Main(string[] args)
        {
            // 初始化Excel应用
            _excelApp = new Application();
            _excelApp.Visible = true; // 必须设置为可见,否则事件无法触发

            // 打开目标工作簿(替换为你的文件路径)
            _workbook = _excelApp.Workbooks.Open(@"C:\YourPath\YourExcelFile.xlsx");
            
            // 获取数据透视表(替换为你的工作表名和数据透视表名)
            Worksheet pivotSheet = _workbook.Worksheets["PivotSheet"];
            _pivotTable = pivotSheet.PivotTables["PivotTable1"];

            // 获取饼图(替换为你的工作表名和图表名)
            Worksheet chartSheet = _workbook.Worksheets["ChartSheet"];
            _pieChart = chartSheet.ChartObjects["PieChart1"].Chart;

            // 绑定MouseUp事件
            _pieChart.MouseUp += PieChart_MouseUp;

            // 保持程序运行,否则退出后Excel进程会关闭
            Console.WriteLine("点击饼图切片测试过滤功能,按任意键退出...");
            Console.ReadLine();

            // 清理资源(生产环境建议添加)
            _workbook.Close(false);
            _excelApp.Quit();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(_pieChart);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(_pivotTable);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(_workbook);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(_excelApp);
        }

        private static void PieChart_MouseUp(int Button, int Shift, int x, int y)
        {
            try
            {
                // 获取点击的切片对应的系列点
                SeriesPoint clickedPoint = _pieChart.SeriesCollection(1).PointFromPixel(x, y);
                if (clickedPoint == null) return;

                // 获取切片对应的类别名称
                string categoryName = clickedPoint.Category;

                // 清除数据透视表的现有筛选
                _pivotTable.PivotFields("CategoryField").ClearAllFilters(); // 替换为你的透视表字段名

                // 筛选对应类别的记录
                _pivotTable.PivotFields("CategoryField").CurrentPage = categoryName;
            }
            catch (Exception ex)
            {
                Console.WriteLine($"操作出错:{ex.Message}");
            }
        }
    }
}

关键注意事项:

  • 必须安装本地Excel客户端,Microsoft.Office.Interop.Excel依赖Excel进程运行;
  • 安装NuGet包:在NuGet包管理器中搜索并安装 Microsoft.Office.Interop.Excel;
  • 类级别的变量(_excelApp、_workbook等)不能改为局部变量,否则垃圾回收器会回收这些对象,导致事件绑定失效;
  • 代码中的路径、工作表名、数据透视表名、字段名需要替换为你实际的内容;
  • 测试时保持控制台窗口打开,否则程序退出后Excel会自动关闭。

内容的提问来源于stack exchange,提问作者Joe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 02:45:10