How to Load Excel Sheet Names into DropDownList using C#

No.of Views1239
Bookmarked1 times
Downloads 
Votes0
By  RRaveen   On  23 Dec 2010 21:12:40
Tag : CSharp , List Controls
In this article, i will show how to load excel sheet names into dropdownlist using C#.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

Today, one of my projects I want to bind or load Excel sheets names in to DropDownList to use by users and perform calculation. I have done that using ADO.NET with C#. I would like share with you here.

Implementation

Let’s create a simple excel sheet with four sheet for this demonstration. I could not share original excel sheet of the customer here. Excel book like below, 

Image Loading

In above book have four sheets. So let’s load these four sheet names in dropdownList to select by users. I have used the OldDbConnection to connect to excel sheet and get the Scheme Tables from the book and then extract names from the source.

To use the OleDbConnection , you have to add the following namespace.

C# Code

using System.Data.OleDb;

Now write code to extract sheet names and add dropdownlist.

C# Code

using System;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;

namespace GetSheetNames
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            LoadSheetsName(@"\Sample Sheet.xlsx");
        }

        private void LoadSheetsName(string fileName)
        {
            try
            {
                DataTable tablesNames;
                cmbNames.Items.Clear();
                string connectionstring = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + fileName + ";Extended Properties=Excel 8.0;";
                using (OleDbConnection oleDbConn = new OleDbConnection(connectionstring))
                {
                    oleDbConn.Open();
                    tablesNames = oleDbConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                    if (tablesNames == null)
                    {
                        cmbNames.Items.Add("-Select-");
                    }
                    else
                    {
                        cmbNames.Items.Add("-Select-");
                        for (int i = 0; i < tablesNames.Rows.Count; i++)
                        {
                            string tableName = tablesNames.Rows[i]["TABLE_NAME"].ToString().Replace("$", "");
                            tableName = tableName.Replace("'", "");
                            cmbNames.Items.Add(tableName.Trim());
                        }
                    }
                }


            }
            catch (Exception)
            {

                throw;
            }
        }       
    }
}

In above code I had connected to excel using OleDbConnection and then open connection. After that Call GetOleDbSchemaTable() method with Tables for Get Schema of the data source. In this demonstration DataSource is excel sheet and scheme table will be every sheets.

Next when you are get the sheet names from the Tables Names object, sheet name has $ and ' .I have replaced those with empty.

string tableName = tablesNames.Rows[i]["TABLE_NAME"].ToString().Replace("$", "");
 tableName = tableName.Replace("'", "");

That's all. Now run application and see output will be followings,

Output 

Image Loading

Download Sample Project 

Download source files -44 kb

 
Sign Up to vote for this article
 
About Author
 
RRaveen
Occupation-Software Engineer
Company-TGS
Member Type-Gold
Location-Singapore
Joined date-03 Jun 2009
Home Page-codegain.com
Blog Page-www.codegain.com
- B.Sc. degree in Computer Science. - 4+ years experience in Visual C#.net and VB.net - Obsessed in OOP style design and programming. - Designing and developing Network security tools. - Designing and developing a client/server application for sharing files among users in a way other than FTP protocol. - Designing and implementing GSM gateway applications and bulk messaging. - Windows Mobile and Symbian Programming - Having knowledge with ERP solutions
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top