Link to home
Start Free TrialLog in
Avatar of Jim Schwetz
Jim Schwetz

asked on

Get Values of cells in a column with formula using C# to paste to another column. (The formula is to remove the apostrophe in front of the number)

Issue -
I am using OfficeOpenXML package in C# to process excel files.

I am not able to read values from the cells with formula, it returns empty string.

I am setting up the formula in the field-
worksheet.Cells["I2"].Formula = "=RIGHT(H2, 9)";

but when I am trying to get the value of cell with formula, I am getting blank value, but the excel file shows value -
string cellValue = worksheet.Cells["I2"].Text

User generated imageexcelHelp.xlsx
Avatar of Subodh Tiwari (Neeraj)
Subodh Tiwari (Neeraj)
Flag of India image

I have no experience with C# or OfficeOpenXML but in Excel Cells object requires two arguments, a RowIndex and a ColumnIndex whereas Range object requires the cell address so I guess you should try one of these lines and see if that works for you...

worksheet.Cells(2,9).Text
Where 2 is the row index and 9 is column index for column I.
OR
worksheet.Range("I2").Text

Also, I assume that worksheet variable is properly defined.
Avatar of Bill Prew
Bill Prew

I haven't worked with OfficeOpenXML, but you might try this.  In a quick look online it looks like you can address the cell by it's row column text address as you are doing so that should be okay.

string cellValue = worksheet.Cells["I2"].Value.ToString()

Are you sure worksheet  is referencing the sheet you think it is?


»bp
ASKER CERTIFIED SOLUTION
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jim Schwetz

ASKER

Thanks all,  I will try these tomorrow,  and thanks gr8gonzo for the trimStart with C#,  That sounds like a smarter way of doing it.