I'm using Office Interop via C# 4.0 to write an Excel file. I would like to shade alternating rows. I know how to do this with the GUI in Excel. I could also loop through each row to provide the shading, but this option is unacceptably slow with large data sets. Is there a way, using Office.Interop.Excel to set the conditional formatting options found in the Excel GUI?
EDIT: Added more information
Using C# with .NET 4.0 and Office Interop 2007
After some research and playing around I believe I have found the best answer. The following code will alternate row colors for the worksheet.
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Interop.Excel.Application();
Workbook workbook = excel.Workbook.Add();
Worksheet worksheet = workbook.Worksheets.Item[1];
FormatCondition format = worksheet.Rows.FormatConditions.Add(XlFormatConditionType.xlExpression, XLFormatConditionOperator.xlEqual, "=MOD(ROW(),2) = 0");
format.Interior.Color = XlRgbColor.rgbBlue;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With