将Excel文件数据导入到SqlServer数据库的三种方案

 

方案一: 通过OleDB方式获取Excel文件的数据,然后通过DataSet中转到SQL Server,这种方法的优点是非常的灵活,可以对Excel表中的各个单元格进行用户所需的操作。

  1. openFileDialog = new OpenFileDialog();
  2. openFileDialog.Filter = "Excel files(*.xls)|*.xls";
  3. if(openFileDialog.ShowDialog()==DialogResult.OK)
  4. {
  5. FileInfo fileInfo = new FileInfo(openFileDialog.FileName);
  6. string filePath = fileInfo.FullName;
  7. string connExcel = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties=Excel 8.0";
  8. try
  9. {
  10. OleDbConnection oleDbConnection = new OleDbConnection(connExcel);
  11. oleDbConnection.Open();
  12. //获取excel表
  13. DataTable dataTable = oleDbConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
  14. //获取sheet名,其中[0][1]...[N]: 按名称排列的表单元素
  15. string tableName = dataTable.Rows[0][2].ToString().Trim();
  16. tableName = "[" + tableName.Replace("'","") + "]";
  17. //利用SQL语句从Excel文件里获取数据
  18. //string query = "SELECT classDate,classPlace,classTeacher,classTitle,classID FROM " + tableName;
  19. string query = "SELECT 日期,开课城市,讲师,课程名称,持续时间 FROM " + tableName;
  20. dataSet = new DataSet();
  21. //OleDbCommand oleCommand = new OleDbCommand(query, oleDbConnection);
  22. //OleDbDataAdapter oleAdapter = new OleDbDataAdapter(oleCommand);
  23. OleDbDataAdapter oleAdapter = new OleDbDataAdapter(query,connExcel);
  24. oleAdapter.Fill(dataSet,"gch_Class_Info");
  25. //从excel文件获得数据后,插入记录到SQL Server的数据表
  26. DataTable dataTable1 = new DataTable();
  27. SqlDataAdapter sqlDA1 = new SqlDataAdapter(@"SELECT classID, classDate,
  28. classPlace, classTeacher, classTitle, durativeDate FROM gch_Class_Info",sqlConnection1);
  29. //SqlCommandBuilder sqlCB1 = new SqlCommandBuilder(sqlDA1);
  30. sqlDA1.Fill(dataTable1);
  31. foreach(DataRow dataRow in dataSet.Tables["gch_Class_Info"].Rows)
  32. {
  33. DataRow dataRow1 = dataTable1.NewRow();
  34. dataRow1["classDate"] = dataRow["日期"];
  35. dataRow1["classPlace"] = dataRow["开课城市"];
  36. dataRow1["classTeacher"] = dataRow["讲师"];
  37. dataRow1["classTitle"] = dataRow["课程名称"];
  38. dataRow1["durativeDate"] = dataRow["持续时间"];
  39. dataTable1.Rows.Add(dataRow1);
  40. }
  41. Console.WriteLine("新插入 " + dataTable1.Rows.Count.ToString() + " 条记录");
  42. sqlDA1.Update(dataTable1);
  43. oleDbConnection.Close();
  44. }
  45. catch(Exception ex)
  46. {
  47. Console.WriteLine(ex.ToString());
  48. }
  49. }

方案二: 直接通过SQL语句执行SQL Server的功能函数将Excel文件转换到SQL Server数据库。

OpenFileDialog openFileDialog = new OpenFileDialog();

  1. openFileDialog.Filter = "Excel files(*.xls)|*.xls";
  2. SqlConnection sqlConnection1 = null;
  3. if(openFileDialog.ShowDialog()==DialogResult.OK)
  4. {
  5. string filePath = openFileDialog.FileName;
  6. sqlConnection1 = new SqlConnection();
  7. sqlConnection1.ConnectionString = "server=(local);integrated security=SSPI;initial catalog=Library";
  8. //import excel into SQL Server 2000
  9. /*string importSQL = "SELECT * into live41 FROM OpenDataSource" +
  10. "('Microsoft.Jet.OLEDB.4.0','Data Source=" + "\"" + "E:\\022n.xls" + "\"" +
  11. "; User ID=;Password=; Extended properties=Excel 5.0')...[Sheet1$]";*/
  12. //export SQL Server 2000 into excel
  13. string exportSQL = @"EXEC master..xp_cmdshell
  14. 'bcp Library.dbo.live41 out " + filePath + "-c -q -S" + "\"" + "\"" +
  15. " -U" + "\"" + "\"" + " -P" + "\"" + "\"" + "\'";
  16. try
  17. {
  18. sqlConnection1.Open();
  19. //SqlCommand sqlCommand1 = new SqlCommand();
  20. //sqlCommand1.Connection = sqlConnection1;
  21. //sqlCommand1.CommandText = importSQL;
  22. //sqlCommand1.ExecuteNonQuery();
  23. //MessageBox.Show("import finish!");
  24. SqlCommand sqlCommand2 = new SqlCommand();
  25. sqlCommand2.Connection = sqlConnection1;
  26. sqlCommand2.CommandText = exportSQL;
  27. sqlCommand2.ExecuteNonQuery();
  28. MessageBox.Show("export finish!");
  29. }
  30. catch(Exception ex)
  31. {
  32. MessageBox.Show(ex.ToString());
  33. }
  34. }
  35. if(sqlConnection1!=null)
  36. {
  37. sqlConnection1.Close();
  38. sqlConnection1 = null;
  39. }

方案三: 通过到入Excel的VBA dll,通过VBA接口获取Excel数据到DataSet

  1. OpenFileDialog openFile = new OpenFileDialog();
  2. openFile.Filter = "Excel files(*.xls)|*.xls";
  3. ExcelIO excelio = new ExcelIO();
  4. if(openFile.ShowDialog()==DialogResult.OK)
  5. {
  6. if(excelio!=null)
  7. excelio.Close();
  8. excelio = new ExcelIO(openFile.FileName);
  9. object[,] range = excelio.GetRange();
  10. excelio.Close();
  11. DataSet ds = new DataSet("xlsRange");
  12. int x = range.GetLength(0);
  13. int y = range.GetLength(1);
  14. DataTable dt = new DataTable("xlsTable");
  15. DataRow dr;
  16. DataColumn dc;
  17. ds.Tables.Add(dt);
  18. for(int c=1; c<=y; c++)
  19. {
  20. dc = new DataColumn();
  21. dt.Columns.Add(dc);
  22. }
  23. object[] temp = new object[y];
  24. for(int i=1; i<=x; i++)
  25. {
  26. dr = dt.NewRow();
  27. for(int j=1; j<=y; j++)
  28. {
  29. temp[j-1] = range[i,j];
  30. }
  31. dr.ItemArray = temp;
  32. ds.Tables[0].Rows.Add(dr);
  33. }
  34. dataGrid1.SetDataBinding(ds,"xlsTable");
  35. if(excelio!=null)
  36. excelio.Close();
  37. }

  当然还有其他一些方法,如遍历Excel文件中的数据然后构造sql语句,直接利用sql操作Excel文件导入数据库等,这些都是很常见的方法,因此就不再做收录了。最后说明下,以上的方法是我从网上找的源码并做了一定的修改,希望对大家有帮助。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值