This article instructs you to get Excel Sheet Names using System.Data.OleDb in C#
using System.Data.Common;
using System.Data.OleDb;
Get Excel Sheet Names
public static List<string> GetSheetNames(string path)
{
List<string> sheets = new List<string>();
string connectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;""", path);
DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb");
DbConnection connection = factory.CreateConnection();
connection.ConnectionString = connectionString;
connection.Open();
DataTable tbl = connection.GetSchema("Tables");
connection.Close();
foreach (DataRow row in tbl.Rows)
{
string sheetName = (string)row["TABLE_NAME"];
if (sheetName.EndsWith("$"))
{
sheetName = sheetName.Substring(0, sheetName.Length - 1);
}
sheets.Add(sheetName);
}
return sheets;
}
Loading data into GridView
private void btnLoadData_Click(object sender, EventArgs e)
{
if (System.IO.File.Exists(txtPath.Text))
{
string connectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;""", txtPath.Text);
string query = String.Format("select * from [{0}$]", comboBox1.SelectedItem);
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, connectionString);
DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet);
dataGridView1.DataSource = dataSet.Tables[0];
}
else
{
MessageBox.Show("No File is Selected");
}
}
Complete Source Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Common;
using System.Data.OleDb;
namespace Get_Excel_Sheet_Names
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnBrowse_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
DialogResult dlgResult = dlg.ShowDialog();
if (dlgResult == DialogResult.OK)
{
txtPath.Text = dlg.FileName;
}
comboBox1.DataSource = GetSheetNames(txtPath.Text);
}
public static List<string> GetSheetNames(string path)
{
List<string> sheets = new List<string>();
string connectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;""", path);
DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb");
DbConnection connection = factory.CreateConnection();
connection.ConnectionString = connectionString;
connection.Open();
DataTable tbl = connection.GetSchema("Tables");
connection.Close();
foreach (DataRow row in tbl.Rows)
{
string sheetName = (string)row["TABLE_NAME"];
if (sheetName.EndsWith("$"))
{
sheetName = sheetName.Substring(0, sheetName.Length - 1);
}
sheets.Add(sheetName);
}
return sheets;
}
private void btnLoadData_Click(object sender, EventArgs e)
{
if (System.IO.File.Exists(txtPath.Text))
{
string connectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;""", txtPath.Text);
string query = String.Format("select * from [{0}$]", comboBox1.SelectedItem);
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, connectionString);
DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet);
dataGridView1.DataSource = dataSet.Tables[0];
}
else
{
MessageBox.Show("No File is Selected");
}
}
}
}
Related posts:
Nice article…wel done…I like it
Thanks…
Thank you dude……
great! thnx
Very nice article. I really enjoyed it reading. And it also cleared lot of my doubts about creating excel file using c# code. Check this link too its also having nice post with wonderful explanation on writing data to excel sheet using c# code….
http://www.mindstick.com/Articles/8abc2b55-713c-4552-9dca-b36a38c686a9/?Writing%20Data%20to%20EXCEL%20Sheet%20using%20C#
Thanks lot for your nice post! keep it up.
thnx dude. nice1, it worked fr me:-)