Home > DeveloperSection > Forums > How to select record from excel spreadsheet in C#?
Pravesh Singh

Total Post:411

Posted on    August-17-2012 2:34 AM

 C# C# 

 3 Reply(s)
 4122  View(s)
Rate this:
Hello Everyone,
Can anyone tell me how to select record from excel spreadsheet using c# code?


Total Post:604

Posted on    August-18-2012 1:57 AM

Hello Pravesh
You can use the following code to selecting record from excel spreadsheet in c#

// Create Data Table for MS-Office 2007 or 2003

            System.Data.DataTable dtExcel = new System.Data.DataTable();

            //DataTable Name

            dtExcel.TableName = "MyExcelData";

            //Path of excel file

            string SourceConstr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='F:\avi\information.xlsx';Extended Properties= 'Excel 8.0;HDR=Yes;IMEX=1'";


            OleDbConnection con = new OleDbConnection(SourceConstr);

            //Query string

            string query = "Select * from [Sheet1$]";

            //DataAdapter object

            OleDbDataAdapter data = new OleDbDataAdapter(query, con);

            //fill record into DataTable


Now you can bind DataTable with control for displaying record. You may check following link too.


I hope it might be resolve your problem.

Sohail Shaikh
Sohail Shaikh

Total Post:7

Posted on    November-29-2012 4:08 AM


Nibaal Bazzi
Nibaal Bazzi

Total Post:2

Posted on    December-03-2013 12:20 AM

Hi Pravesh,
you can also try this C# Excel library, it provides a neat and an efficient API's for various spreadsheet files manipulations.
Here is a small sample how you can read an Excel file in C# and retrieve the values from the selected cell.

public object GetSelectedRecord(string fileName, string cellName)
    var file = ExcelFile.Load(fileName);
    var sheet = file.Worksheets.ActiveWorksheet;
    var cell = sheet.Cells[cellName];

    return cell.Value;

I hope this helps.

Don't want to miss updates? Please click the below button!

Follow MindStick