C#將Excel表格中的資料匯入SQL資料庫

來源:互聯網
上載者:User
private static string _filePath=string.Empty ;
#region Excel匯入SQL資料庫
  /// <summary>
  /// 擷取Excel資料表列表
  /// </summary>
  /// <returns></returns>
  public static ArrayList GetExcelTables()
  {
   //將Excel架構存入資料裡
   System.Data.DataTable dt = new System.Data.DataTable();
   ArrayList TablesList=new ArrayList();
   if (File.Exists(FilePath))
   {
    using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet."+
         "OLEDB.4.0;Extended Properties=\"Excel 8.0\";Data Source=" + FilePath))
    {
     try
     {
      conn.Open();
      dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
     }
     catch (Exception exp)
     {
      Log.StrFileName ="查詢Excel表單名";
      Log.StrDepartment ="Excel架構存入資料";
      Log.StrDescription =exp.ToString ();
      Log.WriteLog ();
     }     //擷取資料表個數
     int tablecount=dt.Rows.Count;
     for (int i=0;i<tablecount;i=i+2)
     {
      string tablename=dt.Rows[2].ToString().Trim().TrimEnd('$');
      if(TablesList.IndexOf(tablename)<0)
      {
       TablesList.Add(tablename);
      }
                        
     }
    }
   }
   return TablesList;
  }
  /// <summary>
  /// 匯入Excel資料表至DataTable(第一行作為表頭)
  /// </summary>
  /// <returns></returns>
  public static System.Data.DataSet FillDataSet()
  {
   if (!File.Exists(FilePath))
   {
    throw new Exception("Excel檔案不存在!");
   }
   ArrayList TableList=new ArrayList();
   TableList = GetExcelTables();
   if(TableList.Count <=0)
   {
     return null;
   }

   System.Data.DataTable table ;
            System.Data .DataSet ds=new DataSet ();
   OleDbConnection dbcon = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FilePath + ";Extended Properties=Excel 8.0");
   try
   {
    if (dbcon.State == ConnectionState.Closed)
    {
     dbcon.Open();
    }
    for(int i=0;i<TableList.Count ;i++)
    {
     string dtname=TableList.ToString ();
     try
     {
      OleDbCommand cmd = new OleDbCommand("select * from [" + dtname + "$]", dbcon);
      OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
      table=new DataTable (dtname);
      adapter.Fill(table);
      ds.Tables .Add (table);
     }
     catch (Exception exp)
     {
      Log.StrFileName =dtname;
      Log.StrDepartment ="將Excel表格匯入DataSet";
      Log.StrDescription =exp.ToString ();
      Log.WriteLog ();
     }
    }
   }
   finally
   {
    if (dbcon.State == ConnectionState.Open)
    {
     dbcon.Close();
    }
   }
   return ds;
  }
  
  /// <summary>
  /// Excel匯入資料庫
  /// </summary>
  /// <returns></returns>
  public static DataSet ImportFromExcel()
  {
   return FillDataSet();
  }

#endregion

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.