0% found this document useful (0 votes)
84 views

Extracting Data From C# Excel File To ListBox

This code opens an Excel file dialog to select one or more Excel files. It then opens a connection to the selected file, executes a SQL query to select data from a range on the first sheet, adds any non-empty results to a list box, and closes the connection.

Uploaded by

Diksin Baba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views

Extracting Data From C# Excel File To ListBox

This code opens an Excel file dialog to select one or more Excel files. It then opens a connection to the selected file, executes a SQL query to select data from a range on the first sheet, adds any non-empty results to a list box, and closes the connection.

Uploaded by

Diksin Baba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

private void button1_Click(object sender, EventArgs e)

{
openFileDialog1.Title = "Lütfen Dosya Seçiniz";
openFileDialog1.Filter = " (*.xlsx)|*.xlsx";
openFileDialog1.FilterIndex = 1;
openFileDialog1.Multiselect = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string dosya_adres = openFileDialog1.FileName; OleDbConnection con
= new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
dosya_adres + ";Extended Properties=Excel 12.0");
con.Open();
string sql = "SELECT * from [Sayfa1$A1:A5000] ";
OleDbCommand veri2 = new OleDbCommand(sql, con); OleDbDataReader dr
= null;
dr = veri2.ExecuteReader();

while (dr.Read())
{
if (dr[0] != "")
{
listBox1.Items.Add(dr[0].ToString());
}
else
{
break;
}
}
con.Close();
}
}

You might also like