c# change cell color from value (xlsx) RRS feed

  • Question

  • I have a file xlsx which contains data:
    ID1204  0
    ID5611  0
    ID6711  1
    ID4572  2

    I want to color the cells in the ID column if VALUE is greater than 1.
    I need to set background color in red the cell number ID4572.
    The number of rows in the file can be more than 1000.
    How to do it?
    I use ClosedXML.Excel
    Monday, October 29, 2018 2:16 PM


  • Hi Aleks,

    Please review following code:

    Range UsedRange = worksheet.UsedRange;
    int lastUsedRow = UsedRange.Row + UsedRange.Rows.Count - 1;
    int row;
    for(row = 2; i <= lastUsedRow; i++)
        if(((Microsoft.Office.Interop.Excel.Range)worksheet.Cells[row, 2).Text.ToString() > 1)
            Microsoft.Office.Interop.Excel.Range range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[row, 1);
            range.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);

    Please refer to the following links:

    Cell color changing In Excel using C#

    C# - How do I iterate all the rows in Excel._Worksheet?

    Best Regards,


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.

    Tuesday, October 30, 2018 7:01 AM