In this tutorial we will look on how to read a cell value in a worksheet in excel (xls or the newer one xlsx is supported).You can use the C# methods provided in this tutorial for reading an excel file for your own C# projects. This will be a quick simple tutorial and you will learn very quick.
Things Needed:
- Microsoft Office 2007 or 2010
- Microsoft Visual Studio or Visual C#
- Add a reference for Microsoft.Office.Interop.Excel (right click references then Add Reference)
You need to use the reference in your code to access the methods.
using Microsoft.Office.Interop.Excel; //use the reference in your code
These are the methods I have created in order to read cell values in excel, you may use it for free.
//Add this codes in your progam code private static Microsoft.Office.Interop.Excel.ApplicationClass appExcel; private static Workbook newWorkbook = null; private static _Worksheet objsheet = null; //Method to initialize opening Excel static void excel_init(String path) { appExcel = new Microsoft.Office.Interop.Excel.ApplicationClass(); if (System.IO.File.Exists(path)) { // then go and load this into excel newWorkbook = appExcel.Workbooks.Open(path, true, true); objsheet = (_Worksheet)appExcel.ActiveWorkbook.ActiveSheet; } else { MessageBox.Show("Unable to open file!"); System.Runtime.InteropServices.Marshal.ReleaseComObject(appExcel); appExcel = null; System.Windows.Forms.Application.Exit(); } } //Method to get value; cellname is A1,A2, or B1,B2 etc...in excel. static string excel_getValue(string cellname) { string value = string.Empty; try { value = objsheet.get_Range(cellname).get_Value().ToString(); } catch { value = ""; } return value; } //Method to close excel connection static void excel_close() { if (appExcel != null) { try { newWorkbook.Close(); System.Runtime.InteropServices.Marshal.ReleaseComObject(appExcel); appExcel = null; objsheet = null; } catch (Exception ex) { appExcel = null; MessageBox.Show("Unable to release the Object " + ex.ToString()); } finally { GC.Collect(); } } }
Use it in your program:
To use it in your program, you need to initialize opening excel first by using this method:
excel_init("C:\\excel_tutorial.xlsx");The format is excel_init(Path_of_your_excel_file); if you use slash, make sure to make it a double slash \\.
Reading a cell value method:
excel_getValue("B10");The format is excel_getValue(Cellname); where cellname is like A1,C4,H10,M21, etc...
This method will return the value of the cell in String data type.
Safely Close the excel connection:
excel_close();
And where done!
Attached is a sample GUI program for reading a cell in excel.

If you have questions/problems/suggestions please comment on this article.
Thank you very much,
Nexus Nemesis @ codecall