使用asp.net获取Excel表的索引

时间:2009-06-15 13:02:32

标签: asp.net excel

我使用以下代码从excel中提取工作表名称:(参见附件代码)

但是数据按照工作表的NAMES排序,这就是问题所在。我需要通过索引提取第一张表的名称。

我该怎么做?

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 个答案:

答案 0 :(得分:1)

尝试传播Sheet Gear第三方组件

http://www.spreadsheetgear.com

相关问题