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

针对Java处理Excel数据时出现的类型匹配问题,如NumberFormatException错误,本文介绍了一种使用自定义ExcelDataHandlerDefaultImpl子类来转换特定字段类型的解决方案,确保了入学成绩和英语成绩等字段能正确从字符串转换为整数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我的问题

类型匹配问题

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;
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员石磊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值