ImportBill.aspx.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. public partial class EFinance_ImportBill : BasePage
  16. {
  17. protected void Page_Load(object sender, EventArgs e)
  18. {
  19. if (!IsPostBack)
  20. {
  21. }
  22. }
  23. private bool checkFile(HttpPostedFile postedFile)
  24. {
  25. if (postedFile != null && postedFile.ContentLength > 0)
  26. {
  27. string fileName = postedFile.FileName;
  28. if (fileName.IndexOf(".xls", StringComparison.OrdinalIgnoreCase) == -1)
  29. return false;
  30. string fileType = postedFile.ContentType;
  31. if (fileType == "application/vnd.ms-excel" || fileType == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  32. return true;
  33. }
  34. return false;
  35. }
  36. protected void btnSure_Click(object sender, EventArgs e)
  37. {
  38. HttpPostedFile postedFile = xlsFile.PostedFile;
  39. if (!checkFile(postedFile))
  40. {
  41. litResult.Text = "选择上传的文件格式不正确,无法上传,格式是xls或xlsx!";
  42. return;
  43. }
  44. string exted = Path.GetExtension(postedFile.FileName);
  45. string filePath = System.Environment.GetEnvironmentVariable("TMP");
  46. //if (!Directory.Exists(filePath))
  47. //{
  48. // Directory.CreateDirectory(filePath);
  49. //}
  50. string file = filePath + "\\ZhangDan" + DateTime.Now.ToFileTime() + exted;
  51. postedFile.SaveAs(file);
  52. if (File.Exists(file))
  53. {
  54. DataSet ds = ExcelDataSource(file);
  55. DataTable dt = ds.Tables[0];
  56. StringBuilder str = new StringBuilder();
  57. foreach (DataRow dr in dt.Rows)
  58. {
  59. //订单号为空则当前行不导入
  60. string ctid = dr[0].ToString().Trim();
  61. if (ctid == "")
  62. continue;
  63. string fileName = dr[4].ToString().Trim();
  64. string num = dr[5].ToString().Trim();
  65. string price = dr[6].ToString().Trim();
  66. string total = dr[7].ToString().Trim();
  67. string logistics = dr[8].ToString().Trim();
  68. string logisticsNo = dr[9].ToString().Trim();
  69. string logisticsAmount = dr[10].ToString().Trim();
  70. string weight = dr[11].ToString().Trim();
  71. string province = dr[12].ToString().Trim();
  72. string finishDate = dr[13].ToString().Trim();
  73. string suplierName = dr[14].ToString().Trim();
  74. CeErpTradeCell entity = null;
  75. entity = CeErpTradeCell.GetByCtid(ctid);
  76. string sProductName = "";
  77. if (entity != null)
  78. {
  79. CeErpProduct eProduct = null;
  80. eProduct = CeErpProduct.GetById(entity.ProductId);
  81. if (eProduct != null)
  82. sProductName = eProduct.PType;
  83. CeErpZhangDan ZhangDanEntity = null;
  84. ZhangDanEntity = CeErpZhangDan.GetBytid(ctid);
  85. if (ZhangDanEntity == null)
  86. {
  87. str.Append(" insert into Ce_ErpZhangDan(num,tid,batchNo,price,total,logistics,logisticsNo,logisticsAmount,weight,fileName,province,supplierName,unusual,explain,importDate,importUserId,shopId,state,finishDate)");
  88. str.AppendFormat(" Values({9},'{0}','',{1},{2},'{3}','{4}',{5},{6},'{7}','{8}','{11}','','',getdate(),{10},1,1,'{12}');",
  89. ctid, price, total, logistics, logisticsNo, logisticsAmount, weight, fileName, province, num,CurrentUser.UserID, suplierName, finishDate);
  90. }
  91. }
  92. }
  93. if (str.Length > 0)
  94. {
  95. DbHelper.DbConn.ExecuteNonQuery(str.ToString());
  96. litResult.Text = "导入成功!";
  97. ShowResult("导入成功", "closeFn()");
  98. }
  99. else
  100. {
  101. litResult.Text = "所有账单已存在,无法重复导入!";
  102. ShowResult("所有账单已存在,无法重复导入!");
  103. }
  104. }
  105. }
  106. public static DataSet ExcelDataSource(string filepath)
  107. {
  108. //string strConn = "Provider=Microsoft.ACE.OleDb.12.0;Data Source=" + filepath + ";Extended Properties='Excel 12.0;HDR=yes;IMEX=1;'";
  109. string strConn="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties=Excel 8.0;";
  110. using (OleDbConnection conn = new OleDbConnection(strConn))
  111. {
  112. conn.Open();
  113. DataTable sheetNames = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
  114. ArrayList al = new ArrayList();
  115. foreach (DataRow dr in sheetNames.Rows)
  116. {
  117. al.Add(dr[2]);
  118. }
  119. OleDbDataAdapter oada = new OleDbDataAdapter("select * from [" + al[0] + "]", strConn);
  120. DataSet ds = new DataSet();
  121. oada.Fill(ds);
  122. conn.Close();
  123. return ds;
  124. }
  125. }
  126. }