Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6159353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:07:05+00:00 2026-05-23T21:07:05+00:00

I want to read Excel file in c# using following code string excelFileName =

  • 0

I want to read Excel file in c# using following code

string excelFileName = "Book2.xls";
string excelConnectString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=Book2.xls;Extended Properties=""Excel 8.0;HDR=YES;""";
//string excelConnectString = @"Provider = Microsoft.Jet.OLEDB.4.0;Data Source = " + excelFileName + ";" + "Extended Properties = Excel 8.0; HDR=Yes;IMEX=1";

OleDbConnection objConn = new OleDbConnection(excelConnectString);
OleDbCommand objCmd = new OleDbCommand("Select * From [Sheet1$]", objConn);

OleDbDataAdapter objDatAdap = new OleDbDataAdapter();
objDatAdap.SelectCommand = objCmd;
DataSet ds = new DataSet();
objDatAdap.Fill(ds);

enter image description here

Everything is working fine.Now my requirement is to read the excel file something like below

SELECT A,B,D From [Sheet1];
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T21:07:05+00:00Added an answer on May 23, 2026 at 9:07 pm

    The Select-command should look like this if you want to read A1 to D1:

    SELECT * FROM [SHEETNAME_HERE$A1:D1]
    

    Whole Code:

    OleDbConnection con = new OleDbConnection(
        "provider=Microsoft.Jet.OLEDB.4.0;data source="
        + XLS_FILE_NAME_AND_PATH_HERE
        + ";Extended Properties=Excel 8.0;");
    
    StringBuilder stbQuery = new StringBuilder();
    stbQuery.Append("SELECT * FROM [" + SHEETNAME_HERE + "$A1:D1]");
    OleDbDataAdapter adp = new OleDbDataAdapter(stbQuery.ToString(), con);
    
    DataSet dsXLS = new DataSet();
    adp.Fill(dsXLS);
    
    DataView dvEmp = new DataView(dsXLS.Tables[0]);
    
    dataGridView1.DataSource = dvEmp;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have got the following code from here to read an Excel file using
I'm using Apache POI 3.6, I want to read an excel file which has
I'm using vb.net 2003 and I want to read excel file 2003 using OleDb
I want to read already open excel file with C#. I am using this
I want to read/retrieve an Image from excel file to PHP using PHPExcel. This
Hi I want to parse an excel file using zend framework. I went to
I want to insert data from excel file to MySQL database using JSP. once
I want to read simple excel xml file to dictionary. I have tried to
How can I read the excel file using C# 4.0. Once I saw in
I have a perl script read Excel file and parse using a perl module

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.