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

Total Post:411

Points:2881
Posted on    August-17-2012 2:34 AM

 C# C# 
Ratings:


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


AVADHESH PATEL

Total Post:604

Points:4228
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'";

            //connectin

            OleDbConnection con = new OleDbConnection(SourceConstr);

            //Query string

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

            //DataAdapter object

            OleDbDataAdapter data = new OleDbDataAdapter(query, con);

            //fill record into DataTable

            data.Fill(dtExcel);


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

http://www.mindstick.com/Blog/294/How%20to%20import%20or%20export%20SQL%20Server%20table%20data%20in%20MS%20Excel%20sheet%20using%20C%2023%20Code


I hope it might be resolve your problem.


Sohail Shaikh
Sohail Shaikh

Total Post:7

Points:49
Posted on    November-29-2012 4:08 AM

http://blueraiden.exofire.net/blog/import-excel-spreadsheet-data-to-datagridview-using-vb-net

Nibaal Bazzi
Nibaal Bazzi

Total Post:2

Points:14
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