C# 更改行的颜色

C# 更改行的颜色,c#,excel-interop,xlsm,C#,Excel Interop,Xlsm,我有一个excel文件,我需要检查B列的特定范围,例如范围:1:3000,如果存在与新单元格和空单元格不同的值,那么我需要将B列的灰色行颜色更改为O。如下所示: 我怎样才能用C来做这个?如有必要,我更喜欢Microsoft.Interop.Excel而不是其他。。。 我已经检查了类似的答案,最后我尝试了类似的方法,但什么都没发生。。。最简单的解决方案是什么 using Excel=Microsoft.Office.Interop.Excel; using System; using Micro

我有一个excel文件,我需要检查B列的特定范围,例如范围:1:3000,如果存在与新单元格和空单元格不同的值,那么我需要将B列的灰色行颜色更改为O。如下所示:

我怎样才能用C来做这个?如有必要,我更喜欢Microsoft.Interop.Excel而不是其他。。。 我已经检查了类似的答案,最后我尝试了类似的方法,但什么都没发生。。。最简单的解决方案是什么

using Excel=Microsoft.Office.Interop.Excel;
using System;
using Microsoft.Office.Interop.Excel;

    namespace RowsColorChange

{
    class Program
    {
        static void Main(string[] args)
        {
          
        Excel.Application xlApp;
        Excel.Workbook xlWorkBook;
        Excel.Worksheet xlWorkSheet;
        object misValue = System.Reflection.Missing.Value;

        xlApp = new Excel.Application();
        xlWorkBook = xlApp.Workbooks.Open("C:\\Users\\Something\\Ge\\Output\\2020-06 1 - MR - Pronto - Copia.xlsm", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
        FormatCondition format = (FormatCondition)(xlWorkSheet.get_Range("B3:B2000",
            Type.Missing).FormatConditions.Add(XlFormatConditionType.xlExpression, XlFormatConditionOperator.xlEqual,
            "=$B3<>null","=$B3<>"+"new", Type.Missing, Type.Missing, Type.Missing, Type.Missing));
        format.Font.Bold = true;
        format.Font.Color = 0x000000FF;
        xlWorkBook.Close(true, misValue, misValue);
        xlApp.Quit();

        System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkSheet);
        System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook);
        System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);

使用条件格式

选择B到O列标题以选择所有列,或者如果愿意,选择B1:O3000 PS执行此操作的快速方法是单击公式栏左侧的名称框并键入B1:O3000

选择条件格式|新规则。。。来自主功能区上的“样式”组

从“选择规则类型”框中选择“使用公式”以确定要格式化的单元格

在该公式为真的格式值:输入字段中,键入 =和$B1新的$B1

选择格式。。。在“填充”选项卡上,设置要使用的填充,然后选择“确定”

再次选择“确定”关闭“新建格式规则”对话框


有什么原因需要在B-O列上使用C而不是条件公式吗?没有,我认为只有使用代码才有可能。我怎么办?我试过了它告诉我:这个公式有一个问题,用AND代替its;,解决了;。感谢you@StefanoBrunato有意思,你在美国以外吗?可能是,;因为你用的是,为基数点?