jeasypoi导入excel,数字列导入失败解决办法

我的问题

类型匹配问题

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;
    }
有理想的coder CSDN认证博客专家 全栈工程师 终生学习者 懂营销的程序猿
坐标郑州,从业经验10余年,擅长javaweb技术栈,实战经验丰富。目前感兴趣方向:打造副业,网络安全,高可用高并发,架构,营销。更多干货请关注微信公众号lovepythoncn,关注我交个朋友!
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 代码科技 设计师:Amelia_0503 返回首页