Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read single Excel cell value

Tags:

c#

excel

I have excel file with sheet1 that has a value I need to read on row 2 and column 10. Here is my code.

Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(workbookPath, 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false); Excel.Sheets excelSheets = excelWorkbook.Worksheets; string currentSheet = "Sheet1"; Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(currentSheet); var cell = (Excel.Range)excelWorksheet.Cells[10, 2]; 

After getting cell object which is Excel.Range, I do not know how to read the content of that cell. I tried converting it into array and looping over it and I tried converting to string array etc. I am sure it is very simple. Is there a direct way to get just one cell value that is string?

like image 932
DoodleKana Avatar asked Sep 24 '13 23:09

DoodleKana


People also ask

How do you read a Excel cell in Matlab?

num = xlsread( filename , sheet ) reads the specified worksheet. num = xlsread( filename , xlRange ) reads from the specified range of the first worksheet in the workbook. Use Excel range syntax, such as 'A1:C3' . num = xlsread( filename , sheet , xlRange ) reads from the specified worksheet and range.


1 Answers

You need to cast it to a string (not an array of string) since it's a single value.

var cellValue = (string)(excelWorksheet.Cells[10, 2] as Excel.Range).Value; 
like image 76
System Down Avatar answered Sep 22 '22 02:09

System Down