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();
       }
      }
      
  • 相关阅读:
    动态二维码
    二维码
    购物车
    logback学习与配置使用
    UML类图几种关系的总结
    java.lang.Excetion,java.lang.RuntimeException,java.lang.Error有什么区别?
    Java编程最差实践
    Hibernate利用@DynamicInsert和@DynamicUpdate生成动态SQL语句
    从 Java 代码到 Java 堆
    Project configuration is not up-to-date with pom.xml
  • 原文地址:https://www.cnblogs.com/dashi/p/4034726.html
Copyright © 2011-2022 走看看