【发布时间】:2009-06-18 12:27:42
【问题描述】:
我正在使用以下代码从 excel 中提取工作表名称:(见附件代码)
但是返回的数据是按工作表的名称排序的,这就是问题所在。我需要按索引提取第一张纸的名称。
我该怎么做?
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$";
}
}
【问题讨论】: