springBoot+Vue文件导入导出excel表

文件导出
一。后台代码:
1.查询所有对象并且把对象信息写入excel表中
2.设置头信息和编码格式
hsp.addHeader(“Content-Disposition”, “attachment;filename
=” + URLEncoder.encode(“项目信息.xls”,“UTF-8”));
hsp.setContentType(“applicatoin/octet-stream;charset=utf-8”);
hsp.setHeader(“Access-Control-Expose-Headers”, “Content-Disposition”);
3.创建输入流读取excel表
InputStream is = new FileInputStream(path + “static/project.xls”);// 文件的存放路径
4.创建输出流
OutputStream os = hsp.getOutputStream();
二.Vue部分:
expor () {
const _this = this
this.KaTeX parse error: Expected 'EOF', got '}' at position 287: …) }) }̲ 请求要设置相应格式应…message.success(resultData.resultValue)
} else {
this.KaTeX parse error: Expected 'EOF', got '}' at position 35: …成功') }̲ retu…message.error(err)
return
}
}
} catch (err) {
// 该异常为无法将字符串转为json,说明返回的数据是一个流文件
// 不需要处理该异常,只需要捕获即刻
}
if (‘download’ in document.createElement(‘a’)) {
// 这里res.data是返回的blob对象
const blob = new Blob([res.data])
const elink = document.createElement(‘a’)
elink.download = fileName
elink.style.display = ‘none’
elink.href = URL.createObjectURL(blob)
document.body.appendChild(elink)
elink.click()
URL.revokeObjectURL(elink.href) // 释放URL 对象
document.body.removeChild(elink)
} else {
// 兼容ie无法下载问题 for ie 10 and later
navigator.msSaveBlob(blob, fileName)
}
}

// 将blob对象以文本的方式读出,读出完成后将会执行 onload 方法
reader.readAsText(blob)
}
上传
一.controller层
@RequestMapping("/upload")
public MsgVo upload(@RequestParam(“excelFile”) MultipartFile excelFile)
{
// File pdfFile = new File(“D://test.pdf”);
// FileInputStream fileInputStream = new FileInputStream(pdfFile);
// MultipartFile multipartFile = new MockMultipartFile(pdfFile.getName(), pdfFile.getName(),
// ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
MsgVo msgvo = new MsgVo();

 try
 {
	 ArrayList<Project> projects = Until.excelToShopIdList(excelFile.getInputStream());
	 projectService.saveAll(projects);
} catch (IOException e)
 {
	// TODO Auto-generated catch block
	e.printStackTrace();
	msgvo.setCode(1);
}
msgvo.setCode(0);
return msgvo;

}
二.excel数据装实体类方法
public static ArrayList excelToShopIdList(InputStream inputStream)
{

    Workbook workbook = null;
    ArrayList<Project> projects = new ArrayList<Project>();
    
        try 
        {
			workbook = WorkbookFactory.create(inputStream);
			  Sheet sheet = workbook.getSheetAt(0);
	            //总行数
	            int rowLength = sheet.getLastRowNum() + 1;
	            //工作表的列
	           for (int i = 1 ; i < rowLength;i++)
	           {
	        	   Project project = new Project();
	        	   Row  row = sheet.getRow(i);
	        	   project.setId(row.getCell(0).getStringCellValue());
	        	   project.setCreater(row.getCell(1).getStringCellValue());
	        	   project.setProjectName(row.getCell(2).getStringCellValue());
	        	   project.setValid(new Double(row.getCell(3).getNumericCellValue()).intValue());
	        	  
	        	  // project.setValid(Integer.parseInt(row.getCell(3).getStringCellValue()));
	        	   projects.add(project);	  
	           }
	           inputStream.close();
		} catch (EncryptedDocumentException e)
        {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) 
        {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}    
        //工作表对
	return projects;
}

三. 最后将返回的实例类集合调用数据库存储方法
vue部分

选取文件
<el-button @click=“muban” size=“small” type=“primary”>下载模板

uploadUrl: function () {
return (
‘http://localhost:8081/test/upload’
)
},
uploadSuccess (response, file, fileList) {
const _this = this
// eslint-disable-next-line eqeqeq
if (response.code == 0) {
alert(‘文件导入成功’)
_this.reload()
} else {
alert(‘文件导入失败’)
}
},
uploadFalse (response, file, fileList) {
alert(‘文件上传失败!’)
},
// 上传前对文件的大小的判断
beforeAvatarUpload (file) {
const extension = file.name.split(’.’)[1] === ‘xls’
const extension2 = file.name.split(’.’)[1] === ‘xlsx’
const extension3 = file.name.split(’.’)[1] === ‘doc’
const extension4 = file.name.split(’.’)[1] === ‘docx’
const isLt2M = file.size / 1024 / 1024 < 10
if (!extension && !extension2 && !extension3 && !extension4) {
alert(‘上传模板只能是 xls、xlsx、doc、docx 格式!’)
}
if (!isLt2M) {
console.log(‘上传模板大小不能超过 10MB!’)
}
return extension || extension2 || extension3 || (extension4 && isLt2M)
},
submitUpload () {
if (this.businessType != null) {
// 触发组件的action
this.$refs.upload.submit()
}
if (this.businessType == null) {
this.businessType = ‘businessType不能为空’
}
}
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值