如何通过 C# 删除 Excel 中的自定义列表

How to delete custom list in Excel by C#

我正在尝试通过 C# 语言删除 Excel 自定义列表中的列表。但是我的程序无法删除我想要的列表。我不知道我的程序有什么问题。它没有注意到任何错误。 我希望有人可以帮助我修复它。 非常感谢。

Object customListContents;
for (int i = 5; i <= excelApp.Application.CustomListCount; i++)
    {
        customListContents = excelApp.Application.GetCustomListContents(i);
        foreach (string item in (dynamic)(customListContents))           
                if (item == "China" || item == "Taiwan")                               
                    {
                        excelApp.Application.DeleteCustomList(i);
                         break; 
                    }                       
    }

我想你只需要这个:

Globals.ThisAddIn.Application.DeleteCustomList(Globals.ThisAddIn.Application.GetCustomListNum(new string[] { "China", "Taiwan" }));

这里是一样的,更简单但也更长:


// get an application object
Excel.Application xlApp = Globals.ThisAddIn.Application;

// assign the list you want to remove to an array
string[] myList = new string[] { "China", "Taiwan" };

// get the number of your list from Excel application
int listNum = xlApp.GetCustomListNum(myList);

// delete the list by its number - it's the only way to delete 
// a custom list that I've found
xlApp.DeleteCustomList(listNum);