请各大网友尊重本人原创知识分享,谨记本人博客:南国以南i、
前言:由于Excel版本的关系,文件扩展名分xls和xlsx,在以往的经验都是使用HSSFWorkbook和XSSFWorkbook两种工作对象来分别处理兼容性问题。具体的方式就是先判断文件的类型,然后根据文件扩展名来选择方法。一系列逻辑判断下来代码过于冗余。
于是乎本文将解决这种代码冗余和版本兼容性问题!
功能需求:
一、前端上传Excel表格,后台获取数据
二、后台能够解析传入xls和xlsx两种Excel文件
三、获得解析数据,进行入库操作
程序如下:
1、在SpringBootpom.xml文件中添加poi的Maven相关依赖
<!--poi依赖-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.9</version>
</dependency>
2、前台发起请求,往后台发送Excel文件
注:在Controller中进行处理使用MultipartFile中getInputStream() 返回InputStream读取文件内容, 我们要解决兼容性问题这里我们使用WorkbookFactory对象的create方法,获取文件流创建Workbook工作对象,调用Service层方法进行逻辑处理。
@PostMapping("/fileUpload")
public BasicResult fileUpload(@RequestParam("file") final MultipartFile meFile) {
try {
Workbook workbook = WorkbookFactory.create(meFile.getInputStream());
return paperService.fileUpload(workbook, sort, token);
} catch (InvalidFormatException | IOException e) {
log.error("无效文件类型", e);
}
return BasicResult.failure("导入文件失败");
}
3、通过传入的Workbook工作对象获取到sheet以及行数以及每个cell,然后分别给数据库中对应的对象的属性赋值,然后插入到数据库中并返回执行结果。
/**
* 导入问卷信息
*
* @param workbook 工作对象
* @return 结果集
*/
public BasicResult fileUpload(final Workbook workbook) {
Sheet sheet = workbook.getSheetAt(0); //获取工作簿
//获取行数
int rows = sheet.getPhysicalNumberOfRows();
List<ChildQuestion2> childQuestion2List = new ArrayList<>();
for (int currentRow = 1; currentRow < rows; currentRow++) {
//子问题对象
ChildQuestion2 childQuestion2 = new ChildQuestion2();
//子问题对象:主键id
childQuestion2.setChildId(UUID.randomUUID().toString());
//子问题对象:排序权重
childQuestion2.setSeq(currentRow);//根据插入顺序
//子问题对象:问题名称
if (!StringUtils.isEmpty(sheet.getRow(currentRow).getCell(1).getStringCellValue())) {
sheet.getRow(currentRow).getCell(1).setCellType(Cell.CELL_TYPE_STRING);
String childName = sheet.getRow(currentRow).getCell(1).getStringCellValue();
childQuestion2.setChildName(childName);
}
//子问题对象:条件
if (!StringUtils.isEmpty(sheet.getRow(currentRow).getCell(2).getStringCellValue())) {
sheet.getRow(currentRow).getCell(2).setCellType(Cell.CELL_TYPE_STRING);
String cellValue = sheet.getRow(currentRow).getCell(2).getStringCellValue();
childQuestion2.setCondition(cellValue);
}
//子问题对象:主话术
if (!StringUtils.isEmpty(sheet.getRow(currentRow).getCell(3).getStringCellValue())) {
sheet.getRow(currentRow).getCell(3).setCellType(Cell.CELL_TYPE_STRING);
String subjectivity = sheet.getRow(currentRow).getCell(3).getStringCellValue();
childQuestion2.setSubjectivity(subjectivity);
}
//把对象保存到List中
childQuestion2List.add(childQuestion2);
}
//保存数据入库操作
paperMapper.addChildQuestion(childQuestion2List);
return BasicResult.success("导入文件成功");
}
}
4、最后一步操作,使用Mybatis书写入库SQL语句,此处使用Oracle数据库,获得传入的List集合进行批量插入操作
<!--添加子问题-->
<insert id="addChildQuestion" parameterType="com.poje.testweb.web.Reporing.entity.ChildQuestion2">
insert all
<foreach collection="list" item="item" index="index" separator="">
into T_BUSINESS_CHILDQUESTION(CHILDID, CHILDNAME, CONDITION, SUBJECTIVITY, IDENTIFIER, SEQ, PARENTID)
values
(#{item.childId},#{item.childName},#{item.condition},#{item.subjectivity},#{item.identifier},#{item.seq},#{item.parentId})
</foreach>
select 1 from dual
</insert>
个人总结:
我是南国以南i记录点滴每天成长一点点,学习是永无止境的!转载请附原文链接!!!