aspose 单元格时间格式

aspose cell time format

我尝试使用 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);

此单元格显示正确,但格式为:"all formats"(而非 "Time")。也就是说,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);

如果不起作用,请改用 style.Custom

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

通过 Aspose.Cells API.

输入单元格时,需要将 TimeSpan 字符串转换为正确的数据类型
cell.PutValue(time.ToString(), true); //true specifies that the data will be converted to proper data type.

现在可以了。可以将这些单元格用作时间单元格(例如,计算总和或平均值)