一个c#与Excel的例子

来源:互联网 发布:软件架构总结 编辑:程序博客网 时间:2024/06/06 04:51

/// <summary>
  ///  将Excel中的数据读入DataSet
  /// </summary>
  /// <param name="fileName">文件路径和文件名</param>
  /// <returns>DataSet</returns>
  private DataSet ReadExcel(string fileName)
  {
   string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +"Data Source="+ fileName +";"+"Extended Properties=Excel 8.0;";
   OleDbConnection conn = new OleDbConnection(strConn);
   conn.Open(); 
   string strExcel = "";  
   OleDbDataAdapter myCommand = null;
   strExcel="select * from [sheet1$]";//从默认的sheet1表中读入数据
   myCommand = new OleDbDataAdapter(strExcel, strConn);
   DataSet ds = new DataSet();
   myCommand.Fill(ds,"Data");
   conn.Close();
   return ds;
  }


原创粉丝点击