用easypoi导入excel,在win可以,部署到linux后就报null错误

9 篇文章 1 订阅

一、描述

1.前端

采用的是element组件

<el-upload
	class="upload-file"
	drag
	ref="uploadExcel"
	:action="importUrl"
	:limit="1"
	name="importMatFile"
	:auto-upload="false"
	accept=".xlsx"
	:before-upload="beforeUploadFile"
	:on-change="fileChange"
	:on-exceed="exceedFile"
	:on-success="handleSuccess"
	:on-error="handleError"
	:file-list="excelFileList"
	>
	<i class="el-icon-upload"></i>
	<div class="el-upload__text">
	  将文件拖到此处,或<em>点击选择Excel文件</em>
	</div>
</el-upload>

2.服务器端

2.1 controller

/**
 * 导入数据
 * @param file
 * @return
 * @throws
 */
@RequestMapping(value = "/import", method = RequestMethod.POST)
public R importExcel(@RequestParam("importMatFile") MultipartFile file) throws IOException {
    List<ImportMatInfoReqVo> list = ExcelUtils.importExcel(file, ImportMatInfoReqVo.class);
    int i = materialService.insertByBatch(list);
    if (i != 0) {
        return R.ok("导入成功");
    } else {
        return R.error("导入失败");
    }
}

一开始以为是后端没有接收到文件,后来发现是接收到的。问题出在处理文件这个方法上:ExcelUtils.importExcel(file, ImportMatInfoReqVo.class)

这个类用来处理导入的excel数据,将每一行数据转换成一个个bean。

进入这个类,发现其中导入的方法中,有一个参数设置存在问题。params.setNeedSave(true);这个属性是设置要把导入的文件进行保存,默认该参数值是false。我注释掉就可以正常使用了。至于为什么不行,可能是保存路径错误,具体的我暂时没有尝试。

/**
 * excel 导入
 *
 * @param inputStream 文件输入流
 * @param titleRows   表格内数据标题行
 * @param headerRows  表头行
 * @param pojoClass   pojo类型
 * @param <T>
 * @return
 */
public static <T> List<T> importExcel(InputStream inputStream, Integer titleRows, Integer headerRows, Class<T> pojoClass) throws IOException {
    if (inputStream == null) {
        return null;
    }
    ImportParams params = new ImportParams();
    params.setTitleRows(titleRows);
    params.setHeadRows(headerRows);
    params.setSaveUrl("/excel/");
//        params.setNeedSave(true);
    try {
        return ExcelImportUtil.importExcel(inputStream, pojoClass, params);
    } catch (NoSuchElementException e) {
        throw new IOException("excel文件不能为空");
    } catch (Exception e) {
        throw new IOException(e.getMessage());
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值