DeliveredImportUpload.aspx.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 EDelivery_DeliveredImportUpload : 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. // HttpPostedFile postedFile = xlsFile.PostedFile;
  42. // if (!checkFile(postedFile))
  43. // {
  44. // litResult.Text = "选择上传的文件格式不正确,无法上传,格式是xls或xlsx!";
  45. // return;
  46. // }
  47. // string exted = Path.GetExtension(postedFile.FileName);
  48. // string filePath = System.Environment.GetEnvironmentVariable("TMP");
  49. // string file = filePath + "\\Delivered" + 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. // StringBuilder str = new StringBuilder();
  56. // foreach (DataRow dr in dt.Rows)
  57. // {
  58. // //订单号为空则当前行不导入
  59. // string ctid = dr[0].ToString().Trim();
  60. // if (ctid == "")
  61. // continue;
  62. // string Logistics = dr[1].ToString().Trim();
  63. // string Supplier = dr[2].ToString().Trim();
  64. // string LogisticsNum = dr[3].ToString().Trim();
  65. // CeErpTradeCell entity = null;
  66. // entity = CeErpTradeCell.GetByCtid(ctid);
  67. // CeErpExpressInfo ExpEntity = null;
  68. // ExpEntity = CeErpExpressInfo.GetByCtid(ctid);
  69. // if (entity != null && ExpEntity == null)
  70. // {
  71. // str.Append(" insert into CE_ErpExpressInfo(tid,company_name,out_sid,supplierUserName,importUserId,import_file,import_time)");
  72. // str.AppendFormat(" Values('{0}','{1}','{2}','{3}',{5},'{4}',getdate());", ctid, Logistics, LogisticsNum, Supplier, postedFile.FileName,CurrentUser.UserID);
  73. // str.Append(" insert into CE_ErpTradeLog(tid,OrderState,UserId,OperateTime,Con,AfterSaleState)");
  74. // str.AppendFormat(" Values('{0}',7,'{1}',getdate(),'{2}',0);", ctid, CurrentUser.UserID, "导入发货");
  75. // }
  76. // }
  77. // if (str.Length > 0)
  78. // {
  79. // DbHelper.DbConn.ExecuteNonQuery(str.ToString());
  80. // litResult.Text = "导入成功!";
  81. // ShowResult("导入成功", "closeFn()");
  82. // }
  83. // else
  84. // {
  85. // litResult.Text = "所有发货单号已存在,无法重复导入!";
  86. // ShowResult("所有发货单号已存在,无法重复导入!", "");
  87. // }
  88. // }
  89. //}
  90. protected void btnSure_Click(object sender, EventArgs e)
  91. {
  92. if (Request.Files.Count > 0 && Request.Files[0].FileName != "")
  93. {
  94. HttpPostedFile file = Request.Files[0];
  95. UploadFile(file);
  96. iSheet = hssfworkbook.GetSheetAt(0);
  97. System.Collections.IEnumerator rows = iSheet.GetRowEnumerator();
  98. int i = 0;
  99. StringBuilder str = new StringBuilder();
  100. List<object> keys = new List<object>();
  101. string ctids_add = "";
  102. while (rows.MoveNext())
  103. {
  104. if (i == 0) { i++; continue; }
  105. IRow row = (HSSFRow)rows.Current;
  106. ICell cell = row.GetCell(0);
  107. if (cell == null) break;
  108. //订单号为空则当前行不导入
  109. string ctid = row.GetCell(0).ToString().Trim();
  110. if (ctid == "")
  111. continue;
  112. if (ctids_add.IndexOf(ctid) != -1)
  113. continue;
  114. ctids_add += ctid;
  115. ctids_add += ",";
  116. string Logistics = row.GetCell(1).ToString().Trim();
  117. string Supplier = row.GetCell(2).ToString().Trim();
  118. string LogisticsNum = row.GetCell(3).ToString().Trim();
  119. CeErpTradeCell entity = null;
  120. entity = CeErpTradeCell.GetByCtid(ctid);
  121. CeErpExpressInfo ExpEntity = null;
  122. ExpEntity = CeErpExpressInfo.GetByCtid(ctid);
  123. if (entity != null && ExpEntity == null)
  124. {
  125. str.Append(" insert into CE_ErpExpressInfo(tid,company_name,out_sid,supplierUserName,importUserId,import_file,import_time,print_time,printUser)");
  126. str.AppendFormat(" Values('{0}','{1}','{2}','{3}',{5},'{4}',getdate(),getdate(),'{3}');", ctid, Logistics, LogisticsNum, Supplier, Request.Files[0].FileName, CurrentUser.UserID);
  127. str.Append(" insert into CE_ErpTradeLog(tid,OrderState,UserId,OperateTime,Con,AfterSaleState)");
  128. str.AppendFormat(" Values('{0}',7,'{1}',getdate(),'{2}',0);", ctid, CurrentUser.UserID, "导入发货");
  129. i++;
  130. }
  131. }
  132. if (i > 1)
  133. {
  134. try
  135. {
  136. DbHelper.DbConn.ExecuteNonQuery(str.ToString());
  137. }
  138. catch (Exception ex)
  139. {
  140. XLog.SaveLog(0, "导入发货" + "," + ex.Message);
  141. }
  142. litResult.Text = "导入成功!";
  143. ShowResult("导入成功", "closeFn()");
  144. }
  145. else
  146. {
  147. litResult.Text = "所有订单号已存在,无法重复导入!";
  148. ShowResult("所有订单号已存在,无法重复导入!");
  149. }
  150. }
  151. }
  152. public void UploadFile(HttpPostedFile file)
  153. {
  154. //FileStream f = new FileStream(
  155. Stream s = file.InputStream;
  156. hssfworkbook = new HSSFWorkbook(s);
  157. DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
  158. dsi.Company = "领淘";
  159. hssfworkbook.DocumentSummaryInformation = dsi;
  160. SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
  161. si.Subject = excelName;
  162. hssfworkbook.SummaryInformation = si;
  163. }
  164. public static DataSet ExcelDataSource(string filepath)
  165. {
  166. //string strConn = "Provider=Microsoft.ACE.OleDb.12.0;Data Source=" + filepath + ";Extended Properties='Excel 12.0;HDR=yes;IMEX=1;'";
  167. string strConn="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties=Excel 8.0;";
  168. using (OleDbConnection conn = new OleDbConnection(strConn))
  169. {
  170. conn.Open();
  171. DataTable sheetNames = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
  172. ArrayList al = new ArrayList();
  173. foreach (DataRow dr in sheetNames.Rows)
  174. {
  175. al.Add(dr[2]);
  176. }
  177. OleDbDataAdapter oada = new OleDbDataAdapter("select * from [" + al[0] + "]", strConn);
  178. DataSet ds = new DataSet();
  179. oada.Fill(ds);
  180. conn.Close();
  181. return ds;
  182. }
  183. }
  184. }