somethingstrang
somethingstrang

Reputation: 1223

C# EPPLUS can't get cell value

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

Upvotes: 10

Views: 25616

Answers (2)

somethingstrang
somethingstrang

Reputation: 1223

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

Upvotes: 4

Shilpa
Shilpa

Reputation: 189

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);

Upvotes: 8

Related Questions