I use the Style.Number property to set the format but how do i change the value of the cell to match the format?
example:
value may be 145.256000022235
format to 145.26
what i really want is the value to be 145.26
There is not an easy way to get the value according to the format. For this specific case, you can try:
Workbook wb = new Workbook();
Cells cells = wb.Worksheets[0].Cells;
cells["A1"].PutValue(145.256000022235);
cells["B1"].Formula = "=ROUND(A1,2)";
wb.CalculateFormula();
Console.WriteLine(cells["B1"].Value);
laurence,
thanks for your quick reply. unfortunately, the cells get populated using a datareader or a datatable and also, i can make no changes to the workbook besides the inserting of data.
any other suggestions?
You can round the number before inserting data into workbook.
For example:
cell.PutValue(Math.Round(123.45600011, 2);
Or you can change the value in this way:
for(int i = 0; i < cells.Count; i ++)
{
Cell cell = cells;
if(cell.Type == CellValueType.IsNumeric)
cell.PutValue(Math.Round(cell.DoubleValue), 2);
}
thanks laurence, this works well.
question: what’s the performance cost of calling the clearcontents method several times throughout the application?
There is some overhead when calling ClearContents method multiple times. I optimized this method in this attached version. Please try it.