码迷,mamicode.com
首页 > 其他好文 > 详细

读取指定路径的Excel内容到DataTable中

时间:2016-05-17 22:38:44      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:

 1 /// <summary>
 2 /// 读取指定路径的Excel内容到DataTable中
 3 /// </summary>
 4 /// <param name="path"></param>
 5 /// <returns></returns>
 6 public DataTable ImportToDataSet(string path)
 7 {
 8     string strConn = "Provider=Microsoft.Ace.OleDb.12.0;" + "Data Source=" + path + ";" + "Extended Properties=‘Excel 12.0;HDR=Yes;IMEX=1‘;";
 9        OleDbConnection conn = new OleDbConnection(strConn);
10      try
11    {
12         DataTable dt = new DataTable();
13         if (conn.State != ConnectionState.Open)
14             conn.Open();
15           string strExcel = "select * from [Sheet1$]";
16          OleDbDataAdapter adapter = new OleDbDataAdapter(strExcel, conn);
17             adapter.Fill(dt);
18             return dt;
19     }
20    catch (Exception ex)
21 {
22       throw new Exception(ex.Message);
23   }
24    finally
25   {
26       if (conn.State != ConnectionState.Closed)
27          conn.Close();
28   }
29 }

 

读取指定路径的Excel内容到DataTable中

标签:

原文地址:http://www.cnblogs.com/linhongquan/p/5503377.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!