Number format lost while appying ForegroundColor to a cell

Hi Team,

We are facing a issue with Aspose.cells. When we set the cell color to a cell, the number format is getting lost. Can you please suggest how we can resolve this. We are using the below code.

eg: suppose the cell had a value 123.000 before applying color, after the color is applied it will become 123 (decinmal part is getting lost)

Dim cellStyle As Aspose.Cells.Style
Dim cellStyleFlag As New Aspose.Cells.StyleFlag
cellStyle = cell.Worksheet.Workbook.Styles(cell.Worksheet.Workbook.Styles.Add())
cellStyle.ForegroundColor = System.Drawing.ColorTranslator.FromHtml(colorCode)
cellStyle.Pattern = BackgroundType.Solid
cellStyleFlag.CellShading = True
cell.SetStyle(cellStyle, cellStyleFlag)

Thanks

Anish

Hi,

Thanks for your posting and using Aspose.Cells for .NET.

You should use one of the following 2 overloads.

  • void Cell.SetStyle(Style style, bool explicitFlag)
  • void Cell.SetStyle(Style style, StyleFlag flag)

Using the above methods, you can preserve existing settings while applying new changes to Style objects.

Also,
if you are using named styles, you can update them also without
affecting their previous state. Please see the following article for
your reference.

Hi,


Well, it should not happen if you entered value as text. By the way, when you simply enter “123.000” the decimal part is eliminated by default, this is MS Excel’s behavior, you may confirm it in MS Excel manually. I think you may set custom format e.g (“0.000”) for your needs. e.g
you may add lines of code
cellStyle.Custom = "0.000"
cellStyleFlag.CellShading = True


Let us know if you still have any confusion.

Thank you.