Convert milliseconds to individual format in C# NPOI - c#

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.

Related

Avoid Date Numbering format in excel

I have been writing an excel using C# and found this issue while i pick a HTML field and write the contents into excel.
Actual Content : 5 8 3
Excel writes : 05/08/2018
Code:
worksheet.Cells[iRow, 5].SetValue(string.Format("{0}", columnValue));
Is there any format that i have to set something like this? I use the below code for DateFormat but for a different Cell.
CellSelection cellSelection = workbook.ActiveWorksheet.Cells[iRow, 1];
cellSelection.SetFormat(new CellValueFormat("yyyy-MM-dd HH:mm:ss"));
How can i make the excel write the same value ('5 8 3') ?
Note:
Also tried removing the above DateFormat code and the issue remains the same.
To format a cell to "Text" you can use the Format Provider "#".
In this case the value can be retained by,
worksheet.Cells[iRow, 5].SetFormat(new CellValueFormat("#"));
Try adding the code before you write into the cell and this should work.

how to get correct format from xlsx - to interpret floating point number formated as text

i am aware of the floating point inaccurrancy, this is just how to get the correct text FORMAT!! by openxml from the xlsx and show the value like excel does. I am not able to edit the excel file and change the format or something like that, because of some reasons.
while working with numbers formated as numbers everything runs fine
formatted as text or general, the number 0.813 is saved as 0.812999.., excel shows it correct!, but via openxml i can't get the used format (0.000), anyone an idea to get the format?
maybe this is a standard format, not saved and choosen by other values?
finally I am using the ugly solution to get rid of this ..
if the cellFormat NumberFormatId is 2 I try to parse the value to double and then to string with two decimals
double.Parse(displayedValue).ToString("0.00");
not my desired solution, but for the moment this works .. of course also with all other unit tests

Reading double's from Excel "the Excel way"

Background
I am trying to read a 22 x 22 matrix from a Excel Worksheet. The matrix holds percent values and the values of each row must have a sum of 100% (or 1 when dealing with the numbers behind the percent value). When I open such a Excel worksheet and build the sum on each row, it is always 100% (1). Perfect.
But when I read the worksheet and sum up the (double) values read from the sheet I get a significant distance to 1 on most of the rows (significant means more than 0.00000000001 absolute distance to 1).
Investigation
I modified the matrix in excel to display me the numbers behind the percent values and the compared it to what I've read using EPPlus. For example I had
99.86% (Excel with percent)
0.998610811163197 (Excel as number)
0.9986108111631975 (read with EPPlus)
I renamed my Excel document to a ZIP archived, unpacked it and opened the according sheet in Visual Studio. The value stored was exact the value I got with EPPlus - which wasn't really surprising.
Solution?
I decided to operate as excel does, at least I thought excel does it so. I tried to round the values after 15 digits. But funny enough, the result wasn't the same as in excel, even worse, after looking at some other values I had:
0.00 % (Excel with percent)
0.00000330942432252678 (Excel as number)
3.3094243225267778E-6 (stored in the XML, read via EPPlus)
So, the question is: is there a way to round or read the values from Excel as Excel displays them?
Here is my code for reading the excel:
using (ExcelPackage excel = new ExcelPackage())
{
excel.Load(File.OpenRead("data.xlsx"));
var a1 = excel.Workbook.Worksheets.First().Cells["A1"].Value;
var a2 = excel.Workbook.Worksheets.First().Cells["A2"].Value;
}
Apologies, I am not able to upload the excel file at the moment from my workplace to dropbox or something else, I'll attach it later.
Edit: here is the excel document.
If i understand your question, you have problem with display double value, right?
You can use correct format for displaying double values. For example:
double val = 99.8610811163198;
Console.WriteLine(val.ToString("P", CultureInfo.InvariantCulture));
About this read MS article: https://msdn.microsoft.com/en-us/library/kfsatb94(v=vs.110).aspx

Bytescout.Spreadsheet Number Formatting Multiplying By 100

I'm currently using the bytescout library to generate a PDF document. Here is some pseudo code of me setting the cell in question:
cell.Value = 20;
cell.NumberFormatString = "0.00%";
When the Excel document is generated the Cell displays as 2000.00%
Does anyone have experience with this library?
Simple answer is to divide by 100. Seems Excel expects the % decimal value. The lightbulb eventually came on.

C# - Decimals not writing out to Excel

I have an ETL that's saving data to an Excel file. The issue is that the decimals are not being written out for integers. Example:
14.00
is being written out as
14
My code for writing out that line is
loWorksheet.Cells[liRowNum, 5] = lcAmount.ToString("0.00");
When I step through the code, it shows as 14.00, but on the Excel file it is not retaining the decimal places. Is this something that can be fixed in my code or is this an Excel issue? Any suggestions?
I'm quite sure you have to set format for your cells. I can't check right now, but it will be something like
xlYourRange.NumberFormat = "0.00";
You can check this question Set data type like number, text and date in excel column using Microsoft.Office.Interop.Excel in c#
If you really want the data to be displayed literally the way it is in the source file, you have to deal with trade-offs. The simplest way is to format the data as text. You can do this a cell at a time or for entire columns:
loWorksheet.Columns["A:E"].NumberFormat = "#";
The trade-off is it's just text at this point. You can't add, sum, average, whatever.
On the other hands, if your data looks like this:
4.0
4.00
4.000
You can't really keep it as numbers and expect to retain the original format without doing some funny business.
If it's consistently two decimal places, and you know it's going to be, then I agree with #RenatZamaletdinov's solution.
And you might want to consider other strings and what Excel might to do them
0000123 becomes 123
10/23 will probably render as a date, depending on your localization
12345678901234567890 will render as scientific notation probably
These are all avoided if you make the numeric format text (#), but again without knowing what you plan to do with the data, it's hard to say if this is the correct approach.
Wrap lcAmount.ToString("0.00");in a pair of quotes and put an equal sign in front of it. This will prevent excel from overriding the format.
loWorksheet.Cells[liRowNum, 5] = "=" + '"' lcAmount.ToString("0.00") + '"';

Categories

Resources