Excel Interop – 如何search格式的单元格?
我看到Excel互操作程序有Find方法,但我不知道如何使用它。
我想在范围(Microsoft.Office.Interop.Excel.Range)中search具有特定颜色的单元格。 在Excel中,你会这样做:
- Ctrl + F打开“查找和replace”对话框。 保留文本框“查找内容:”为空。
- 点击button[选项>>]或Alt + T
- 单击button[Format …]或Alt + M
- 转到选项卡字体 – >单击颜色选项(Alt + C)并select颜色。
- 点击确定,然后点击[查找下一个]
我只是想用Excel Interop来编程。
谢谢阅读 :)
您需要在调用Find()
方法之前设置Application.FindFormat
属性。
例如,如果要使用“查找和replace”对话框的标准设置来search红色单元格,则可以使用以下代码:
// These are the search options of the "Format" dialog. _application.FindFormat.Font.Color = 255; _application.FindFormat.Font.TintAndShade = 0; // cell is null if nothing was found. var cell = _application.Cells.Find(What: "", After: _application.ActiveCell, LookIn: XlFindLookIn.xlFormulas, LookAt: XlLookAt.xlPart, SearchOrder: XlSearchOrder.xlByRows, SearchDirection: XlSearchDirection.xlNext, // It's important to set SearchFormat to true. MatchCase: false, SearchFormat: true);
如果您想要search当前主题的重音颜色2,则也可以这样做:
_application.FindFormat.Font.ThemeColor = XlThemeColor.xlThemeColorAccent2;
如果要模拟“查找和replace”对话框的确切行为,可以在find单元格后调用cell.Activate()
。