...............
The code above works by setting foreground to all negative numbers to red.
But the Style.Number = 2 is not working (I want all numbers to also have the decimal format (e.g. 3.00)
Well, by default, FormatCondition would work for those values only which satisfy the condition, this is same as per MS Excel. To apply style to all the numbers with your desired formatting (irrespective of those values which are automatically formatted due to conditional formatting applied/ set), you have to apply the formatting by yourself in your code, see the sample code below for your complete reference (see the lines in bold specially). I have also attached the output file for your reference.
e.g
Sample code:
Workbook workbook = new Workbook();
Worksheet sheet = workbook.Worksheets[0];
int column = 1;
int index = sheet.ConditionalFormattings.Add();
var fcs = sheet.ConditionalFormattings[index];
var ca = new CellArea();
ca.StartRow = 1;
ca.EndRow = 24;
ca.StartColumn = 1;
ca.EndColumn = column + 1;
fcs.AddArea(ca);
//Create and apply the style to the range of cells.
Style style = workbook.CreateStyle();
style.Number = 2;
//Create the range based on the cell area
Range range = sheet.Cells.CreateRange(ca.StartRow, ca.StartColumn, ca.EndRow - ca.StartRow + 1, ca.EndColumn - ca.StartColumn + 1);
range.ApplyStyle(style, new StyleFlag() { NumberFormat = true });
var negativeIndex = fcs.AddCondition(FormatConditionType.CellValue);
var negativeCondition = fcs[negativeIndex];
negativeCondition.Operator = OperatorType.LessThan;
negativeCondition.Formula1 = "0.0";
FormatCondition fc = fcs[negativeIndex];
fc.Style.Font.Color = Color.Red;
fc.Style.Number = 2;
workbook.Save("e:\\test2\\outcondtionalformattingstyle1.xlsx");
Let us know if you still have any issue.
Thank you.