.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Get Excel file sheetname using C#

Posted By:Dhivakaran       Posted Date: November 29, 2010    Points: 25    Category: C#    URL: http://www.dotnetspark.com  

Here I m going to show how do we get sheetnames in one excel file.
 

Step1:

using System.Text;
using System.Data.OleDb;
using System.Data;

  public static string[] GetAllExcelSheetNames(string excelFileName)
        {
            OleDbConnection con = null;
            DataTable dt = null;
            String[] excelSheetNames ={ };
            try
            {
                conStr = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + excelFileName + ";Extended Properties=Excel 8.0;";
                con = new OleDbConnection(conStr);
                con.Open();
                dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                if (dt == null)
                {
                    return null;
                }
                excelSheetNames = new String[dt.Rows.Count];
                int i = 0;
                foreach (DataRow row in dt.Rows)
                {
                    excelSheetNames[i] = row["TABLE_NAME"].ToString();
                    i++;
                }
            }
            catch(Exception ex)
            {
                throw new Exception  ex.Message.ToString();
            }
            finally
            {
                con = null;
                dt = null;
            }
            return excelSheetNames;
        }

string[] arrsheet=GetAllExcelSheetNames("Excelfile");

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend