How to populate listbox from excel using C#
-
12-06-2021 - |
Domanda
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.
Soluzione
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..
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow