3

My requirement to change background color of cells in a row of a excel sheet.

ex : if A1 cell value is less than 100, i need to show it in Red Background.

I searched alot, i found some code to create stylesheets from this

http://blogs.msdn.com/b/chrisquon/archive/2009/11/30/stylizing-your-excel-worksheets-with-open-xml-2-0.aspx

that code is using StyleIndex property. And its very complex. Is there any other solution...???????

1
  • 1
    OpenXml should really be called ChaosXml. I don't think you are giong to find a simpler solution. Look here :/ Commented May 6, 2013 at 11:14

1 Answer 1

5

For manipulating spreadsheets in OpenXML format there are several wrappers around the raw SDK that make things much simpler, e.g.

Using ClosedXML you could use conditional formatting to achieve your desired result (see documentation):

using (var wb = new XLWorkbook())
{
    using (var ws = wb.AddWorksheet("Test"))
    {
        ws.Cell("A1").Value = 42;
        ws.Cell("A1").AddConditionalFormat().WhenLessThan(100)
            .Fill.SetBackgroundColor(XLColor.Red);
    }
    wb.SaveAs(@"C:\Dev\Test.xlsx");
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.