0

Hi All,

I want to populate the listbox columwise from excelsheet using C# .net.
Column 1 of excelsheet should map with listbox1 and column 2 with listbox2,.....
Excel sheet is placed at folder C:\Data\..
Any ideas?


Thanks,
Shweta

2
Contributors
6
Replies
8
Views
6 Years
Discussion Span
Last Post by shwetas1
0

Do you have that line of code.
Please share.


Thanks.

Use OleDB data provider or Office InterOp API.

0

Hi,

Could you please help to correct this code:

string excelFilePath = "d:\\documents and settings\\rsr44\\My Documents\\Exporttoxls\\TestReport.xls";
        string sExcelConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;
        Data Source=" + excelFilePath + ";Extended Properties=" + "\"Excel 8.0;HDR=YES;\"";  
        string myExcelDataQuery = "Select * from [TestReport$],sExcelConnectionString";
        OleDbConnection OleDbConn = new OleDbConnection(sExcelConnectionString);
        OleDbCommand OleDbCmd = new OleDbCommand(myExcelDataQuery, OleDbConn);
        OleDbConn.Open();
        OleDbDataReader dr = OleDbCmd.ExecuteReader();

and please share if any one have i am new to it.

Thanks.

Edited by __avd: Added [code] tags

0

Take a look at,

string cnstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\sample.xls;Extended Properties=" + "\"Excel 8.0;HDR=YES\"";
            using (System.Data.OleDb.OleDbDataAdapter adp=new System.Data.OleDb.OleDbDataAdapter("select * from [TestReport$]",cnstr))
            {
                DataTable dt = new DataTable();
                adp.Fill(dt);
                listBox1.DataSource = dt;
                listBox1.DisplayMember = dt.Columns[0].ColumnName;
                listBox1.ValueMember  = dt.Columns[1].ColumnName;
            }
0

Thanks for the response.

I am getting error at " adp.Fill(dt);"
Error :
'TestReport$' is not a valid name. Make sure that it does not include invalid characters or punctuation and that it is not too long.

Please help me with this.

Thanks.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.