【发布时间】:2017-12-12 23:21:29
【问题描述】:
我有一个使用 Microsoft.Office.Interop.Excel 参考的 C# 控制台应用程序。该应用程序打开一个 Excel 电子表格,对其进行写入,保护工作表,保存它,然后关闭 Excel。这一切都有效。
电子表格在一张工作表的一部分上有一个过滤器。我需要应用程序在我写入数据后自动重新应用过滤器。
我尝试使用Worksheets.AutoFilter.ApplyFilter()。这会导致
未处理的异常
...但是它确实以某种形式工作。
// 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 传递击键。
标签: c# excel console autofilter