C#。 Excel中。 需要以编程方式重新应用在控制台应用程序中打开的电子表格上的filter
我有一个使用Microsoft.Office.Interop.Excel参考的C#控制台应用程序。 该应用程序打开一个Excel电子表格,写入,保护工作表,保存并closuresExcel。 这一切工作。
电子表格在一张纸上有一个filter。 写入数据后,我需要应用程序自动重新应用filter。
我尝试使用Worksheets.AutoFilter.ApplyFilter()
。 这导致一个
未处理的exception
然而,它确实以一种forms工作。
// The two lines of code below cause the unhandled exception // in a console app but not in a form app. // Refresh the filter programmatically Excel.Worksheet ceSheet = Globals.xlApp.Worksheets["FAW Summary"]; ceSheet.AutoFilter.ApplyFilter();
我也尝试使用SendKeys.Send
和一个ApplicationContext
类来发送Ctrl – Alt – L键组合,重新申请。 这不会给我一个错误,但它也不会做任何事情时,在控制台应用程序中运行。
// Refresh the filter with SendKeys if (!Globals.interactiveMode) Application.Run(new AGMDataExtractConsoleContext());
AGMDataExtractConsoleContext
是ApplicationContext
类的一个类。 它应该从控制台运行一个应用程序,然后使用SendKeys.Send
传递它的击键。