简体   繁体   中英

Convert milliseconds to individual format in C# NPOI

I use NPOI .NET third party library to export datas to *.xlsx file. I have got a time value which represented in milliseconds. For example, 2 minutes 12 seconds 3 milliseconds represented as 132003 milliseconds. I would like to display "132003" as "02m 12.003s" in Excel. So if i click a cell in excel which contains this value, i would like to see "02m 12.003s" in the cell, and "132003" in the formula editor.

How can I solve it?

Thanks in advance.

在此处输入图片说明

Consider the code below:

var t = TimeSpan.FromMilliseconds(521516);
var formatted = String.Format("{0:D2}m:{1:D2}s:{2:D3}ms", t.Minutes, t.Seconds, t.Milliseconds);
System.Diagnostic.Debug.WriteLine(formatted);

This will output:

08m:41s:516ms

You can use that code to retrive the formatted text and then add it to excel .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM