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
Related
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
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.
Having some problems parsing numbers out of the following excel spread sheet.
The code:
var curQOH = toolkit.ExcelWorksheet.Cells[i, 28] as Range;
var curQAV = toolkit.ExcelWorksheet.Cells[i, 29] as Range;
if (!curQOH.Text.Contains("("))
Int32.TryParse(curQOH.Text, out lastQOH);
else
Int32.TryParse(curQOH.Value as string, out lastQOH);
if (!curQAV.Text.Contains("("))
Int32.TryParse(curQAV.Text, out lastQAV);
else
Int32.TryParse(curQAV.Value as string, out lastQAV);
The code above parses the positive numbers just fine. No issues. But it seems like it cannot parse negative number.
To my knowledge, Text is suppose to give me what the viewer sees so I would get (10) as an output. Value does give the right number but I cannot seem to parse that after casting to string. (this issue why I cant store the value as string or cast it to int, Excel cell value as string won't store as string)
Stoped using Excel Interlop and started using OpenXML Excel library
I am using Excel Library - http://code.google.com/p/excellibrary/ - To generate an excel 2003 spreadsheet. Everything works fine except when some big values are used.
These are some reference numbers that are used by a client and I simply need to present them as integer values in the spreadsheet.
int val = 1420007117;
worksheet.Celss[row, col] = new Cell(val); // Displays - 352108063
This results in the value 352108063 being displayed in the spreadsheet. If the value is lower, then it displays fine.
Anyone know what the issue might be, or how to work around this problem. Outputting the value as string is not possible as it leaves a green Number stored as Text error.
I would say that excel doesn't support 64-bit integers and excellibrary doesn't care about it.
For such big numbers you better use floating point. This is how Excel handles big numbers.
I want to save a worksheet as CSV file using Excel interop. Doing so by using:
sheet.SaveAs(tempfile, Microsoft.Office.Interop.Excel.XlFileFormat.xlCSVWindows);
causes losing of precision. Iterating over each cell and constructing CSV is not an option as its very slow. Getting an array of values from Range is not good as well, as I cannot obtain NumberFormat of cells this way.
I noticed that precision is lost because during the export to CSV saved value is the one displayed, not the one actually being in the cell.
Any advice?