HOW TO SELECT RECORD FROM EXCEL SPREADSHEET IN C#?

Pravesh Singh

Total Post:412

Points:2888
Posted by  Pravesh Singh
C# 
C#
 5087  View(s)
Ratings:
Rate this:
Hello Everyone,
Can anyone tell me how to select record from excel spreadsheet using c# code?
  1. AVADHESH PATEL

    Post:604

    Points:4228
    Re: How to select record from excel spreadsheet in C#?

    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.

  1. Sohail Shaikh

    Post:7

    Points:49
    Re: How to select record from excel spreadsheet in C#?

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

  1. Nibaal Bazzi

    Post:2

    Points:14
    Re: How to select record from excel spreadsheet in C#?

    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.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!