1

I have an windows application. I browse for the file and select the excel file using OpenFileDialog control. The excel file contains email id's in column A. I want to populate the list-box with excel file column values. Office 2003 is installed on my machine. Can somebody Please help me out? Thanks in Advance.

Unknown
  • 721
  • 1
  • 8
  • 12

1 Answers1

1

Refer: Reading Excel files from C#

To connect to an excel file you need the appropriate connection string:

string connString = @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=<YourExcelPath>;
Extended Properties=\"Excel 12.0;HDR=YES;\"";

After use the OleDb classes to query the information from the file:

string selectCmd = "SELECT * FROM <SheetName>";

using(OleDbConnection excelConn = new OleDbConnection(connString))
{
    excelConn.Open(); 
    OleDbCommand command = new OleDbCommand(selectCmd, excelConn);
    OleDbDataAdapter da = new OleDbDataAdapter(command);

    DataTable sheetInfo = new DataTable();
    dataAdapter.Fill(sheetInfo);

    //Do something with the data.
    Bind your control with this datatable here
}

So you need to replace "YourExcelPath" with the path of your excel file..

Community
  • 1
  • 1
Niranjan Singh
  • 18,017
  • 2
  • 42
  • 75