之前做产品清单遇到的。代码如下:
@Autowired
FrameAgreementProListMapper mapper;
//导入报价产品
@RequestMapping("/importData.do")
@ResponseBody
public String importData(FrameAgreementProListModel model ,HttpServletRequest request ,HttpServletResponse response)throws Exception{
//该model中只有 frameId 和file .前端传的
String filename = model.getFile().getOriginalFileName();
String fileType = filename.substring(filename.lastIndexOf(".")); //====.xls
List<String> errorList = new LinkedList<>();//因为返回String类型,所以这里就用了一个errorList,装错误信息返回。
//获取原产品清单列表
model.setLoginName(SystemUtils.getUserModel().getLoginName());
List<FrameAgreementProListModel> oldList = mapper.selectFrameProList(model);
if(fileType.equals(".xls")){
List<FrameAgreementProListModel> importList = readExcel(model.getFile().getInputStream(), model.getFrameId(),errorList, mapper);
if(errorList.isEmpty()){
//读取到Excel数据 | 未读取到Excel数据
if ( importList != null ){
//导入的Excel列表数据物料号 与 原列表数据物料号 一样,则进行更新
for( FrameAgreementProListModel importModel : importList ){
if( oldList != null ){ //原来有产品清单,进行物料号比对,选择是更新还是添加
//我需要在这里定义一个布尔变量,true则更新,false则新增
//我不能在内层进行更新操作或者添加,因为那样会拿第一个importModel依次比对oldModel进行操作
//然后第二个importModel,第三个。。。会有很多重复的。
boolean update = false;
for( FrameAgreementProListModel oldModel : oldList ){
if(importModel.getPartNum() .equals(oldModel.getPartNum())){
importModel.setRowId(oldModel.getRowId());//设置Id
update = true;
break;
}
}
//在外层for循环进行 更新。操作。因为我只关心importModel的 物料号是否与之前的一样
if(update){
service.update(importModel);
}else{
service.addAndGetRowId(importModel);
}
}else{ //原来没有产品清单,则直接添加。
super.asynEdit(importModel,request,response);
//我不能在这里return,不然添加了一个就结束了
//需要在for循环之后,所有的数据都遍历完,在return。
}
}
}else{
errorList.add("无可导入的数据");
return StringUtils.resultFailToJson(errorList);//说明没有数据导入。
}
//for循环外,只要没有出错,就必然会走到这里来,所以在这里return
return StringUtils.resultSuccessToJson("导入成功!");
}
return StringUtils.resultFailToJson(errorList); //前面的代码有问题,就返回错误信息
}else{
return StringUtils.resultFailToJson("您上传的文件格式有误,请上传xls格式的文件!");
}
}
//读取Excel内容,到model -----mapper是我自己加的,因为要用到。查数据
public static List<FrameAgreementProListModel> readExcel(InputStream is,String frameId, List<String> errorList, FrameAgreementProListMapper mapper) throws Exception{
//创建一个list 用来存储读取的内容
List<FrameAgreementProListModel> list = new LinkedList<FrameAgreementProListModel>();
//获取Excel 文件对象
try{ Workbook rwb = Workbook.getWorkbook(is);
//获取文件的指定工作表 默认的第一个
Sheet sheet = rwb.getSheet(0);
Cell cell1 = ExcelUtils.getCellByValue(sheet,"物料号");
Cell cell2 = ExcelUtils.getCellByValue(sheet,"销售价格");
Cell cell3 = ExcelUtils.getCellByValue(sheet,"实际质保期");
Cell cell4 = ExcelUtils.getCellByValue(sheet,"服务费(百分比)");
if(cell1 == null || cell2 == null || cell3 == null || cell4 == null ){
errorList.add("模板格式不正确");
return list;
}
int int_i = cell1.getRow() + 1;
for(int i = ini_i ; ; i++){
try{ StringBuffer error = new StringBuffer();
//物料号
Cell val1 = sheet.getCell(cell1.getColumn(),i);
if(StringUtils.isBlank(val1.getContents())){
break;
}
//销售价格
Cell val2 = sheet.getCell(cell2.getColumn(),i);
if(!"".equals(val2.getContents())){
Integer num = Integer.parseInt(val2.getContents());
if( num < 0 ) error.append("[销售价格]"必须为正数) ;
}else{
error.append("[销售价格]必填");
}
//实际质保期
Cell val3 = sheet.getCell(cell3.getColumn(),i);
if(!"".equals(val3.getContents())){
//判断当前数字是否为正数0-9
try{
Double.parseDoubel(val3.getContents());
}catch(Exception e){ //如果不是正数,就不能解析成Double?
if(error.length() > 0) error.append(",");
error.append("[实际质保期]必须为正数");
}
}else{
error.append("[实际质保期]必填");
}
//服务费百分比
Cell val4 = sheet.getCell(cell4.getColumn(),i);
if(!"".equals(val4.getContents())){
//判断当前数字是否为正数0-9
try{
Double.parseDoubel(val4.getContents());
}catch(Exception e){ //如果不是正数,就不能解析成Double?
if(error.length() > 0) error.append(",");
error.append("[服务费百分比]必须为正数");
}
}else{
error.append("[服务费百分比]必填");
}
if( error.length() > 0 ){
errorList.add(String.format("行号【%d】:%s",i+1,error.toString()));
}
//读取到Model中
FrameAgreementProListModel model = new FrameAgreementProListModel();
model.setPartNum( val1.getContents() ); //物料号
model.setSalesPrice(val2.getContents()); //销售价格
model.setActualWP(val3.getContents()); //实际质保期
model.setServiceCost(val4.getContents()); //服务费百分比
model.setFrameId(frameId); //关联框架。
//根据物料号,查询产品,拿到产品的RowId。设置到当前Model的产品IdProductId.
List<FrameAgreementProListModel > proList = mapper.selectProList(model);
model.setProductId(SystemUtils.getUserModel().getRowId());
list.add(model);
}catch(Exception e){
e.printStackTrace();
continue;
}
}//for循环
return list;
}finally{ //第一个try
rwb.close();
}
}