ImportMtrlPriceByExcelExcutor.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. using JLHHJSvr.BLL;
  2. using JLHHJSvr.Com;
  3. using JLHHJSvr.Com.Model;
  4. using JLHHJSvr.LJException;
  5. using LJLib.DAL.SQL;
  6. using LJLib.Net.SPI.Server;
  7. using NPOI.HSSF.UserModel;
  8. using NPOI.HSSF.Util;
  9. using NPOI.SS.UserModel;
  10. using NPOI.XSSF.UserModel;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Data.SqlClient;
  14. using System.IO;
  15. using System.Linq;
  16. using System.Text;
  17. using System.Threading.Tasks;
  18. namespace JLHHJSvr.Excutor
  19. {
  20. internal sealed class ImportMtrlPriceByExcelExcutor : ExcutorBase<ImportMtrlPriceByExcelRequest, ImportMtrlPriceByExcelResponse>
  21. {
  22. protected override void ExcuteInternal(ImportMtrlPriceByExcelRequest request, object state, ImportMtrlPriceByExcelResponse rslt)
  23. {
  24. var tokendata = BllHelper.GetToken(request.token);
  25. if (tokendata == null)
  26. {
  27. throw new LJCommonException("会话已经中断");
  28. }
  29. using (var con = new SqlConnection(GlobalVar.ConnectionString))
  30. using (var cmd = con.CreateCommand())
  31. {
  32. con.Open();
  33. if (!string.IsNullOrEmpty(request.base64))
  34. {
  35. var typeEndIndex = request.base64.IndexOf(";base64", StringComparison.Ordinal);
  36. if (typeEndIndex <= 0)
  37. {
  38. rslt.ErrMsg = "格式异常,请重新上传";
  39. return;
  40. }
  41. request.filedata = Convert.FromBase64String(request.base64.Substring(typeEndIndex + 8));
  42. }
  43. if (request.filedata == null || request.filedata.Length == 0)
  44. {
  45. throw new LJCommonException("excel文件不能为空");
  46. }
  47. IWorkbook workbook = null;
  48. using (var ms = new MemoryStream(request.filedata))
  49. {
  50. if (request.filename.ToLower().IndexOf(".xlsx") > 0)
  51. {
  52. workbook = new XSSFWorkbook(ms);
  53. }
  54. else if (request.filename.ToLower().IndexOf(".xls") > 0)
  55. {
  56. workbook = new HSSFWorkbook(ms);
  57. }
  58. else
  59. {
  60. throw new LJCommonException("只支持excel类型文件");
  61. }
  62. }
  63. var sheet = workbook.GetSheetAt(0); //获取第一个工作表
  64. if (sheet.LastRowNum <= 0)
  65. {
  66. throw new Exception("该表没有数据");
  67. }
  68. IRow row;
  69. row = sheet.GetRow(0);
  70. if (row == null)
  71. {
  72. throw new Exception("没有数据");
  73. }
  74. IRow headerRow = sheet.GetRow(0);
  75. List<HeaderPropetry> headers = new List<HeaderPropetry>();
  76. List<HeaderPropetry> priceHeaders = new List<HeaderPropetry>();
  77. int k = 0;
  78. foreach (ICell cell in headerRow.Cells)
  79. {
  80. if(cell.ToString() == "类别" || cell.ToString() == "名称")
  81. {
  82. headers.Add(new HeaderPropetry { name = cell.ToString(), colIndex = k});
  83. }
  84. //ICellStyle cellStyle = cell.CellStyle;
  85. ////if(cellStyle.FillForegroundColorColor)
  86. //byte[] rgb;
  87. //if (request.filename.ToLower().IndexOf(".xlsx") > 0)
  88. //{
  89. // rgb = ((XSSFColor)cell.CellStyle.FillForegroundColorColor).RGB;
  90. //}
  91. //else
  92. //{
  93. // rgb = ((HSSFColor)cell.CellStyle.FillForegroundColorColor).RGB;
  94. //}
  95. //if(rgb.Count() == 3 && rgb[0].ToString() == "255" && rgb[1].ToString() == "204" && rgb[2].ToString() == "153")
  96. //if (cell.ToString().IndexOf("价格表") > -1)
  97. //{
  98. cmd.CommandText = "Select isnull(pricelistid,0) From u_pricelist where pricelistname = @pricelistname";
  99. cmd.Parameters.Clear();
  100. cmd.Parameters.AddWithValue("@pricelistname", cell.ToString().Trim());
  101. var pricelistid = Convert.ToInt32(cmd.ExecuteScalar());
  102. if (pricelistid > 0)
  103. {
  104. priceHeaders.Add(new HeaderPropetry { name = cell.ToString(), colIndex = k, keyId = pricelistid });
  105. }
  106. //}
  107. k++;
  108. }
  109. if (headers.Count <= 0 || priceHeaders.Count <= 0)
  110. {
  111. rslt.ErrMsg = "excel格式不正确";
  112. return;
  113. }
  114. using (cmd.Transaction = con.BeginTransaction())
  115. {
  116. try
  117. {
  118. rslt.allqty = sheet.LastRowNum - 1;
  119. int successqty = 0;
  120. rslt.messageList = new List<string>();
  121. for (int rowIndex = 1; rowIndex <= sheet.LastRowNum; rowIndex++)
  122. {
  123. row = sheet.GetRow(rowIndex);
  124. if (row == null) continue;
  125. var mtrltype = string.Empty;
  126. var mtrlname = string.Empty;
  127. foreach (var head in headers)
  128. {
  129. ICell cell = row.GetCell(head.colIndex);
  130. if (cell == null || cell.ToString() == "") continue;
  131. if (head.name == "类别")
  132. {
  133. mtrltype = cell.ToString();
  134. }
  135. else if (head.name == "名称")
  136. {
  137. mtrlname = cell.ToString();
  138. }
  139. }
  140. if (!string.IsNullOrEmpty(mtrltype) && !string.IsNullOrEmpty(mtrlname))
  141. {
  142. var mtrlid = 0;
  143. cmd.CommandText = @"Select isnull(u_mtrl_price.mtrlid,0) from u_mtrl_price
  144. where u_mtrl_price.mtrltype = (select isnull(mtrltypeid,0) from u_mtrltype where mtrltype = @mtrltype)
  145. and u_mtrl_price.name = @mtrlname";
  146. cmd.Parameters.Clear();
  147. cmd.Parameters.AddWithValue("@mtrltype", mtrltype);
  148. cmd.Parameters.AddWithValue("@mtrlname", mtrlname);
  149. mtrlid = Convert.ToInt32(cmd.ExecuteScalar());
  150. if (mtrlid == 0)
  151. {
  152. //throw new Exception(string.Format("类别:{0},名称:{1}的物料类别或物料不存在",mtrltype,mtrlname));
  153. rslt.messageList.Add(string.Format("第【{0}】行, 类别:【{1}】,名称:【{2}】", rowIndex, mtrltype, mtrlname));
  154. continue;
  155. }
  156. foreach (var pricename in priceHeaders)
  157. {
  158. var _cell = row.GetCell(pricename.colIndex);
  159. if (GetCellValue(_cell).ToString() == "")
  160. {
  161. continue;
  162. }
  163. var updatePrice = new u_mtrl_price_pricelist { mtrlid = mtrlid, pricelistid = pricename.keyId };
  164. //ICell cell = row.GetCell(pricename.colIndex);
  165. updatePrice.price = Convert.ToDecimal(GetCellValue(_cell));
  166. DbSqlHelper.InsertOrUpdate(cmd, updatePrice, "price");
  167. successqty++;
  168. }
  169. }
  170. }
  171. rslt.successqty = successqty;
  172. cmd.Transaction.Commit();
  173. }
  174. catch (Exception ex)
  175. {
  176. cmd.Transaction.Rollback();
  177. rslt.ErrMsg = ex.Message;
  178. }
  179. }
  180. }
  181. }
  182. private object GetCellValue(ICell cell)
  183. {
  184. if (cell == null) return null;
  185. switch (cell.CellType)
  186. {
  187. case CellType.Numeric:
  188. if (DateUtil.IsCellDateFormatted(cell))
  189. {
  190. return cell.DateCellValue.ToString("yyyy-MM-dd");
  191. }
  192. else
  193. {
  194. return cell.NumericCellValue;
  195. }
  196. case CellType.Boolean:
  197. return cell.BooleanCellValue;
  198. default:
  199. return cell.ToString().Trim();
  200. }
  201. }
  202. public class HeaderPropetry
  203. {
  204. public string name { get; set; }
  205. public int colIndex { get; set; }
  206. public int keyId { get; set; }
  207. }
  208. }
  209. }