C#Excel如何更改特定行的颜色 [英] c# excel how to change a color of a particular row

查看:843
本文介绍了C#Excel如何更改特定行的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要问你们,如何更改行以红色在Excel表,如果小区1不为null。

I want to ask you guys, how to change color of a row to red in Excel table if the cell 1 isn't null.

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







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



我非常心存感激

I'm very thankfull

推荐答案

给这个一杆,我已经测试它和它的作品:

Give this a shot, I have tested it and it works:

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);

这篇关于C#Excel如何更改特定行的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆