AfterSaleImportUpload.aspx.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. using BizCom;
  15. using NPOI.HSSF.UserModel;
  16. using NPOI.SS.UserModel;
  17. using NPOI.HPSF;
  18. public partial class EAfterSale_AfterSaleImportUpload : ReportBase
  19. {
  20. protected void Page_Load(object sender, EventArgs e)
  21. {
  22. if (!IsPostBack)
  23. {
  24. }
  25. }
  26. private bool checkFile(HttpPostedFile postedFile)
  27. {
  28. if (postedFile != null && postedFile.ContentLength > 0)
  29. {
  30. string fileName = postedFile.FileName;
  31. if (fileName.IndexOf(".xls", StringComparison.OrdinalIgnoreCase) == -1)
  32. return false;
  33. string fileType = postedFile.ContentType;
  34. if (fileType == "application/vnd.ms-excel" || fileType == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  35. return true;
  36. }
  37. return false;
  38. }
  39. protected void btnSure_Click(object sender, EventArgs e)
  40. {
  41. if (Request.Files.Count > 0 && Request.Files[0].FileName != "")
  42. {
  43. HttpPostedFile file = Request.Files[0];
  44. UploadFile(file);
  45. iSheet = hssfworkbook.GetSheetAt(0);
  46. System.Collections.IEnumerator rows = iSheet.GetRowEnumerator();
  47. int i = 0;
  48. StringBuilder str = new StringBuilder();
  49. List<object> keys = new List<object>();
  50. string ctids_add = "";
  51. while (rows.MoveNext())
  52. {
  53. if (i == 0) { i++; continue; }
  54. IRow row = (HSSFRow)rows.Current;
  55. ICell cell = row.GetCell(0);
  56. if (cell == null) break;
  57. //订单号为空则当前行不导入
  58. string ctid = row.GetCell(0).ToString().Trim();
  59. if (ctid == "")
  60. continue;
  61. if (ctids_add.IndexOf(ctid) != -1)
  62. continue;
  63. ctids_add += ctid;
  64. ctids_add += ",";
  65. string reason = row.GetCell(1).ToString().Trim();
  66. string method = "";
  67. if (row.Cells.Count >= 3)
  68. {
  69. method = row.GetCell(2).ToString().Trim();
  70. }
  71. CeErpTradeCell entity = null;
  72. entity = CeErpTradeCell.GetByCtid(ctid);
  73. if (entity != null)
  74. {
  75. str.AppendFormat("update ce_erptradecell with(rowlock) set AfterSaleState=1,AfterSaleReason={1},AfterSaleMethod={2},AfterSaleTime=getdate() where ctid={0} ;", ctid, reason, method);
  76. i++;
  77. }
  78. }
  79. if (i > 1)
  80. {
  81. try
  82. {
  83. DbHelper.DbConn.ExecuteNonQuery(str.ToString());
  84. }
  85. catch (Exception ex)
  86. {
  87. XLog.SaveLog(0, "导入售后" + "," + ex.Message);
  88. }
  89. litResult.Text = "导入成功!";
  90. ShowResult("导入成功", "closeFn()");
  91. }
  92. else
  93. {
  94. litResult.Text = "所有订单号已存在,无法重复导入!";
  95. ShowResult("所有订单号已存在,无法重复导入!");
  96. }
  97. }
  98. }
  99. public void UploadFile(HttpPostedFile file)
  100. {
  101. //FileStream f = new FileStream(
  102. Stream s = file.InputStream;
  103. hssfworkbook = new HSSFWorkbook(s);
  104. DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
  105. dsi.Company = "领淘";
  106. hssfworkbook.DocumentSummaryInformation = dsi;
  107. SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
  108. si.Subject = excelName;
  109. hssfworkbook.SummaryInformation = si;
  110. }
  111. public static DataSet ExcelDataSource(string filepath)
  112. {
  113. //string strConn = "Provider=Microsoft.ACE.OleDb.12.0;Data Source=" + filepath + ";Extended Properties='Excel 12.0;HDR=yes;IMEX=1;'";
  114. string strConn="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties=Excel 8.0;";
  115. using (OleDbConnection conn = new OleDbConnection(strConn))
  116. {
  117. conn.Open();
  118. DataTable sheetNames = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
  119. ArrayList al = new ArrayList();
  120. foreach (DataRow dr in sheetNames.Rows)
  121. {
  122. al.Add(dr[2]);
  123. }
  124. OleDbDataAdapter oada = new OleDbDataAdapter("select * from [" + al[0] + "]", strConn);
  125. DataSet ds = new DataSet();
  126. oada.Fill(ds);
  127. conn.Close();
  128. return ds;
  129. }
  130. }
  131. }