问题描述
字段均为字符串类型,其中两个字段解析不到数据,一直为空
字段类型:
核心代码
@Override
public String importFile(MultipartFile file, String nickName, Long userId) {
List<PatentInvention> inventions = FileUtil.importExcel(file, 0, 1, PatentInvention.class);
if (!CollectionUtils.isEmpty(inventions)) {
for (PatentInvention invention : inventions) {
if(StringUtils.equals("发明专利",invention.getTypeName())){
invention.setType(0);
}else if(StringUtils.equals("实用新型",invention.getTypeName())){
invention.setType(1);
}if(StringUtils.equals("软著",invention.getTypeName())){
invention.setType(2);
}
invention.setCreateTime(new Date());
invention.setCreateBy(nickName);
patentInventionMapper.insert(invention);
}
} else {
return "导出失败,参数为空";
}
return null;
}
/**
* 导入
* @param file 需要导入的文件
* @param titleRows 从第一行开始解析
* @param headerRows 头部行下标
* @param pojoClass 转化为对应的实体类
* @param <T>
* @return 返回解析后的实体类对象集合
*/
public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass){
if (file == null){
return null;
}
ImportParams params = new ImportParams();
params.setTitleRows(titleRows);
params.setHeadRows(headerRows);
List<T> list = null;
try {
list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);
}catch (NoSuchElementException e){
//throw new NotFoundException("excel文件不能为空");
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
// throw new NotFoundException(e.getMessage());
}
return list;
}
结果(数据不全)
问题解决
1.字段中文括号问题
2.excel中日期识别不到问题
接受字段为字符串类型,接收不到其中日期,部分能接收到
正常的日期展示
提示有空格的字符串日期
采用笨办法,将日期全部增加空格,调整格式,进行导入