Dhrup
Dhrup

Reputation: 81

c# excel cell format to hh:mm:ss

I am having a problem while converting text into the format [hh:mm:ss].

When I double click, formatting is fixed and cell value is automatically aligned to the right.

I am using [SpreadsheetStreams.net] NuGet package to create an excel file.

Here what I have already tried:

    1.
writer.AddCell(TimeSpan.Parse(columnNames[i].ToString()))
    2.
Style test = new Style();

NumberFormat format = new NumberFormat(NumberFormatType.Custom);

format.Custom = "hh:mm";

test.NumberFormat = format;

writer.AddCell(TimeSpan.Parse(columnNames[i].ToString()), test) OR

writer.AddCell(columnNames[i].ToString(), test)

Please refer to this image:

enter image description here

How can I format the cell to [hh:mm:ss]?

Upvotes: 0

Views: 580

Answers (1)

Dhrup
Dhrup

Reputation: 81

After trying many solutions I found a way : Create a Style object having a custom type like below :

Style timeStyle = new Style();
NumberFormat format = new NumberFormat(NumberFormatType.Custom);
format.Custom = "hh:mm";
timeStyle.NumberFormat = format;

Then pass this as the second parameter and first parameter as a day (in double)

writer.AddCell(TimeSpan.Parse(columnNames[i]).TotalSeconds / 86400, timeStyle );

Upvotes: 1

Related Questions