我的问题
类型匹配问题
java.lang.NumberFormatException: For input string: "439.0" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:580) at java.lang.Integer.valueOf(Integer.java:766)
实体
public class StudentInfoEntity implements java.io.Serializable { /**入学成绩*/ @Excel(name="入学成绩",width=15,type=4) private Integer admissionScore; /**英语成绩*/ @Excel(name="英语成绩",width=15,type=4) private Integer englishScore; }
excel数据
解决办法使用ExcelDataHandlerDefaultImpl
进行类型转换
package com.util; import org.apache.commons.lang3.ArrayUtils; import org.jeecgframework.poi.handler.impl.ExcelDataHandlerDefaultImpl; import java.util.Map; public class ExcelHandler extends ExcelDataHandlerDefaultImpl { @Override public Object importHandler(Object obj, String name, Object value) { if(ArrayUtils.contains( this.getNeedHandlerFields(),name)){ Double d = Double.valueOf(value.toString()); return d.intValue(); }else{ return super.importHandler(obj, name, value); } } }
controller导入
@RequestMapping(params = "importExcel", method = RequestMethod.POST) @ResponseBody public AjaxJson importExcel(HttpServletRequest request, HttpServletResponse response) { AjaxJson j = new AjaxJson(); Integer year = Integer.valueOf(request.getParameter("year")); MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; Map<String, MultipartFile> fileMap = multipartRequest.getFileMap(); for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) { MultipartFile file = entity.getValue();// 获取上传文件对象 ImportParams params = new ImportParams(); ExcelHandler excelHandler = new ExcelHandler(); //设置要转换的字段 excelHandler.setNeedHandlerFields(new String[] { "入学成绩","英语成绩" }); params.setTitleRows(0); params.setHeadRows(1); params.setDataHanlder(excelHandler); params.setNeedSave(true); try { List<StudentInfoEntity> listJeecgDemoExcelEntitys = ExcelImportUtil.importExcel(file.getInputStream(),StudentInfoEntity.class,params); for (StudentInfoEntity studentInfoEntity : listJeecgDemoExcelEntitys) { studentInfoService.save(studentInfoEntity); studentInfoEntity.setYear(year); } j.setMsg("文件导入成功!"); } catch (Exception e) { e.printStackTrace(); j.setMsg("文件导入失败!"); logger.error(e.getMessage()); }finally{ try { file.getInputStream().close(); } catch (IOException e) { e.printStackTrace(); } } } return j; }