Correct TimeSpan import

Hello!
I want to write a TimeSpan value - 25 hours - to cell as Time format.

cell.PutValue(((TimeSpan)value).ToString(“g”), true);

If value is less than 24 h than it’s right.
But if value is greater than or equal 24 h than would be 2 problems:

  1. target cell has string format. for example, 25 h was written as 01:00:00 but as string. (operation SUM or AVERAGE aren’t valid)
  2. is it possible to display 25 h as 25:00:00? Excel has such format. How to write value correct to get such format?

Thank you!

@SeminDm,

Could you try to format the cell after inputting Time value into it. See the following sample code if it suits your needs:
e.g
Sample code:

   Workbook workbook = new Workbook();
    Worksheet worksheet = workbook.Worksheets[0];
    var cell = worksheet.Cells[0, 0];
    cell.PutValue("25:00:00", true);
    Style style = cell.GetStyle();
    style.Custom = "[h]:mm:ss";
    cell.SetStyle(style);
    workbook.Save("e:\\test2\\out1.xlsx");

If it still does not suit your requirements, kindly provide a sample Excel file containing your desired DateTime value applied (you may create the Excel file in MS Excel manually), we will check on how to do it via Aspose.Cells APIs.

@Amjad_Sahi

Your advice is working, but I have string as “1.1:00:00” (forget to mention it)
I resolve problem:
cell.PutValue(new DateTime(1900, 1, 1, 0, 0, 0, 0, DateTimeKind.Local).Add(time));

Thank you for help!

@SeminDm,

It looks like your issue is sorted out now. Let us know with details, sample code and template file if your issue is not resolved or you have some other query or issue, we will be happy to assist you soon.