drupal stats

Get Excel Sheet Names using C#

July 14th, 2011 Windows Forms 6 Comments


Get Excel Sheet Names thumb Get Excel Sheet Names using C#

 

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");

            }

        }

    }

}

zip Get Excel Sheet Names using C# Download Example Code

Tags: get sheet name excel c#, how to get excel sheet name in c#, get excel sheet name c#, how to get sheet name in excel using c#, c# excel sheet name, c# get excel sheet names, get excel sheet name in C#, C# excel get worksheet by name, c# get excel sheet name, how to get the excel sheet name in c#

Related posts:

  1. Import Excel Data to GridView
  2. Import Excel file into DataGridView
  3. Import Data from CSV file to GridView
  4. Export GridView to Excel in ASP.NET
  5. Export DataGridView to Excel

6 Comments

  1. john says:

    Nice article…wel done…I like it

  2. Ragav says:

    Thanks…

  3. Raj says:

    Thank you dude……

  4. uae says:

    great! thnx

  5. Ashish Pandey says:

    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.

  6. Aashni says:

    thnx dude. nice1, it worked fr me:-)