龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > asp.net编程 >

使用.net获取excel文件表名sheet的源代码

时间:2009-12-21 11:47来源:未知 作者:admin 点击:
分享到:
下面源代码将获取到excel文件中sheet表名的方法: 1 protected string[] sheetfromexcel(string filepath) 2 { 3 OleDbConnection objConn = null; 4 DataTable dt = null; 5 string[] sheetName = null; 6 try 7 { 8 string connString =

下面源代码将获取到excel文件中sheet表名的方法:

1 protected string[] sheetfromexcel(string filepath)
 2     {
 3         OleDbConnection objConn = null;
 4         DataTable dt = null;
 5         string[] sheetName = null;
 6         try
 7         {
 8             string connString =
 9                 "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+filepath+";Extended Properties=Excel 8.0;";
10             objConn = new OleDbConnection(connString);
11             objConn.Open();
12             dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
13
14             if (dt != null)
15             {
16                 //string[] sheetName = new string[dt.Rows.Count];
17                 sheetName = new string[dt.Rows.Count];
18                 int i = 0;
19                 foreach (DataRow row in dt.Rows)
20                 {
21                     sheetName[i] = row["TABLE_NAME"].ToString();
22                     i++;
23                 }
24             }
25             objConn.Close();
26             objConn.Dispose();
27         }
28         catch (Exception e1)
29         {
30             Response.Write(e1.Message);
31         }
32         return sheetName;
33
34     }

精彩图集

赞助商链接