Excel Interlop Value vs Text vs Value2 - c#

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

Related

How can I force Excel to display scientific notation via EPPlus?

Is there any way to control whether a number displays in scientific notation using the EPPlus library to generate an Excel file in C#?
worksheet.Cells[row, col].Value = someValue; //example value: 0.0000152691
worksheet.Cells[row, col].StyleName = RegularCenterAlign;
worksheet.Cells[row, col].Style.Numberformat.Format = "WHAT DO I PUT HERE???";
I tried "0.00e0" for the format and that causes excel to throw an error:
I didn't see it in the documentation but if you look in Excel at the formatting options, scientific notation is available, so you'd think EPPlus has a string code we could use for this.
What seems to be happening now is Excel is making its own determination about whether to format a given number in scientific notation. The same code inserted all of the numbers in the following screenshot. No format was specified but you can see that last one did not use scientific notation.
Use:
ws.Cells["A1"].Style.Numberformat.Format = "0E+00";
You can add the number of decimals with something like:
ws.Cells["A1"].Style.Numberformat.Format = "0.0000E+00";
Don't forget that your value should be numeric. This value will be formatted:
ws.Cells["A1"].Value = 0.0000152691;
this one won't:
ws.Cells["A1"].Value = "0.0000152691";

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

Excel Library possible integer overflow

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.

Excel Interop currency format

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?

Categories

Resources