• 读取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();
       }
      }
      
  • 相关阅读:
    天下第一 (spfa判断环)
    网络的可靠性 (最小生成树)
    星际之门(一) (快幂+最小生成树)
    吝啬的国度 建图+深搜
    表达式求值 第九届河南省省赛
    Apple Tree (树形结构变为线性结构+树状数组)
    士兵杀敌(五)(线段树??)
    动物统计加强版
    Supermarket
    生活的烦恼
  • 原文地址:https://www.cnblogs.com/dashi/p/4034726.html
Copyright © 2020-2023  润新知