用vue+springboot下载和上传excel文件

需求

  1. 前端发送下载请求
  2. 后端接收请求,从数据库拿出数据,写成excel格式,传给前端
  3. 前端拿到excel进行下载
  4. 在excel中增加数据
  5. 将新的excel上传给服务器

环境

  1. 前端vue+ElementUI
  2. 后端springboot+mybatisplus+mysql
  3. 后端生成excel用到org.apache.poi

下载

html
<el-button type="primary" @click="exportWord" icon="el-icon-download" plain>导出</el-button>
js
	exportWord () {
   
      this.$axios.post('/web/xxxxxx/export', {
   }, {
   
        responseType: 'blob'
      }).then(res => {
   
        let blob = new Blob([res.data], {
    type: 'application/ms-excel;charset=utf-8' });
        let downloadElement = document.createElement('a');
        let href = window.URL.createObjectURL(blob); //创建下载的链接
        downloadElement.href = href;
        downloadElement.download = 'forbidden-words.xls'; //下载后文件名
        document.body.appendChild(downloadElement);
        downloadElement.click(); //点击下载
        document.body.removeChild(downloadElement); //下载完成移除元素
        window.URL.revokeObjectURL(href); //释放掉blob对象
      })
    }
controller
	@PostMapping("/export")
    public void exportXXXXXXWords(HttpServletResponse response) {
   
        List<ForbiddenWord> forbiddenList;
        try {
   
        	// get your data
            wordList = wordService.getWords();
            // 设置excel第一行的标题
            String[] titleRow = new String[]{
   "单词", "级别"};
            List<String[]> data = new LinkedList<String[]>();
            data.add(0, titleRow);
            for (int i = 0; i < wordList.size(); i++) {
   
                Word word = wordList.get(i);
                data.add(new String[]{
   
                        word.getWord(),
                        word.getLevel().toString()
                });
            }
            Map<String, List
  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值