ImportMtrlPriceByExcelExcutor.cs 8.9 KB

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