SSH导入读取导入的Excel

痛定思痛。 2022-05-08 08:42 299阅读 0赞

一.前端关键代码

  1. <script type="text/javascript" src="<%=basePath%>/smp/js/jquery-1.4.2.min.js"></script>
  2. <script type="text/javascript" src="<%=basePath%>/smp/js/jquery-lib/jquery.form.js"></script>
  3. <script type="text/javascript" src="<%=basePath%>/smp/js/jquery-lib/jquery.blockUI.js"></script>
  4. <form id="thisform" method="post" enctype="multipart/form-data" name="thisform" action="<%=basePath%>smp/SmpProduct-doImport.action">
  5. <table class="TableGeneral" border="1" cellspacing="0" cellpadding="0" rules="cols">
  6. <tr class="clsItem1" height="25">
  7. <td class="TdNoPadding" align="left" >模板下载</td>
  8. <td align="left" class="TdNoPadding" style="border-bottom:#cccccc solid 1px;">
  9. <a href="<%=basePath%>smp/pages/smpproduct/addProduct.xls" style="text-decoration: underline;">下载添加商品模板</a>
  10. <a href="<%=basePath%>smp/smp/SmpProduct-downLoadProductType.action" style="text-decoration: underline;">下载商品种类对照模板</a>
  11. </td>
  12. </tr>
  13. <tr class="clsItem1" height="25">
  14. <td class="TdNoPadding" align="left">上传填写好的模板</td>
  15. <td align="left" class="TdNoPadding">
  16. <input type="file" name="importFile" id="importFile"/>
  17. </td>
  18. </tr>
  19. <tr class="clsItem1" height="25">
  20. <td class="TdNoPadding" align="center" colspan="2" >
  21. <input type="submit" value="上传导入文件" onclick="doImport();" class="InputButton" />
  22. </td>
  23. </tr>
  24. </table>
  25. <table cellpadding="0" cellspacing="0">
  26. <tr id="resultTr" style="display: none;">
  27. <td id="resultTd" valign="top" style="text-align: left;"></td>
  28. </tr>
  29. </table>
  30. </form>

70

二.js提交脚本

  1. //关闭
  2. function closep(){
  3. parent.layer.closeAll();
  4. }
  5. //导入文件
  6. function doImport() {
  7. $('#thisform').submit(function(){
  8. var fName = $("#importFile").val();
  9. var i = fName.lastIndexOf("\\");
  10. if(i!=-1)
  11. {
  12. fName = fName.substring(i+1);
  13. }
  14. if(fName=='')
  15. {
  16. alert("请选择文件(excel文件)");
  17. return false;
  18. }
  19. if(fName.substring(fName.length - 4, fName.length).toLowerCase() != ".xls") {
  20. alert("只能导入excel文件!");
  21. return false;
  22. }
  23. var options = {
  24. url : basePath+"smp/smp/SmpProduct-doImport.action?upFile="+fName,
  25. beforeSubmit: showRequest,
  26. success: showResponse,
  27. dataType: 'json'
  28. };
  29. $(this).ajaxSubmit(options);
  30. return false;//阻止表单提交
  31. });
  32. }
  33. function showRequest() {
  34. $.blockUI({message: '<div style="margin: 5px;"> ' +
  35. '<img src="'+basePath+'/smp/images/background/ajax_loader.gif"/>' +
  36. '<div style="margin: 5px; font-size: 14px;">系统正在导入数据,请稍等...</div> </div>'
  37. });
  38. return true;
  39. }
  40. function showResponse(msg) {
  41. $.unblockUI();
  42. if(msg.result == "ok") {
  43. var supNumSuc = msg.supNumSuc;
  44. var userNumSuc = msg.userNumSuc;
  45. var supArrayFai = msg.supArrayFai.length;
  46. var userArrayFai = msg.userArrayFai.length;
  47. var h = '<font color="#3366FF" style="line-height: 24px;">一共导入商品:</font>'+supNumSuc+' 个,<font color="#3366FF" style="line-height: 24px;">成功导入商品:</font>'+userNumSuc+'个。<br/>';
  48. h += '<font color="#808000" style="line-height: 24px;">未成功导入商品:</font>'+supArrayFai+' 个。';
  49. if(supArrayFai > 0) {
  50. h += '<a href="javascript:void(0);" onclick="displayOO(\'faOO\',this)">查看</a><br />';
  51. h += '<span style="display:none;" id="faOO">';
  52. $.each(msg.supArrayFai, function(i, n){
  53. h += '<span style="padding-left: 20px; line-height: 20px;">'+n.message+' </span><br/>';
  54. });
  55. h += '</span>'
  56. } else {
  57. h += '<br />';
  58. }
  59. $("#resultTr").show();
  60. $("#resultTd").html(h);
  61. }else if(msg.result == "errmodle") {
  62. $("#resultTr").show();
  63. $("#resultTd").html('<font color="red">上传文件格式应为Excel文件,后缀名为【xls】!</font>');
  64. }else if(msg.result == "fileModeErr") {
  65. $("#resultTr").show();
  66. $("#resultTd").html('<font color="red">'+msg.errInfo+'</font>');
  67. }else if(msg.result == "readFileErr"){
  68. alert("读取Excel出错!请检查所填数据格式是否合法再提交!");
  69. }else if(msg.result == "noProduct"){
  70. alert("本次无商品导入!请检查所填数据!");
  71. }
  72. }
  73. function displayOO(id, thi) {
  74. $("#"+id).toggle();
  75. if(thi.innerHTML=="查看"){
  76. thi.innerHTML = "收起";
  77. } else {
  78. thi.innerHTML = "查看";
  79. }
  80. }

三.后端关键代码

  1. private File importFile;//上传文件
  2. private List<SmpProduct> smpProductlist;//参数
  3. /*
  4. * 保存Excel导入的商品信息
  5. */
  6. public String doImport(){
  7. JSONObject json = new JSONObject();
  8. JSONArray supArrayFai = new JSONArray();
  9. JSONArray userArrayFai = new JSONArray();
  10. int userNumSuc=0;
  11. try {
  12. smpProductlist = readProductExcel();
  13. } catch (Exception e) {
  14. System.out.println("-------->>读取Excel出错!");
  15. e.printStackTrace();
  16. json.put("result", "readFileErr");
  17. ajaxResultText(json.toString());
  18. return NONE;
  19. }
  20. if(smpProductlist.size()==0){
  21. System.out.println("-------->>本次无商品导入!");
  22. json.put("result", "noProduct");
  23. ajaxResultText(json.toString());
  24. return NONE;
  25. }else{
  26. //获取该用户一级商品类别
  27. List<Map<String,Object>> commList = wechatService.getCommTypeByTypeNo(smpProductlist.get(0).getSupId(),1,"");
  28. for(int i=0; i<smpProductlist.size(); i++){
  29. try {
  30. JSONObject jo = null;
  31. String typeNo=smpProductlist.get(i).getTypeNo();
  32. if(typeNo.equals("")){
  33. jo = new JSONObject();
  34. jo.put("message", "第"+(i+2)+"行,商品类别编码不能为空!");
  35. supArrayFai.put(jo);
  36. continue;
  37. }else{
  38. if(typeNo.length()>50){
  39. jo = new JSONObject();
  40. jo.put("message", "第"+(i+2)+"行,商品类别编码不能大于50长度!");
  41. supArrayFai.put(jo);
  42. continue;
  43. }else{
  44. List<Map<String,Object>> childList = wechatService.getSmpProductTypeByFirstLevelAndTypeNo(commList, typeNo);
  45. if(childList.size()==0){
  46. jo = new JSONObject();
  47. jo.put("message", "第"+(i+2)+"行,商品类别编码不正确!");
  48. supArrayFai.put(jo);
  49. continue;
  50. }
  51. }
  52. }
  53. String productName=smpProductlist.get(i).getProductName();
  54. if(productName.equals("")){
  55. jo = new JSONObject();
  56. jo.put("message", "第"+(i+2)+"行,商品名称不能为空!");
  57. supArrayFai.put(jo);
  58. continue;
  59. }else{
  60. if(productName.length()>200){
  61. jo = new JSONObject();
  62. jo.put("message", "第"+(i+2)+"行,商品名称不能大于200长度!");
  63. supArrayFai.put(jo);
  64. continue;
  65. }
  66. }
  67. String productAlias=smpProductlist.get(i).getProductAlias();
  68. if(productAlias.equals("")){
  69. jo = new JSONObject();
  70. jo.put("message", "第"+(i+2)+"行,商品通用名称不能为空!");
  71. supArrayFai.put(jo);
  72. continue;
  73. }else{
  74. if(productAlias.length()>200){
  75. jo = new JSONObject();
  76. jo.put("message", "第"+(i+2)+"行,商品通用名称不能大于200长度!");
  77. supArrayFai.put(jo);
  78. continue;
  79. }
  80. }
  81. String factory=smpProductlist.get(i).getFactory();
  82. if(factory.equals("")){
  83. jo = new JSONObject();
  84. jo.put("message", "第"+(i+2)+"行,生产厂家不能为空!");
  85. supArrayFai.put(jo);
  86. continue;
  87. }else{
  88. if(factory.length()>200){
  89. jo = new JSONObject();
  90. jo.put("message", "第"+(i+2)+"行,生产厂家不能大于200长度!");
  91. supArrayFai.put(jo);
  92. continue;
  93. }
  94. }
  95. String registerCardNo=smpProductlist.get(i).getRegisterCardNo();
  96. if(registerCardNo.length()>100){
  97. jo = new JSONObject();
  98. jo.put("message", "第"+(i+2)+"行, 注册证号不能大于100长度!");
  99. supArrayFai.put(jo);
  100. continue;
  101. }
  102. String productCertificate=smpProductlist.get(i).getProductCertificate();
  103. if(productCertificate.equals("")){
  104. jo = new JSONObject();
  105. jo.put("message", "第"+(i+2)+"行,生产许可证不能为空!");
  106. supArrayFai.put(jo);
  107. continue;
  108. }else{
  109. if(productCertificate.length()>100){
  110. jo = new JSONObject();
  111. jo.put("message", "第"+(i+2)+"行,生产许可证不能大于100长度!");
  112. supArrayFai.put(jo);
  113. continue;
  114. }
  115. }
  116. String brand=smpProductlist.get(i).getBrand();
  117. if(brand.equals("")){
  118. jo = new JSONObject();
  119. jo.put("message", "第"+(i+2)+"行, 品牌不能为空!");
  120. supArrayFai.put(jo);
  121. continue;
  122. }else{
  123. if(brand.length()>100){
  124. jo = new JSONObject();
  125. jo.put("message", "第"+(i+2)+"行, 品牌不能大于100长度!");
  126. supArrayFai.put(jo);
  127. continue;
  128. }
  129. }
  130. String specification=smpProductlist.get(i).getSpecification();
  131. if(specification.equals("")){
  132. jo = new JSONObject();
  133. jo.put("message", "第"+(i+2)+"行, 规格不能为空!");
  134. supArrayFai.put(jo);
  135. continue;
  136. }else{
  137. if(specification.length()>300){
  138. jo = new JSONObject();
  139. jo.put("message", "第"+(i+2)+"行, 规格不能大于300长度!");
  140. supArrayFai.put(jo);
  141. continue;
  142. }
  143. }
  144. String model=smpProductlist.get(i).getModel();
  145. if(model.equals("")){
  146. jo = new JSONObject();
  147. jo.put("message", "第"+(i+2)+"行, 型号不能为空!");
  148. supArrayFai.put(jo);
  149. continue;
  150. }else{
  151. if(model.length()>300){
  152. jo = new JSONObject();
  153. jo.put("message", "第"+(i+2)+"行, 型号不能大于300长度!");
  154. supArrayFai.put(jo);
  155. continue;
  156. }
  157. }
  158. String unit=smpProductlist.get(i).getUnit();
  159. if(unit.equals("")){
  160. jo = new JSONObject();
  161. jo.put("message", "第"+(i+2)+"行, 单位不能为空!");
  162. supArrayFai.put(jo);
  163. continue;
  164. }else{
  165. if(unit.length()>20){
  166. jo = new JSONObject();
  167. jo.put("message", "第"+(i+2)+"行, 单位不能大于20长度!");
  168. supArrayFai.put(jo);
  169. continue;
  170. }
  171. }
  172. String pack=smpProductlist.get(i).getPack();
  173. if(pack.equals("")){
  174. jo = new JSONObject();
  175. jo.put("message", "第"+(i+2)+"行, 包装不能为空!");
  176. supArrayFai.put(jo);
  177. continue;
  178. }else{
  179. if(pack.length()>100){
  180. jo = new JSONObject();
  181. jo.put("message", "第"+(i+2)+"行, 包装不能大于100长度!");
  182. supArrayFai.put(jo);
  183. continue;
  184. }
  185. }
  186. jo=wechatService.addSmpProductByExcel(smpProductlist.get(i));
  187. if(!jo.getBoolean("result")){
  188. //商品未添加成功
  189. userArrayFai.put(jo);
  190. }else{
  191. userNumSuc++;
  192. }
  193. } catch (Exception e) {
  194. System.out.println("第"+(i+2)+"行["+smpProductlist.get(i).getProductName()+"]商品导入时系统出现异常!");
  195. e.printStackTrace();
  196. JSONObject jo = new JSONObject();
  197. jo.put("message", "第"+(i+2)+"行["+smpProductlist.get(i).getProductName()+"]商品导入时系统出现异常!");
  198. supArrayFai.put(jo);
  199. }
  200. }
  201. }
  202. json.put("supNumSuc", smpProductlist.size());
  203. json.put("supArrayFai", supArrayFai);
  204. json.put("userNumSuc", userNumSuc);
  205. json.put("userArrayFai", userArrayFai);
  206. json.put("result", "ok");
  207. ajaxResultText(json.toString());
  208. return NONE;
  209. }
  210. /*
  211. * 读取上传的Excel信息
  212. */
  213. private List<SmpProduct> readProductExcel() throws BiffException, IOException, JSONException{
  214. JSONObject jo = new JSONObject();
  215. //上传Excel
  216. String fileName = (request.getParameter("upFile"));
  217. if (!fileName.substring(fileName.lastIndexOf(".")).equalsIgnoreCase(".xls")) {
  218. jo.put("result", "errmodle");
  219. super.ajaxResultText(jo.toString());
  220. return null;
  221. }
  222. String path =application.getRealPath("/smp/tmp")+ File.separator;
  223. File uploadPath = new File(path);
  224. if (!uploadPath.isDirectory())
  225. uploadPath.mkdirs();
  226. long time = new Date().getTime();
  227. String fileSaveName = time + ".xls";
  228. OutputStream bos = new FileOutputStream(uploadPath + File.separator + fileSaveName);
  229. FileInputStream fileInputStream=new FileInputStream(this.getImportFile());
  230. IOUtils.copy(fileInputStream, bos);
  231. fileInputStream.close();
  232. bos.close();
  233. //获取当前用户
  234. PreSupplier ps = supplierRegService.getRegisterInfoByUserCode(super.getUserCode());
  235. //创建工作薄
  236. InputStream is=new FileInputStream(new File(path + fileSaveName));
  237. Workbook rwb = Workbook.getWorkbook(is);
  238. //获取第一张Sheet表
  239. Sheet rs=rwb.getSheet(0);
  240. List<SmpProduct> list=new ArrayList<SmpProduct>();
  241. int row=1;
  242. //读取Excel信息
  243. while(row<rs.getRows()){
  244. SmpProduct sp=new SmpProduct();
  245. sp.setSupId(ps.getPreSupId());
  246. sp.setTypeNo(rs.getCell(0, row).getContents().trim());
  247. sp.setProductName(rs.getCell(1, row).getContents().trim());
  248. sp.setProductAlias(rs.getCell(2, row).getContents().trim());
  249. sp.setStat(0);
  250. sp.setFactory(rs.getCell(3, row).getContents().trim());
  251. sp.setRegisterCardNo(rs.getCell(4, row).getContents().trim());
  252. sp.setRegisterCardNoAttrPath("");
  253. sp.setProductCertificate(rs.getCell(5, row).getContents().trim());
  254. sp.setProductCertificateAttrPath("");
  255. sp.setBrand(rs.getCell(6, row).getContents().trim());
  256. sp.setSpecification(rs.getCell(7, row).getContents().trim());
  257. sp.setModel(rs.getCell(8, row).getContents().trim());
  258. sp.setUnit(rs.getCell(9, row).getContents().trim());
  259. sp.setPack(rs.getCell(10, row).getContents().trim());
  260. sp.setRemark("");
  261. sp.setOptDate(new Date());
  262. sp.setRegFrom(0);
  263. sp.setPinYinCode("");
  264. sp.setProduceArea("");
  265. sp.setBarCode("");
  266. list.add(sp);
  267. row++;
  268. }
  269. rwb.close();
  270. return list;
  271. }

发表评论

表情:
评论列表 (有 0 条评论,299人围观)

还没有评论,来说两句吧...

相关阅读

    相关 导入Excel

    前面利用poi实现了导出excel,现在再来实现导入功能。 基本思路是前台页面上传Excel文件,将文件路径作为参数传到后台,后台获取完整路径,并通过InputStream来