Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
how to read sheet name from Excel sheet
 

dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,null);
 
         for (int i = 0; i < dtExcelSchema.Rows.Count; i++)
         {
             drpsheet.DataSource=dtExcelSchema.Rows[i]["Table_Name"].ToString().Replace("$","");
         }
 
         //TableName.Rows[0]["ColumnName"].ToString();
         drpsheet.DataBind();
Posted 16-Oct-12 20:51pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I remember an old post a while ago where the guy used a string array to get all sheets.
Not hundred percent sure but off the bat i would say something like this.
Havent tested code but sheet names should be added to the array.
 
int i = 0;
String[] xSheets = new String[dtExcelSchema.rows.count];
 
foreach(DataRow row in dt.Rows)
{
xSheets[i] = row["tablename"].ToString();
i++;
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi,
 
See the following code. It might be help you.
String connString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
          "Data Source=" + excelFile + ";Extended Properties=Excel 8.0;";
        objConn = new OleDbConnection(connString);
        objConn.Open();
        
        dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
 
        if(dt == null)
        {
           return null;
        }
 
        String[] excelSheets = new String[dt.Rows.Count];
        int i = 0;
        
        foreach(DataRow row in dt.Rows)
        {
           excelSheets[i] = row["TABLE_NAME"].ToString();
           i++;
        }
 
Thnaks,
Viprat
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

 Microsoft.Office.Interop.Excel.ApplicationClass excelObj = new  Microsoft.Office.Interop.Excel.ApplicationClass();
 Microsoft.Office.Interop.Excel.Workbook workBookObj = null;
 workBookObj = excelObj.Workbooks.Open(fileName, Missing.Value, Missing.Value,Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
foreach (Microsoft.Office.Interop.Excel.Worksheet sheet in workBookObj.Sheets)
{
 sheet.Name
}
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 6,745
1 OriginalGriff 6,696
2 CPallini 5,315
3 George Jonsson 3,599
4 Gihan Liyanage 2,650


Advertise | Privacy | Mobile
Web03 | 2.8.140922.1 | Last Updated 17 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100