Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
115 views
in Technique[技术] by (71.8m points)

c# excel how to change a color of a particular row

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

Excel.Application application = new Excel.Application();
Excel.Workbook workbook = application.Workbooks.Open(@"C:TestWhatever.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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...