tags:

views:

193

answers:

1

Hi All,

I am using the following code to extract the sheet names from excel: (See attached code)

But the data is returned sorted by the NAMES of the sheets, and this is the issue. I need to extract the name of the first sheet, by index.

How can I do this?

String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
                                          "Data Source=" + fileSavePath + newFileName + ".xls; Extended Properties='Excel 8.0;HDR=NO;'";

OleDbConnection objConn = new OleDbConnection(sConnectionString);

objConn.Open();

// Get the data table containg the schema guid.
DataTable dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
 string sheetName = "Sheet1$";

if (dt != null) {
    try {
            String[] excelSheets = new String[dt.Rows.Count];

            int i = 0;
            foreach (DataRow rows in dt.Rows) {
                     excelSheets[i] = rows["TABLE_NAME"].ToString();
                      i++;
             }

             sheetName = excelSheets[0];
        }
        catch {
                 sheetName = "Sheet1$";
          }
   }
+1  A: 

try spread Sheet Gear third party component

http://www.spreadsheetgear.com

Muhammad Akhtar