public partial class Form1 : Form { public Form1() { InitializeComponent(); } public string[] GetExcelSheetNames(string excelfilename) { OleDbConnection con = null; DataTable dt = null; string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelfilename + ";Extended Properties=Excel 12.0;"; con = new OleDbConnection(connStr); con.Open(); dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); if (dt == null) { return null; } string[] excelSheetNames = new String[dt.Rows.Count]; int i = 0; foreach (DataRow row in dt.Rows) { excelSheetNames[i] = row["TABLE_NAME"].ToString(); i++; } con.Close(); return excelSheetNames; } private void button1_Click(object sender, EventArgs e) { OpenFileDialog openDialog = new OpenFileDialog(); openDialog.Title = ""; openDialog.InitialDirectory = @"C:\\"; openDialog.Filter = "ExcelSheet(*.xsls)|*.xsls|All Files(*.*)|*.*"; openDialog.FilterIndex = 1; openDialog.RestoreDirectory = true; if (openDialog.ShowDialog() == DialogResult.OK) { if (openDialog.FileName != "") { textBox1.Text = openDialog.FileName; comboBox1.DataSource = GetExcelSheetNames(openDialog.FileName); } } else { MessageBox.Show("chose Excel sheet path..", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); } } private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e) { OleDbConnection con = null; String conStr = "Provider=Microsoft.Jet.OLEDB..0;" + "Data Source=" + textBox1.Text + ";Extended Properties=Excel 8.0;"; con = new OleDbConnection(conStr); OleDbCommand oconn = new OleDbCommand("Select * From [" + comboBox1.SelectedValue + "]", con); OleDbDataAdapter da = new OleDbDataAdapter(oconn); DataTable data = new DataTable(); da.Fill(data); dataGridView1.DataSource = data; } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)