Excel 互操作条件格式

发布于 2024-07-23 22:58:08 字数 399 浏览 3 评论 0原文

我刚刚发现以下页面:在 Excel 2007 中设置条件格式 这与我想做的事情非常相似,但我似乎找不到合适的函数来完成稍微不同的事情。

我想知道是否有人知道一种基于一组文本值将条件格式应用于范围的方法。 例如我想说:

如果您看到“InvalidValue1”或“InvalidValue2”突出显示红色 否则,如果您看到“警告”突出显示黄色,

我有一系列无效值,可能还有警告值。 我还需要对非常大的数据集逐列执行此操作,因此在可能的情况下,我想使用内置的 Excel 功能来突出显示范围内的错误。

有谁知道这是否可能?

I've just spotted the following page: Setting Conditional Formatting in Excel 2007 which is very similar to something I'd like to do, but I can't seem to find appropriate functions to do something slightly different.

I'm wondering if anyone knows a way to apply conditional formatting to a range, based upon a set of textual values. E.g. I want to say:

If you see "InvalidValue1" OR "InvalidValue2" Highlight RED
else if you see "WARNING" Highlight YELLOW

I have a whole range of invalid values, and possibly warning values. I also need to do this on a column by column basis for very large datasets, so where possible I'd like to use built in Excel features to highlight errors within the range.

Does anyone know if this is at all possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

稚气少女 2024-07-30 22:58:09
 using Excel = Microsoft.Office.Interop.Excel;
 ...
 object mis = Type.Missing;

 Excel.FormatCondition cond =
    (Excel.FormatCondition)range.FormatConditions.Add(Excel.XlFormatConditionType.xlCellValue,
    Excel.XlFormatConditionOperator.xlEqual, "1",
    mis, mis, mis, mis, mis);
    cond.Interior.PatternColorIndex = Excel.Constants.xlAutomatic;
    cond.Interior.TintAndShade = 0;
    cond.Interior.Color = ColorTranslator.ToWin32(Color.White);
    cond.StopIfTrue = false;
 using Excel = Microsoft.Office.Interop.Excel;
 ...
 object mis = Type.Missing;

 Excel.FormatCondition cond =
    (Excel.FormatCondition)range.FormatConditions.Add(Excel.XlFormatConditionType.xlCellValue,
    Excel.XlFormatConditionOperator.xlEqual, "1",
    mis, mis, mis, mis, mis);
    cond.Interior.PatternColorIndex = Excel.Constants.xlAutomatic;
    cond.Interior.TintAndShade = 0;
    cond.Interior.Color = ColorTranslator.ToWin32(Color.White);
    cond.StopIfTrue = false;
深空失忆 2024-07-30 22:58:09

如果您使用 .Net 4,以下是使用动态和命名参数的重写

dynamic range = sheet.Range("A2").Resize(rowCount, 11);

const string redCondition = "=OR(ERROR1, ERROR2, ERROR3)";

dynamic format = range.FormatConditions.Add(XlFormatConditionType.xlExpression, Formula1: redCondition);
format.Interior.Color = 0x0000FF;
format.Font.Color = 0x00FFFF;

If you're using .Net 4, the following is a rewrite using dynamics and named parameters

dynamic range = sheet.Range("A2").Resize(rowCount, 11);

const string redCondition = "=OR(ERROR1, ERROR2, ERROR3)";

dynamic format = range.FormatConditions.Add(XlFormatConditionType.xlExpression, Formula1: redCondition);
format.Interior.Color = 0x0000FF;
format.Font.Color = 0x00FFFF;
心病无药医 2024-07-30 22:58:08

我相信我已经找到了问题的解决方案(尽管单元格选择相当奇怪,而且我还没有完全解决这个问题。例如,我的公式使用 A1,由于选择的范围,它实际上意味着 C1)。

以下是我为其他感兴趣的人使用的代码:

string condition = @"=OR(ERROR1, ERROR2, ERROR3)";
var cfOR = (FormatCondition)targetSheet.get_Range("C1", "C10").FormatConditions.Add(XlFormatConditionType.xlExpression, Type.Missing,condition), Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

cfOR.Interior.Color = 0x000000FF;
cfOR.Font.Bold = true;
cfOR.Font.Color = 0x00FFFFFF;

请注意,FormatConditions.Add() 方法对于不同版本的 Excel 互操作具有不同的签名。

I believe I have managed to find a solution to the problem (although Cell selection is rather bizarre and I haven't quite sorted that out yet. e.g. my formula uses A1 which actually means C1 because of the selected range).

Here is the code I used for anyone else interested:

string condition = @"=OR(ERROR1, ERROR2, ERROR3)";
var cfOR = (FormatCondition)targetSheet.get_Range("C1", "C10").FormatConditions.Add(XlFormatConditionType.xlExpression, Type.Missing,condition), Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

cfOR.Interior.Color = 0x000000FF;
cfOR.Font.Bold = true;
cfOR.Font.Color = 0x00FFFFFF;

Note that the FormatConditions.Add() method has a different signature for different versions of the Excel interop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文