c#excel如何更改特定行的颜色

时间:2012-10-04 11:06:17

标签: c# excel colors row cell

我想问你们,如果单元格1不为空,如何在Excel表格中将行的颜色更改为红色。

XX     YY     ZZ
-----------------
aa     bb     cc
aa1    bb1    cc1
aa2           cc2
aa3    bb3    cc3
aa4          

Excel.Application xlApp;
Excel. Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;

我非常感谢

2 个答案:

答案 0 :(得分:15)

给它一个镜头,我测试了它并且它有效:

Excel.Application application = new Excel.Application();
Excel.Workbook workbook = application.Workbooks.Open(@"C:\Test\Whatever.xlsx");
Excel.Worksheet worksheet = workbook.ActiveSheet;

Excel.Range usedRange = worksheet.UsedRange;

Excel.Range rows = usedRange.Rows;

int count = 0;

foreach (Excel.Range row in rows)
{
    if (count > 0)
    {
        Excel.Range firstCell = row.Cells[1];

        string firstCellValue = firstCell.Value as String;

        if (!string.IsNullOrEmpty(firstCellValue))
        {
            row.Interior.Color = System.Drawing.Color.Red;
        }
    }

    count++;
}

workbook.Save();
workbook.Close();

application.Quit();

Marshal.ReleaseComObject(application);

答案 1 :(得分:0)

bringToFront()