oImports.aspx.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using SQLData;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.Data.OleDb;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Web;
  11. using System.Web.UI;
  12. using System.Web.UI.WebControls;
  13. using SiteCore;
  14. public partial class plug_oImports : BasePage
  15. {
  16. protected void Page_Load(object sender, EventArgs e)
  17. {
  18. if (!IsPostBack)
  19. {
  20. }
  21. }
  22. private bool checkFile(HttpPostedFile postedFile)
  23. {
  24. if (postedFile != null && postedFile.ContentLength > 0)
  25. {
  26. string fileName = postedFile.FileName;
  27. if (fileName.IndexOf(".xls", StringComparison.OrdinalIgnoreCase) == -1)
  28. return false;
  29. string fileType = postedFile.ContentType;
  30. if (fileType == "application/vnd.ms-excel" || fileType == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  31. return true;
  32. }
  33. return false;
  34. }
  35. protected void btnSure_Click(object sender, EventArgs e)
  36. {
  37. HttpPostedFile postedFile = xlsFile.PostedFile;
  38. if (!checkFile(postedFile))
  39. {
  40. litResult.Text = "选择上传的文件格式不正确,无法上传,格式是xls或xlsx!";
  41. return;
  42. }
  43. string exted = Path.GetExtension(postedFile.FileName);
  44. string filePath = "";
  45. if (!Directory.Exists(filePath))
  46. {
  47. Directory.CreateDirectory(filePath);
  48. }
  49. string file = filePath+"\\"+DateTime.Now.ToFileTime() + exted;
  50. postedFile.SaveAs(file);
  51. if (File.Exists(file))
  52. {
  53. DataSet ds = ExcelDataSource(file);
  54. DataTable dt = ds.Tables[0];
  55. //dt.Rows.RemoveAt(0);
  56. StringBuilder str = new StringBuilder();
  57. //str.Append(" declare @userId int ;");
  58. foreach (DataRow dr in dt.Rows)
  59. {
  60. string personName = dr[0].ToString().Trim();
  61. if (personName == "序号") continue;
  62. if (personName == "")
  63. break;
  64. //不存在则新增会员
  65. str.AppendFormat(" delete from S_Subject where title='{0}';", dr[1].ToString().Trim());
  66. str.Append(" insert into S_Subject(title,stype,optiona,optionb,optionc,optiond,answer,resolution,point)");
  67. str.AppendFormat(" Values('{0}',{1},'{2}','{3}','{4}','{5}','{6}','{7}',{8}) ;",
  68. dr[1].ToString().Trim(), getType(dr[2].ToString().Trim()), dr[3].ToString().Trim(), dr[4].ToString().Trim(), dr[5].ToString().Trim(), dr[6].ToString().Trim(), dr[7].ToString().Trim(), dr[8].ToString().Trim(), dr[9].ToString().Trim());
  69. }
  70. if (str.Length > 0)
  71. {
  72. DbHelper.DbConn.ExecuteNonQuery(str.ToString());
  73. //DbHelper.DbConn.ExecuteNonQuery(str.ToString());
  74. litResult.Text = "导入成功!";
  75. ShowResult("导入成功", "closeFn();");
  76. }
  77. }
  78. }
  79. private int getType(string t)
  80. {
  81. t = t.Replace("题", "");
  82. switch (t) {
  83. case "单选":return 1;
  84. case "多选": return 2;
  85. case "判断": return 3;
  86. }
  87. return 1;
  88. }
  89. public static DataSet ExcelDataSource(string filepath)
  90. {
  91. string strConn = "Provider=Microsoft.ACE.OleDb.12.0;Data Source=" + filepath + ";Extended Properties='Excel 12.0;HDR=yes;IMEX=1;'";
  92. //string strConn="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties=Excel 8.0;";
  93. using (OleDbConnection conn = new OleDbConnection(strConn))
  94. {
  95. conn.Open();
  96. DataTable sheetNames = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
  97. ArrayList al = new ArrayList();
  98. foreach (DataRow dr in sheetNames.Rows)
  99. {
  100. al.Add(dr[2]);
  101. }
  102. OleDbDataAdapter oada = new OleDbDataAdapter("select * from [" + al[0] + "]", strConn);
  103. DataSet ds = new DataSet();
  104. oada.Fill(ds);
  105. conn.Close();
  106. return ds;
  107. }
  108. }
  109. }