黑马程序员技术交流社区

标题: 将EXCEL中的数据导入SQL SERVER数据库 [打印本页]

作者: 王晨    时间: 2012-12-14 23:05
标题: 将EXCEL中的数据导入SQL SERVER数据库
亲测可用,贴出来供大家分享,需要的话拷走可用!
  1. protected void btnUpload_Click(object sender, EventArgs e)
  2. {
  3. if (this.fuOpen.PostedFile.FileName != "")
  4. {
  5. //这里是确保文件是excel格式
  6. //Response.Write(this.fuOpen.FileName.Substring(this.fuOpen.FileName.LastIndexOf('.')));
  7. if (this.fuOpen.FileName.Substring(this.fuOpen.FileName.LastIndexOf('.') + 1) == "xls")
  8. {
  9. Random rd = new Random(1);
  10. string filename = DateTime.Now.Date.ToString("yyyymmdd") + DateTime.Now.ToLongTimeString().Replace(":", "") + rd.Next(9999).ToString() + ".xls";
  11. try
  12. {
  13. this.fuOpen.PostedFile.SaveAs(@Server.MapPath("fileupload/") + filename);
  14. }
  15. catch (HttpException he)
  16. {
  17. Response.Write("文件上传不成功,请检查文件是否过大,是否有写权限!");
  18. return;
  19. }
  20. #region --------读取文件内容到服务器内存----------
  21. string conn = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =" + Server.MapPath("fileupload") + "/" + filename + ";Extended Properties=Excel 8.0";
  22. OleDbConnection thisconnection = new OleDbConnection(conn);
  23. thisconnection.Open();
  24. //这是从EXCEL中读取内容,要保证字段名和excel表中的字段名相同
  25. string Sql = "select taotiName, Que_type, timu_neirong, OptionA, OptionB, OptionC, OptionD,Que_answer from [Sheet1$]";
  26. OleDbDataAdapter mycommand = new OleDbDataAdapter(Sql, thisconnection);
  27. DataSet ds = new DataSet();
  28. mycommand.Fill(ds, "[Sheet1$]");
  29. thisconnection.Close();
  30. this.GridView1.DataSource = ds;
  31. this.GridView1.DataBind();
  32. #endregion

  33. #region --------插入到数据库中---------

  34. string conn1 = "Data Source=.\\SQLEXPRESS;Initial Catalog=ExamOnline;Integrated Security=SSPI;Provider=SQLOLEDB.1;";
  35. OleDbConnection thisconnection1 = new OleDbConnection(conn1);
  36. thisconnection1.Open();
  37. int count = ds.Tables["[Sheet1$]"].Rows.Count;

  38. for (int i = 0; i < count; i++)
  39. {
  40. string taotiName, Que_type, timu_neirong, OptionA, OptionB, OptionC, OptionD, Que_answer;
  41. taotiName = ds.Tables["[Sheet1$]"].Rows[i]["taotiName"].ToString();
  42. timu_neirong = ds.Tables["[Sheet1$]"].Rows[i]["timu_neirong"].ToString();
  43. Que_type = ds.Tables["[Sheet1$]"].Rows[i]["Que_type"].ToString();
  44. OptionA = ds.Tables["[Sheet1$]"].Rows[i]["OptionA"].ToString();
  45. OptionB = ds.Tables["[Sheet1$]"].Rows[i]["OptionB"].ToString();
  46. OptionC = ds.Tables["[Sheet1$]"].Rows[i]["OptionC"].ToString();
  47. OptionD = ds.Tables["[Sheet1$]"].Rows[i]["OptionD"].ToString();
  48. Que_answer = ds.Tables["[Sheet1$]"].Rows[i]["Que_answer"].ToString();
  49. string excelsql = "insert into Questions_info(taotiName, Que_type, timu_neirong, OptionA, OptionB, OptionC, OptionD,Que_answer) values('" + taotiName + "','" + Que_type + "','" + timu_neirong + "','" + OptionA + "','" + OptionB + "','" + OptionC + "','" + OptionD + "','" + Que_answer + "') ";
  50. OleDbCommand mycommand1 = new OleDbCommand(excelsql, thisconnection1);
  51. try
  52. {
  53. mycommand1.ExecuteNonQuery();
  54. }
  55. catch (OleDbException ode)
  56. {
  57. Response.Write("<b>导入不成功,请重试!</b>");
  58. return;
  59. }
  60. }
  61. Response.Write("更新成功");
  62. thisconnection1.Close();
  63. #endregion
  64. }
  65. else
  66. {
  67. Response.Write("导入文件的格式不正确!");
  68. }

  69. }
  70. else
  71. {
  72. Response.Write("您还没有选择要导入的文件!");
  73. }

  74. }
复制代码

作者: 许庭洲    时间: 2012-12-15 07:37
值得学习ing!
作者: 夏闯富    时间: 2013-6-23 09:50
强大!对应的把数据里面的数据导出为Excel表格的代码呢?求学习,谢谢.




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2