下午在做少年儿童出版社的订单转换:他们家给的是这样的格式需要转换成这样的主要是一段代码需要兼顾不同的形式。还有就是通过+号连接的要装换成单本书,具体操作如下:
1.分解+号,以下代码:
public String[] getbookname(String bookseries) { String[] bookname; String s = bookseries; if(s.indexOf("+")!=-1){ String[] subs = s.split("[+]"); String name = subs[0].substring(0, subs[0].length() - 1); // System.out.println(name); String last = subs[subs.length - 1]; // System.out.println(subs[0]); bookname = new String[subs.length]; bookname[0] = subs[0]; for (int i = 1; i < subs.length - 1; i++) { // System.out.println(name+subs[i]+","); bookname[i] = name + subs[i]; } // System.out.println(name+last.substring(0, last.indexOf("册"))); bookname[bookname.length - 1] = name + last.substring(0, last.indexOf("册")); for (String string : bookname) { System.out.print(string + ","); } }else{ bookname = new String[1]; bookname[0]=bookseries; } return bookname; }
如果是含有+号就分成一个书名数组。
不含有就数组就一个元素
然后首先根据单个单元格的换行符进行循环增加对象,每行就对+分割的元素进行循环增加。如下代码:
// 获取到数量,如果是大于1.现判断内件说明中是几行 int booknum = Integer.parseInt(((ro.getCell(5) + "").replace(".0", ""))); // 书名集合 String[] bname = ((ro.getCell(4) + "").trim()).split(" "); // 根据行数循环,一行只有一个订单 N行有N个子订单 for (int j = 0; j < bname.length; j++) { String[] bookname = this.getbookname(bname[j]); if (bname[j].indexOf("[+]") != -1) { System.out.println("本行为套装里多个单本!!!"); } for (int k = 0; k < bookname.length; k++) { StandardOrder sd = new StandardOrder(); System.out.println(bookname[k]+"书本为"); System.out.println("第" + (i + 1) + "行"); // 获取订单号 String ocode = (sdf2.format(new Date()) + "") + (i + "") + (j + "") +(k+ "")+""; // 子订单 if (j != 0) { ocode = ocode + "-" + j+""+k+""; } sd.setOrdercode(ocode); // 收货人也是买家 买家ID String person = ro.getCell(1) + ""; sd.setReceivername(person); // DecimalFormat df = new DecimalFormat("0"); // TODO try { // 收货人手机 DecimalFormat df = new DecimalFormat("#"); String mobile = ro.getCell(2) + ""; switch (ro.getCell(2).getCellType()) { case HSSFCell.CELL_TYPE_NUMERIC:// 数字 mobile = df.format(ro.getCell(2).getNumericCellValue()); break; case HSSFCell.CELL_TYPE_STRING:// 字符串 mobile = df.format(Double.parseDouble(ro.getCell(2).toString())); break; default: mobile = ro.getCell(2).toString(); break; } sd.setReceivermobile(mobile); } catch (Exception e) { session.setAttribute("error", (i + 1) + "行手机格式应为文本!"); return "exchange_failed"; } // 收货地址 String detail = ro.getCell(3) + " "; sd.setReceiverdetail(detail); // 省份 // String province = ro.getCell(13) + ""; // System.out.println(detail.indexOf(" ")); // sd.setReceiverprovince(province); // 条形码 String bookcode = ayd.querystr( "SELECT bookcode FROM anhuiyonger WHERE bookname='" + bookname[k].trim() + "'"); Anhuiyonger ayg = (Anhuiyonger) ayd.queryOneAnhuiyonger(bookcode); if (bookcode == null) { // TODO session.setAttribute("error", bookname[k] + "书籍未录入"); return "exchange_failed"; } else // 定价乘以折扣 条形码 sd.setProductcode(bookcode); System.out.println("book" + ayg); int number = booknum / bname.length; float providemoney = ayg.getSalemoney() * ayg.getDiscount() ; // 价格乘以数量 总价 sd.setProductmoney(providemoney); sd.setTotalmoney(providemoney * number+((float)7/(bookname.length*bname.length))); // 品名 sd.setProductname(bookname[k]); // 数量 sd.setProductnumber(number); // 状态 sd.setOrderstatus("买家已付款,等待卖家发货"); // 买家留言 sd.setBuyermessage(""); // 买家ID sd.setBuyerid(person); // 买家昵称 sd.setBuyernickanme(person); // System.out.println(province); sd.setSonordercode(ocode); sd.setProductspec(null); sd.setTransfee((float)7/(bookname.length*bname.length)); sd.setOnsale(0); sd.setZipcode(null); sd.setReceiverphone(null); sd.setCreatetime(new Date()); // 付款时间 sd.setPaytime(new Date()); sd.setTranscompany(null); sd.setTranscode(null); sd.setSendmessage(null); sd.setEmail(null); System.out.println("这个订单即将被添加:" + sd.toString()); so.add(sd); // System.out.println("内容" + so); 出错了 } } }