imports.aspx.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using NPOI.HSSF.UserModel;
  2. using NPOI.SS.UserModel;
  3. using NPOI.HPSF;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Text;
  8. using System.Web;
  9. using SiteCore;
  10. using BizCom;
  11. public partial class plug_imports : ReportBase
  12. {
  13. protected void Page_Load(object sender, EventArgs e)
  14. {
  15. if (IsPostBack)
  16. {
  17. if (Request.Files.Count > 0 && Request.Files[0].FileName != "")
  18. {
  19. HttpPostedFile file = Request.Files[0];
  20. UploadFile(file);
  21. iSheet = hssfworkbook.GetSheetAt(0);
  22. System.Collections.IEnumerator rows = iSheet.GetRowEnumerator();
  23. StringBuilder sql = new StringBuilder();
  24. int i = 0;
  25. List<object> keys = new List<object>();
  26. while (rows.MoveNext())
  27. {
  28. if (i == 0) { i++; continue; }
  29. IRow row = (HSSFRow)rows.Current;
  30. ICell cell = row.GetCell(0);
  31. if (cell == null) break;
  32. string code = row.GetCell(0).StringCellValue;
  33. if (code == "" || code == "条码号") break;
  34. string name = "";
  35. if (row.GetCell(1) != null) name = row.GetCell(1).StringCellValue;
  36. //insert into S_LyxyUser(UserNo, UserName, Faculties)values('XW00404', '王雅琦', '校外读者500')
  37. name = name.Replace(" ", "").Replace(" ", "");
  38. sql = new StringBuilder();
  39. sql.AppendFormat(" if (select count(0) from S_LyxyUser where UserName='{0}' and UserNo='{1}') < 1 ", name,code);
  40. sql.Append(" begin ");
  41. sql.AppendFormat(" insert into S_LyxyUser(UserNo,UserName) values('{0}','{1}') ;", code, name);
  42. sql.Append(" end ");
  43. try
  44. {
  45. SqlHelper.ExecuteNonQuery(sql.ToString());
  46. }
  47. catch (Exception ex)
  48. {
  49. XLog.SaveLog(0,code + "," + name, ex);
  50. break;
  51. }
  52. i++;
  53. }
  54. ShowResult("导入成功!", "closeFn(1)");
  55. }
  56. }
  57. }
  58. public void UploadFile(HttpPostedFile file)
  59. {
  60. //FileStream f = new FileStream(
  61. Stream s = file.InputStream;
  62. hssfworkbook = new HSSFWorkbook(s);
  63. DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
  64. dsi.Company = "连城鑫港";
  65. hssfworkbook.DocumentSummaryInformation = dsi;
  66. SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
  67. si.Subject = excelName;
  68. hssfworkbook.SummaryInformation = si;
  69. }
  70. }