ImportEmployee.aspx.cs.exclude 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 Ehr_ImportEmployee : 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 == "")
  62. continue;
  63. string mobile = dr[1].ToString().Trim();
  64. string mobile2 = dr[2].ToString().Trim();
  65. string zw = dr[3].ToString().Trim();
  66. string sex = dr[4].ToString().Trim();
  67. string jointime = dr[5].ToString().Trim();
  68. //不存在则新增会员
  69. str.Append(" insert into ce_erpperson(name,mobile,mobile2,sex,CreateTime)");
  70. str.AppendFormat(" Values('{0}','{1}','{2}','{3}',getdate()) ;select @userId=SCOPE_IDENTITY() ;", personName, mobile, mobile2, sex);
  71. str.AppendFormat(" insert into ce_erppersonpost(personid,orgid,postid) select @userId,orgid,id from ce_erppost where name='{0}' ;", zw);
  72. }
  73. if (str.Length > 0)
  74. {
  75. DbHelper.DbConn.ExecuteNonQuery(str.ToString());
  76. litResult.Text = "导入成功!";
  77. ShowResult("导入成功", "closeFn();");
  78. }
  79. }
  80. }
  81. public static DataSet ExcelDataSource(string filepath)
  82. {
  83. string strConn = "Provider=Microsoft.ACE.OleDb.12.0;Data Source=" + filepath + ";Extended Properties='Excel 12.0;HDR=yes;IMEX=1;'";
  84. //string strConn="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties=Excel 8.0;";
  85. using (OleDbConnection conn = new OleDbConnection(strConn))
  86. {
  87. conn.Open();
  88. DataTable sheetNames = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
  89. ArrayList al = new ArrayList();
  90. foreach (DataRow dr in sheetNames.Rows)
  91. {
  92. al.Add(dr[2]);
  93. }
  94. OleDbDataAdapter oada = new OleDbDataAdapter("select * from [" + al[0] + "]", strConn);
  95. DataSet ds = new DataSet();
  96. oada.Fill(ds);
  97. conn.Close();
  98. return ds;
  99. }
  100. }
  101. }