aspose cell time format

1k Views Asked by At

I try to set cell format using AsposeCell API (C#):

var cell = worksheet.Cells[i, j];
Style style = cell.GetStyle();
style.Number = 21;
cell.SetStyle(style);

var time = new TimeSpan(1, 2, 3);
cell.PutValue(time);

This cell is displayed correctly, but has format: "all formats" (not "Time"). That is to say, style property did not work. What's wrong?

Thank you!

2

There are 2 best solutions below

1
On BEST ANSWER

Need to kindly convert TimeSpan string to proper data type when inputting into the cell via Aspose.Cells API.

cell.PutValue(time.ToString(), true); //true specifies that the data will be converted to proper data type.

It works now. May to use these cells as time cells (for example, to count sum or average)

1
On

Try putting value first, then setting style.

var cell = worksheet.Cells[i, j];

var time = new TimeSpan(1, 2, 3);
cell.PutValue(time);

Style style = cell.GetStyle();
style.Number = 21;
cell.SetStyle(style);

If not working, use style.Custom instead.

style.Custom = "h:mm:ss";