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

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.


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

  public static string[] GetAllExcelSheetNames(string excelFileName)
            OleDbConnection con = null;
            DataTable dt = null;
            String[] excelSheetNames ={ };
                conStr = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + excelFileName + ";Extended Properties=Excel 8.0;";
                con = new OleDbConnection(conStr);
                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();
            catch(Exception ex)
                throw new Exception  ex.Message.ToString();
                con = null;
                dt = null;
            return excelSheetNames;

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

 Subscribe to Articles


Further Readings:


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