今天写一个批量导入~
controller层
@RequestMapping(value = "/importLines",method=RequestMethod.POST)
public Response importLines(@RequestBody Map<String, Object> request){
Map map=serivce.importLines(request);
return Response.ok().setData(map);
}
service层
public Map importLines(Map<String, Object> request) {
HashMap<String, Object> map = new HashMap<>();
JSONObject req = new JSONObject(request);
Map<String, Object> dataMap = new HashMap<>();
//获取值
String base64Code = req.getString("base64CodeExcle");
List<com.fawjiefang.process.extension.pojo.MaterFacApplicFormLine> lines = new ArrayList<>();
try {
ByteArrayInputStream stream = null;
BASE64Decoder decoder = new BASE64Decoder();
byte[] bt = decoder.decodeBuffer(base64Code);
stream = new ByteArrayInputStream(bt);
Workbook wb = Workbook.getWorkbook(stream);
for (int index = 0; index < 1; index++) {
Sheet sheet = wb.getSheet(index);
for (int i = 1; i < sheet.getRows(); i++) {
//除去表头从第二行开始拿数据
String cellinfo = sheet.getCell(0, i).getContents();
String cellinfo1 = sheet.getCell(1, i).getContents();
String cellinfo2 = sheet.getCell(2, i).getContents();
String cellinfo3 = sheet.getCell(3, i).getContents();
String cellinfo4 = sheet.getCell(4, i).getContents();
String cellinfo5 = sheet.getCell(5, i).getContents();
String cellinfo6 = sheet.getCell(6, i).getContents();
com.fawjiefang.process.extension.pojo.MaterFacApplicFormLine line = new com.fawjiefang.process.extension.pojo.MaterFacApplicFormLine();
line.setShipArticleName(cellinfo);
line.setShipArticleModel(cellinfo1);
line.setMeteringUnit(cellinfo2);
line.setAmount(cellinfo3);
line.setIsBack(cellinfo4);
line.setIsValuable(cellinfo5);
String[] split = cellinfo6.split("/");
line.setExpectedReturndate(split[2]+"-"+split[1]+"-"+split[0]);
lines.add(line);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
map.put("lineList",lines);
return map;
}
这样就可以实现一个批量导入,现在有点硬编码 ,有时间了写个工具类分享给大家,拜拜~