Reputation: 393
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$";
}
}
Upvotes: 0
Views: 1057
Reputation: 52241
try spread Sheet Gear third party component
http://www.spreadsheetgear.com
Upvotes: 1