C#导入Excel|读取Excel方法

OleDbConnection读取

/// <summary>
      /// 返回Excel数据源
      /// </summary>
      /// <param name="filename">文件路径</param>
      /// <param name="TSql">TSql</param>
      /// <returns>DataSet</returns>
      public static DataSet ExcelToDataSet(string filename, string TSql)
      {
          DataSet ds;
          string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;data source=" + filename;
          OleDbConnection myConn = new OleDbConnection(strCon);
          string strCom = TSql;
          myConn.Open();
          OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myConn);
          ds = new DataSet();
          myCommand.Fill(ds);
          myConn.Close();
          return ds;
      }
      写sql语句
          string TSql = "SELECT  * FROM [sheel1$]";
        //读取数据
        DataTable table = ExcelToDataSet(fullPath, TSql).Tables[0];
 
posted @ 2017-09-01 17:36  黑哥_XJ  阅读(451)  评论(0编辑  收藏  举报