Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# EPPLUS can't get cell value

Tags:

c#

.net

epplus

I have Cell "A1" with the value of 1.00, set by a formula

I want to save this value to a variable.

I tried:

ws.Cells["A1"].Value
ws.Cells["A1"].Text
ws.Cells["A1"].GetValue<double>
ws.Cells["A1"].Value.ToString()

None of these work as I either get an error or I don't get my number at all (console.writeline outputs a blank).

I tried searching online and I get what I tried above. I know I'm referencing the cell correctly because I can actually set the value just fine.

So how do I actually get my value of 1.00 and save it in a double variable?

EDIT: my code, where the worksheet in the filePath has "A1" value of 1.00

using (var pck = new ExcelPackage(filePath))
{
   var ws = pck.Workbook.Worksheets[1];

   var test1 = ws.Cells["A1"].Value;
   var test2 = ws.Cells["A1"].Text;
   var test3 = ws.Cells["A1"].GetValue<double>();

   Console.WriteLine(test1);
   Console.WriteLine(test2);
   Console.WriteLine(test3);
 }

output is:

[blank]
[blank]
0

EDIT2: The value of 1.00 is from a formula

like image 309
somethingstrang Avatar asked Nov 16 '15 01:11

somethingstrang


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C full form?

Originally Answered: What is the full form of C ? C - Compiler . C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC PDP-11 computer in 1972.

How old is the letter C?

The letter c was applied by French orthographists in the 12th century to represent the sound ts in English, and this sound developed into the simpler sibilant s.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


2 Answers

Get the cell value using the row and column indices. This value is in string. Convert the string value to double.

Try this:

var ws = pck.Workbook.Worksheets[1];

//var test1 = ws.Cells[rowIndex, columnIndex].Value;
//For cell A1 - rowIndex is 1, columnIndex is 1
var test1 = ws.Cells[1, 1].Value;
double dValue = Convert.ToDouble(test1);
like image 110
Shilpa Avatar answered Oct 16 '22 20:10

Shilpa


Sorry for the hassle. By doing ws.Cell["A1"].Calculate() I can get the value. Stupid me for omitting that fact

like image 43
somethingstrang Avatar answered Oct 16 '22 18:10

somethingstrang