【问题标题】:C# Excel PivotItem VisibilityC# Excel PivotItem 可见性
【发布时间】:2019-04-08 17:22:47
【问题描述】:

我想使用 C# 从 Excel 过滤器中获取复选框的值。

所以...

Proj_1 should be true
Proj_2 should be true
Proj_3 should be false
Proj_4 should be false

以下是我当前使用的代码,但 item.Visible 始终为 false,无论 UI 是否有检查(除非我取消选中 Select Multiple Items),但我需要检查

  foreach (PivotField fld in pivotFields)
                {
                    Console.WriteLine(fld.Name + " -- " + fld.Orientation + " -- " + fld.EnableItemSelection + " -- " + fld.EnableMultiplePageItems + " -- ");

                    foreach (PivotItem item in fld.PivotItems(Type.Missing))
                    {
                        if (item.Visible == true)
                        {
                            Console.WriteLine("Item"  + item.Name + " in field " + fld.Name + "is Visible  (Selected)");
                        }
                        else
                        {
                            Console.WriteLine("Item" + item.Name + " in field " + fld.Name + "is Hidden (Not Selected)");
                        }
                    }

                }

【问题讨论】:

  • minimal reproducible example 确实是在 StackOverflow 中获得答案的第一步。按照您写问题的方式,可以回答的人不超过 100 人(其中 95 人可能在 Microsoft 担任 Excel 工程师)。如果添加了整个代码,人数会跳到 100^3。

标签: c# excel vba


【解决方案1】:

如果添加了 Microsoft.Office.Interop.Excel 库,这是可行的:

namespace ExcelAtSomething
{
    using System;
    using Excel = Microsoft.Office.Interop.Excel;

    class Startup
    {
        static void Main()
        {
            string filePath = @"C:\Users\stackoverflow\Desktop\Sample.xlsx";
            Excel.Application excel = new Excel.Application();
            excel.Visible = true;
            excel.EnableAnimations = true;

            Excel.Workbook wkb = Open(excel, filePath);

            foreach (Excel.Worksheet xlWorksheet in wkb.Worksheets)
            {
                Excel.PivotTables pivotTablesCollection = xlWorksheet.PivotTables();
                if (pivotTablesCollection.Count > 0)
                {
                    for (int i = 1; i <= pivotTablesCollection.Count; i++)
                    {
                        Excel.PivotTable currentPivotTable = pivotTablesCollection.Item(i);
                        Console.WriteLine($"Table is named -> {currentPivotTable.Name}");

                        foreach (Excel.PivotField pivotField in currentPivotTable.PivotFields())
                        {
                            Console.WriteLine($"\nField is named -> {pivotField.Name}");
                            foreach (Excel.PivotItem visibleItems in pivotField.VisibleItems)
                            {
                                Console.WriteLine($"Visible item name -> {visibleItems.Name}");
                            }

                            foreach (Excel.PivotItem PivotItem in pivotField.PivotItems())
                            {
                                Console.WriteLine($"Item is named -> {PivotItem.Name}");
                                Console.WriteLine(PivotItem.Visible);
                            }
                        }
                    }
                }
            }

            excel.EnableAnimations = true;
            wkb.Close(true);
            excel.Quit();
            Console.WriteLine("Finished!");
        }

        private static Excel.Workbook Open(Excel.Application excelInstance,
                string fileName, bool readOnly = false,
                bool editable = true, bool updateLinks = true)
        {
            Excel.Workbook book = excelInstance.Workbooks.Open(
                fileName, updateLinks, readOnly,
                Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                Type.Missing, editable, Type.Missing, Type.Missing, Type.Missing,
                Type.Missing, Type.Missing);
            return book;
        }
    }
}

诀窍在于,除了PivotItem,还有一个 PivotField,它有一个可见项目的列表。如果显示这些,则只显示可见的:

试一试并可能自定义一个函数,返回不在该列表中的不可见项目,这对于 Excel 来说是一个不错的选择。

【讨论】:

  • 是的,我实际上想知道检查了哪些值......就像我的示例 Proj_1 /.. True Proj_2 ... true 我不关心数据透视表中的实际数据。
  • 为什么我看不到.PivotFields() 收藏?我正在使用最新版本的Interop
  • 仅供参考,如果它可以帮助任何人,这就是我必须访问数据透视字段的方式:currentPivotTable = pivotTablesCollection.Item("ptName"); vf = (Excel.PivotFields)currentPivotTable.VisibleFields; foreach (Excel.PivotField f in vf) { if (f.Name == "fieldName") f.BaseItem = "20200101"; }
猜你喜欢
  • 2018-12-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-18
  • 2015-01-10
  • 1970-01-01
  • 1970-01-01
  • 2019-04-30
  • 2011-01-18
相关资源
最近更新 更多