.netexcelaspose-cells

show the excel value as Red if the cellvalue is negative with comma for Polish and Czech Republic culture


I am working on excel report generation using Aspose, my users are across various countries and they should see the report in their culture. I am using aspose.net to generate the excel report, for Polish and Czech Republic culture they use ',' instead if '.' to represent decimal values. I need to highlight the negative values by showing background as grey and font color in red. Only for these culture it doesn't apply red font.

          dataCell[exportUtil.constants.cell.numberFormat] = "#,##0,00;[Red]#,##0,00";
          dataCell[exportUtil.constants.cell.cellType] = "number"; 
          dataCell[exportUtil.constants.cell.colorBackground] = 14474460;
          dataCell[exportUtil.constants.cell.fontColor] = 255;

Below are the default custom excel format.

enter image description here

Any help on this will be highly appreciated.


Solution

  • See the following sample code for your reference. You may refer to the code segment and then write/update your own code accordingly for your needs.

    e.g.,

    Sample code:

    //Instantiating a Workbook object
    Workbook workbook = new Workbook();
    Worksheet sheet = workbook.Worksheets[0];
    int index = sheet.ConditionalFormattings.Add();
    FormatConditionCollection fcs = sheet.ConditionalFormattings[index];
    //Sets the conditional format range.
    CellArea ca = new CellArea();//A1:A11
    ca.StartRow = 0;
    ca.EndRow = 10;
    ca.StartColumn = 0;
    ca.EndColumn = 0;
    fcs.AddArea(ca);
    //Adds condition.
    int conditionIndex = fcs.AddCondition(FormatConditionType.CellValue, OperatorType.LessThan, "0", null);
               
    //Sets the formatting based on condition.
    FormatCondition fc1 = fcs[conditionIndex];
    fc1.Style.Custom = "#,##0,00;[Red]#,##0,00";
    fc1.Style.BackgroundColor = System.Drawing.Color.Gray;
    fc1.Style.Font.Color = System.Drawing.Color.Red;
    //Saving the Excel file
    workbook.Save("g:\\test2\\out1.xlsx");
    

    You may also post your queries or discuss further in the dedicated forum.

    PS. I am working as Support developer/ Evangelist at Aspose.