简体   繁体   English

使用Worksheet.Range.Autofilter(Field,Criteria,Operator)按特定颜色索引/ RGB过滤范围

[英]Filter Range by specific Color Index/RGB using Worksheet.Range.Autofilter(Field,Criteria,Operator)

I am doing some automated excel manipulation using C#. 我正在使用C#执行一些自动化的excel操作。 I have been having a hard time figuring out how to autofilter based on a specific color. 我一直很难弄清楚如何根据特定的颜色自动过滤。

There is very little documentation about this type of manipulation, however I have found some VB.net and VBA code for it. 关于这种类型的操纵的文档很少,但是我发现了一些VB.net和VBA代码。 I cannot seem to convert the code to C# as "RGB" is not usable as it is in VB.net and VBA (See VB.net code below). 我似乎无法将代码转换为C#,因为“ RGB”不能像在VB.net和VBA中那样使用(请参阅下面的VB.net代码)。

Since there has been no answers to this questions, I want to specify the code that needs to be looked at. 由于此问题没有答案,因此我想指定需要查看的代码。 In Autofilter(Field,Criteria,Operator), I need to know the C# Microsoft.Office.Interop.Excel criteria that would let me choose a color to filter. 在Autofilter(Field,Criteria,Operator)中,我需要了解C#Microsoft.Office.Interop.Excel标准,该标准可以让我选择要过滤的颜色。

Here is what my code looks like: 这是我的代码:

Excel.Worksheet xs1:
Excel.Range xRange1;
Excel.Range xRange2;
Excel.Range lastrow;
Excel.Range lastcol; 
lastrow = xs1.Rows.End[Excel.XlDirection.xlDown];
lastcol = xs1.Columns.End[Excel.XlDirection.xlToRight];   
xRange1 = xs1.Cells[2, 14];
xRange2 = xs1.Cells[lastrow.Row, 14];

Below selects the entire sheet and adds an autofilter(), setting it to filter for color. 下面选择整个工作表并添加一个autofilter(),将其设置为过滤颜色。 This works fine, but how do I pick the color I want it to filter for? 这可以正常工作,但是如何选择要过滤的颜色呢?

 xs1.Range["A1", xs1.Cells[lastrow.Row, lastcol.Column]].
 AutoFilter(14,Excel.XlColorIndex.xlColorIndexAutomatic,
 Excel.XlAutoFilterOperator.xlFilterCellColor);

Here is an example of what the autofilter code would look like in VB.net. 这是VB.net中自动过滤器代码的示例。 It looks very similar to this in an excel macro as well. 在excel宏中看起来也与此非常相似。

xs1.Range("A1", xs1.Cells(lastrow.Row, lastcol.Column)).
AutoFilter(Field:=14,Criteria1:=RGB(0,202,255),
Operator:=Excel.XlAutoFilterOperator.xlFilterCellColors)

So this is how you pick the color index for any poor souls that need to figure it out themselves. 因此,这是您为需要自行计算的贫穷灵魂选择颜色索引的方式。 I could not find this anywhere on the internet. 我在互联网上的任何地方都找不到。

xs1.Range["A1", xs1.Cells[lastrow.Row, lastcol.Column]].
AutoFilter(14, xlBook.Colors[33], Excel.XlAutoFilterOperator.xlFilterCellColor);

The important part is the "xlBook.Colors[33]". 重要的部分是“ xlBook.Colors [33]”。 xlBook being the Workbook. xlBook是工作簿。 33 being the color index. 33是颜色指数。

I don't have any reputation points, so I must submit an answer instead of a comment. 我没有任何声誉得分,因此我必须提交答案而不是发表评论。 Anyway, thank you, thank you, thank you. 无论如何,谢谢,谢谢,谢谢。 I have spent weeks looking for this answer and happened to search the right key words to find this post. 我花了数周的时间寻找这个答案,并且碰巧搜索了正确的关键词来找到这篇文章。 I signed up just to upvote the answer! 我报名只是为了答覆答案!

My scenario is not exactly like yours, but similar enough that you led me to the solution. 我的情况与您的情况不完全相同,但足够相似,您使我找到了解决方案。 So, I will share what worked for me. 因此,我将分享对我有用的东西。 I am trying to filter by color index using PowerShell. 我正在尝试使用PowerShell按颜色索引进行过滤。 However, PowerShell does not allow the RGB values, as in the VB.net example above. 但是,PowerShell不允许RGB值,如上面的VB.net示例中所示。 Now, for my contribution to the answer. 现在,为我对答案的贡献。 If you are doing this in PowerShell then it will need to look like this: 如果您在PowerShell中执行此操作,则它将需要如下所示:

$xlFilterCellColor = 8
$xl = New-Object -comobject excel.application 
$xl.Visible = $true
$xl.DisplayAlerts = $False
$wb = $xl.Workbooks.Open("\\path\to\file.xlsx")
$ws = $wb.worksheets.items(1)
$xlCellTypeLastCell = 11
$used = $ws.usedRange
$lastCell = $used.SpecialCells($xlCellTypeLastCell)
$range = $ws.range(A1:$lastCell)
$range.select | out-null
$range.autofilter(1,$wb.colors(6),$xlFilterCellColor)

The operator decimal values are listed here. 运算符十进制值在此处列出 In the code above, I am filtering yellow colored cells. 在上面的代码中,我正在过滤黄色的单元格。 Cell index colors and values can be found here. 单元格索引的颜色和值可以在这里找到

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM