使用.net获取excel文件表名sheet的源代码
下面源代码将获取到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 }