第一次接触Excel导出功能,写的有点费劲,把代码贴出来希望能帮到大家
后端:springboot+springcloud+mybatis
前端:vue+iview
数据库:oracle
先上vue
uploadLoading: false,
file: null,
//导入
handleFormatError(file){
this.$Notice.warning({
title: '文件格式不正确',
desc: '文件 ' + file.name + ' 格式不正确,请上传.xls,.xlsx文件。'
})
},
handleSuccess(res,file){
this.$Message.success("数据导入成功!")
//提示完渲染页面
this.getTreeData(1)
},
handleError(error,file){
this.$Message.error("数据导入失败!")
},
接下来我们写后台
导入jar包(不知道是否都能用到,引就vans了,版本可能对代码有影响)
org.apache.poi
poi-ooxml
3.9
org.apache.poi
poi
3.9
org.dom4j
dom4j
2.1.1
org.apache.poi
poi-ooxml-schemas
3.9
org.apache.poi
poi-scratchpad
3.9
commons-fileupload
commons-fileupload
1.3.1
commons-io
commons-io
2.4
org.apache.xmlbeans
xmlbeans
2.3.0
org.apache.poi
poi-examples
3.9
org.apache.poi
poi-excelant
3.9
controller
/**
* 导入Excel
* @param file
* @throws Exception
*/
@ApiOperation(value="Excel导入", notes="上传Excel表格插入数据库")
@RequestMapping(value = "/upload",method = RequestMethod.POST)
public void upload(@RequestParam("file") MultipartFile file) throws Exception {
stationService.batchImport(file);
}
service
/**
* 导入Excel
* @param file
* @throws Exception
*/
void batchImport(MultipartFile file) throws Exception;
serviceimpl
/**
* 导入Excel
* @param file
* @throws Exception
*/
@Transactional(readOnly = false,rollbackFor = Exception.class)
@Override
public void batchImport(MultipartFile file)throws Exception {
List ss = new ArrayList<>();
InputStream is = file.getInputStream();
Workbook workbook = WorkbookFactory.create(is);
//确定版本
boolean isExcel2003 = file.getOriginalFilename().endsWith("xls")?true:false;
List
dao
/**
* Excel导入
* @param s
*/
void addStationExcel(Station s);
**.xml
insert into 表名
station_id,
station_name,
contacts,
into_use_time,
address,
#{stationId,jdbcType=VARCHAR},
#{stationName,jdbcType=VARCHAR},
#{contacts,jdbcType=VARCHAR},
to_date(#{intoUseTime,jdbcType=DATE},'yyyy-MM-dd'),
#{address,jdbcType=VARCHAR},