使用asp.net获取Excel工作表的索引 [英] Get Index of the Excel Sheet using asp.net

查看:55
本文介绍了使用asp.net获取Excel工作表的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码从excel中提取工作表名称:(请参阅随附的代码)

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.

我该怎么做?

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$";
          }
   }

推荐答案

尝试扩展Sheet Gear第三方组件

try spread Sheet Gear third party component

http://www.spreadsheetgear.com

这篇关于使用asp.net获取Excel工作表的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆