zoukankan      html  css  js  c++  java
  • 读取Excel文件到DataSet

      /// <summary>
      /// 读取Excel文件
      /// </summary>
      /// <param name="exPath">本地文件绝对路径</param>
      /// <returns></returns>
      public DataSet ReadExcelData(string strExcelPath)
      { 
       OleDbConnection oledbConn = null;
       OleDbDataAdapter comm =null;   
       try
       {
        //string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source=" + strExcelPath;
        string strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=YES';data source=" + strExcelPath;
        oledbConn = new OleDbConnection(strCon);
        oledbConn.Open();
        DataSet ds = new DataSet();
        DataTable dt = oledbConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,new object[]{null, null, null, "TABLE"});
        foreach(DataRow dr in dt.Rows)
        {
         string tableName = dr["TABLE_NAME"].ToString();
         string strSql = "SELECT * FROM [" + tableName + "]";
         string name = tableName.Remove(tableName.Length-1,1);
         if (name.ToLower()=="merit_project_data")
         {
          comm = new OleDbDataAdapter(strSql,oledbConn);
          comm.Fill(ds, name.ToLower() );
          comm.Dispose();
         }
        }
        oledbConn.Close();
        return ds;
       }
       catch(Exception ex)
       {
        throw ex;
       }
       finally
       {
        comm.Dispose();
        oledbConn.Dispose();
       }
      }
      
  • 相关阅读:
    5. java 的类和对象
    java 的变量以及构造方法
    idea运行Test时为啥会运行两次
    MYSQL(三)
    MYSQL(二)
    MySql密码操作
    MYSQL(一)
    【数据结构】2.线性表及其结构
    【数据结构】1.数据结构及算法的入门
    推荐四款可视化工具,解决99%的可视化大屏需求
  • 原文地址:https://www.cnblogs.com/dashi/p/4034726.html
Copyright © 2011-2022 走看看