I am attempting to read timestamps out of an excel sheet, which go down to a millisecond level.
e.g. 00:00:15.480, 00:00:24.640
However when i read the text value of the cell, it is rounding to the nearest second.
var ts = sheet.Cells[rowIndex, 2].Text; /* would produce 00:00:15.000 and 00:00:25.000 in above examples */
I have tried setting the number format on the cells to the be same custom format referenced in the sheet itself, but this has no effect:
sheet.Cells[rowIndex, 2].Style.Numberformat.Format = "hh:mm:ss.000";
If I try to access the value directly with
var ts = sheet.Cells[rowIndex, 2].Value;
it returns a double, e.g. 2.9166666666666666E-05
How can I get the timestamps with their millisecond values? Does EPPlus simply not allow this level of granularity?
As per Walter Vehoeven's comment, my issue was the format should have been "hh:mm:ss.fff"
Related
I am trying to format a specific XLS cell to the "Percentage" format using NPOI.
I am able to convert it to text using
(dataRow.GetCell(17) ?? dataRow.CreateCell(17)).CellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("Text");
But if I try do it with percentage it doesnt work. This is the code I am trying
(dataRow.GetCell(17) ?? dataRow.CreateCell(17)).CellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("Percentage");
It will just set that cell as Generic. How would I go about setting it as percentage?
Please keep in mind I want the value e.g 0.05 to be in the cell (not 5%), but with the format as Percentage.
I found the answer. I had to use:
(dataRow.GetCell(17) ?? dataRow.CreateCell(17)).CellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00%");
It make it 2dp percentage without the symbol
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
I have an excel function that get's data from the internet. The problem is that the function takes a long time to execute and it slows down everything.
It will be amazing if I can change the value of the cell without changing its formula! So if I call the function =GetNumOfEmployee() that returns 0 imediately and then from my addin when I get the result I can replace the value of that cell with 100 for example. If I do that the fomula gets lost and I do not want that to happen.
What I did in order to preserve the formula was to change the formatting of the cell by doing:
this.Application.ActiveCell.NumberFormat = 5;
if the active cell had a 0, then that line of code will replace the value to a 5 which is very cool. I get to preserve the formula and I have a new value.
The problem with that approach is that I get a lot of calculations and every time I write the line:
this.Application.ActiveCell.NumberFormat = 1234;
excel saves that formating at:
and very soon I reach the maximum number of formatings that excel enables. In 1 minute that list has about 500 formats and soon I am not able to delete them. If I programatically delete them excel acts kind of slow and I get the windows spining mouse icon every time I delete a format.
So in short I will like to replace the value of a cell without changing its' formula from my addin once I have the result that I need back. Should I place the value to the right of the cell?
I am trying to use Microsoft.Office.Interop.Excel to read excel file in c#.
I want to read range of cells as it is way faster than reading cell one by one:
Range rbeg = (Range)sheet.Cells[1, i + 1];
Range rend = (Range)sheet.Cells[totalRowCount, i + 1];
Range range = sheet.get_Range(rbeg, rend);
column = (object[,])range.Value2;
The problem is when I want to get number format of cells by calling:
range.NumberFormat
I get System.DBNull. It works when I call it for single cell.
I want to distinguish between cells with numerical values and "%" values.
Any ideas?
See the documentation:
[NumberFormat] returns Null if all cells in the specified range
don't have the same number format.
What were you expecting it to do in this case?
I'm attempting to format a cell in excel to the currency format. So I go into excel, record a macro of me converting an ordinary cell to a currency format, take a look at the vb script and see that it outputs the following:
NumberFormat = "$ #,##0.00"
So i take that format and paste it into my code, it works to the extent that im getting the currency character before the values in my excel sheet. However, the format of the cell is still a number and excel places a little green triangle at the bottom left of the cell informing me that the format is incorrect (which it is, cos its supposed to be currency, but its set to number) is there any way in c# to actually set the cell to a "Currency" format?
thanks
The range object has a "Style" property... the intelisense metadata says that it is "Returns an object you can use" only but you can also just set the Style with that property. To get the built-in "Currency" style, use the "Styles" property of (for instance) a Workbook object.
Example:
using Excel = Microsoft.Office.Interop.Excel;
...
var excel = new Excel.Application();
var wb = excel.Workbooks.Add();
var sheet = (Excel.Worksheet)wb.Sheets[1];
((Excel.Range)sheet.Cells[3, 4]).Style = wb.Styles["Currency"];
In fact you can just set it to the string "Currency" which may be what Charles was suggesting.
((Excel.Range)sheet.Cells[3, 4]).Style = "Currency";
Ok i tried many that codes and i found this works better than other on any language/regional settings.
numberFormat = #"_-[$$-409]* #,##0.00_ ;_-[$$-409]* -#,##0.00 ;_-[$$-409]* ""-""??_ ;_-#_ ";
Not sure I understand your question:In Excel all numeric values including currency, integers and date/time are held as floating-point doubles. The Format of a value only controls how it is presented in the rendering (visible) layer. The green triangle tests do not include tests for formatting. Is the problem that you are creating a text value rather than a numeric value?