ImportSample.aspx.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 EFinance_ImportSample : 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 sql = new StringBuilder();
  49. List<object> keys = new List<object>();
  50. while (rows.MoveNext())
  51. {
  52. if (i == 0) { i++; continue; }
  53. IRow row = (HSSFRow)rows.Current;
  54. ICell cell = row.GetCell(0);
  55. if (cell == null) break;
  56. //订单号为空则当前行不导入
  57. string ctid = row.GetCell(0).ToString().Trim();
  58. if (ctid == "")
  59. continue;
  60. string ReceiveDate = "";
  61. string dateStr = row.GetCell(1).ToString().Trim();
  62. if (dateStr.IndexOf("/") < 3 && dateStr.IndexOf("/") > 0)
  63. {
  64. string[] datelist = dateStr.Split(' ');
  65. if (datelist.Length > 1)
  66. {
  67. string needdate = datelist[0];
  68. string[] dlist = needdate.Split('/');
  69. string changedt = "";
  70. for (int idx = dlist.Length - 1; idx >= 0; idx--)
  71. {
  72. changedt += dlist[idx];
  73. if (idx != 0)
  74. {
  75. changedt += "-";
  76. }
  77. }
  78. ReceiveDate = changedt + " " + datelist[1];
  79. }
  80. else
  81. ReceiveDate = "";
  82. }
  83. else
  84. {
  85. ReceiveDate = row.GetCell(1).ToString().Trim();
  86. }
  87. string AdvanceFee = row.GetCell(2).ToString().Trim();
  88. string Refund = row.GetCell(3).ToString().Trim();
  89. string ServiceFee = row.GetCell(4).ToString().Trim();
  90. string Commission = row.GetCell(5).ToString().Trim();
  91. string LogisticsFee = row.GetCell(6).ToString().Trim();
  92. CeErpTradeCell entity = null;
  93. entity = CeErpTradeCell.GetByCtid(ctid);
  94. CeErpTradeSample TradeSampleEntity = null;
  95. TradeSampleEntity = CeErpTradeSample.GetBytid(ctid);
  96. int supplierId = Convert.ToInt32(webConfig.SampleOrderSupplier);
  97. if (entity != null && TradeSampleEntity == null)
  98. {
  99. //新增拿样子表数据
  100. sql.Append(" insert into CE_ErpTradeSample(ctid,ReceiveDate,AdvanceFee,Refund,Commission,ServiceFee,LogisticsFee,SampleDate,SampleUserId)");
  101. sql.AppendFormat(" Values('{0}','{6}',{1},{2},{3},{4},{5},getdate(),{7});", ctid, AdvanceFee, Refund, Commission, ServiceFee, LogisticsFee, ReceiveDate, CurrentUser.UserID);
  102. sql.AppendFormat("update CE_ErpTradeCell set IsSample=1 ,SupplierId={1},OrderState=6,seller_memo='拿样订单【导入】' where ctid='{0}';", ctid, supplierId);
  103. CeErpTrade TradeEntity = null;
  104. TradeEntity = CeErpTrade.Get(entity.tid);
  105. if (TradeEntity != null)
  106. {
  107. //taobaoHelper.TradeMemoUpdate(ctid, TradeEntity.seller_nick, 5, "拿样订单【导入】");//修改备注,5为紫色旗子
  108. string res = apiHelper.API_TradeMemoUpdate(TradeEntity.tid, TradeEntity.posCode, "PURPLE", "拿样订单【导入】");
  109. }
  110. i++;
  111. }
  112. }
  113. if (i > 1)
  114. {
  115. try
  116. {
  117. DbHelper.DbConn.ExecuteNonQuery(sql.ToString());
  118. }
  119. catch (Exception ex)
  120. {
  121. XLog.SaveLog(0, "导入拿样" + "," + ex.Message);
  122. }
  123. litResult.Text = "导入成功!";
  124. ShowResult("导入成功", "closeFn()");
  125. }
  126. else
  127. {
  128. litResult.Text = "所有订单号已存在,无法重复导入!";
  129. ShowResult("所有订单号已存在,无法重复导入!");
  130. }
  131. }
  132. }
  133. public void UploadFile(HttpPostedFile file)
  134. {
  135. //FileStream f = new FileStream(
  136. Stream s = file.InputStream;
  137. hssfworkbook = new HSSFWorkbook(s);
  138. DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
  139. dsi.Company = "领淘";
  140. hssfworkbook.DocumentSummaryInformation = dsi;
  141. SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
  142. si.Subject = excelName;
  143. hssfworkbook.SummaryInformation = si;
  144. }
  145. //protected void btnSure_Click(object sender, EventArgs e)
  146. //{
  147. // HttpPostedFile postedFile = xlsFile.PostedFile;
  148. // if (!checkFile(postedFile))
  149. // {
  150. // litResult.Text = "选择上传的文件格式不正确,无法上传,格式是xls或xlsx!";
  151. // return;
  152. // }
  153. // string exted = Path.GetExtension(postedFile.FileName);
  154. // string filePath = System.Environment.GetEnvironmentVariable("TMP");
  155. // string file = filePath + "\\Sample" + DateTime.Now.ToFileTime() + exted;
  156. // postedFile.SaveAs(file);
  157. // if (File.Exists(file))
  158. // {
  159. // DataSet ds = ExcelDataSource(file);
  160. // DataTable dt = ds.Tables[0];
  161. // StringBuilder str = new StringBuilder();
  162. // int importCnt = 0;
  163. // foreach (DataRow dr in dt.Rows)
  164. // {
  165. // //订单号为空则当前行不导入
  166. // string ctid = dr[0].ToString().Trim();
  167. // if (ctid == "")
  168. // continue;
  169. // string ReceiveDate = dr[1].ToString().Trim();
  170. // string AdvanceFee = dr[2].ToString().Trim();
  171. // string Refund = dr[3].ToString().Trim();
  172. // string ServiceFee = dr[4].ToString().Trim();
  173. // string Commission = dr[5].ToString().Trim();
  174. // string LogisticsFee = dr[6].ToString().Trim();
  175. // CeErpTradeCell entity = null;
  176. // entity = CeErpTradeCell.GetByCtid(ctid);
  177. // CeErpTradeSample TradeSampleEntity = null;
  178. // TradeSampleEntity = CeErpTradeSample.GetBytid(ctid);
  179. // int supplierId = getSupplierIDByName(webConfig.SampleOrderSupplier);
  180. // if (entity != null && TradeSampleEntity == null)
  181. // {
  182. // str = new StringBuilder();
  183. // //新增拿样子表数据
  184. // str.Append(" insert into CE_ErpTradeSample(ctid,ReceiveDate,AdvanceFee,Refund,Commission,ServiceFee,LogisticsFee,SampleDate,SampleUserId)");
  185. // str.AppendFormat(" Values('{0}','{6}',{1},{2},{3},{4},{5},getdate(),{7});", ctid, AdvanceFee, Refund, Commission, ServiceFee, LogisticsFee, ReceiveDate,CurrentUser.UserID);
  186. // str.AppendFormat("update CE_ErpTradeCell set IsSample=1 ,SupplierId={1},OrderState=6,seller_memo='拿样订单【导入】' where ctid='{0}';", ctid,supplierId);
  187. // DbHelper.DbConn.ExecuteNonQuery(str.ToString());
  188. // CeErpTrade TradeEntity = null;
  189. // TradeEntity = CeErpTrade.Get(entity.tid);
  190. // if (TradeEntity != null)
  191. // {
  192. // //taobaoHelper.TradeMemoUpdate(ctid, TradeEntity.seller_nick, 5, "拿样订单【导入】");//修改备注,5为紫色旗子
  193. // apiHelper.API_TradeMemoUpdate(TradeEntity.tid, TradeEntity.posCode, "PURPLE", "拿样订单【导入】");
  194. // }
  195. // importCnt++;
  196. // }
  197. // }
  198. // if (importCnt > 0)
  199. // {
  200. // litResult.Text = "导入成功!";
  201. // ShowResult("导入成功", "closeFn()");
  202. // }
  203. // else
  204. // {
  205. // litResult.Text = "所有订单号已存在,无法重复导入!";
  206. // ShowResult("所有订单号已存在,无法重复导入!");
  207. // }
  208. // }
  209. //}
  210. public int getSupplierIDByName(string name)
  211. {
  212. StringBuilder sql2 = new StringBuilder();
  213. sql2.AppendFormat("select top 1 * from CE_ErpSupplier where ComName like '%{0}%';", name);
  214. DataTable dt2 = DbHelper.DbConn.ExecuteDataset(sql2.ToString()).Tables[0];
  215. if (dt2.Rows.Count > 0)
  216. {
  217. return Convert.ToInt32(dt2.Rows[0]["ID"]);
  218. }
  219. return 0;
  220. }
  221. public static DataSet ExcelDataSource(string filepath)
  222. {
  223. //string strConn = "Provider=Microsoft.ACE.OleDb.12.0;Data Source=" + filepath + ";Extended Properties='Excel 12.0;HDR=yes;IMEX=1;'";
  224. string strConn="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties=Excel 8.0;";
  225. using (OleDbConnection conn = new OleDbConnection(strConn))
  226. {
  227. conn.Open();
  228. DataTable sheetNames = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
  229. ArrayList al = new ArrayList();
  230. foreach (DataRow dr in sheetNames.Rows)
  231. {
  232. al.Add(dr[2]);
  233. }
  234. OleDbDataAdapter oada = new OleDbDataAdapter("select * from [" + al[0] + "]", strConn);
  235. DataSet ds = new DataSet();
  236. oada.Fill(ds);
  237. conn.Close();
  238. return ds;
  239. }
  240. }
  241. }