Java导出Excel,提示格式与文件扩展名不一致

后台代码,这是基础方法,还要根据使用调用


   public static void exportExcel(HttpServletResponse response, String sheetName, String fileName, String[] header, List<String[]> content)
           throws IOException {

      // 清空response
      response.reset();
      // 设置response的Header
      response.setHeader("Content-disposition",
              "attachment;filename=" + new String(fileName.getBytes("gbk"), "iso8859-1") + ".xlsx");
      response.setContentType("application/vnd.ms-excel;charset=gb2312");
      Sheet sheet = new Sheet(1, 0);
      sheet.setSheetName(sheetName);
      // 设置自适应宽度
      sheet.setAutoWidth(Boolean.TRUE);
      // 设置表头
      List<List<String>> list = new ArrayList<>();
      for (int i = 0; i < header.length; i++) {
         list.add(Collections.singletonList(header[i]));
      }
      sheet.setHead(list);
      OutputStream outputStream = null;
      ExcelWriter writer = null;
      try {
         outputStream = new BufferedOutputStream(response.getOutputStream());
         writer = EasyExcelFactory.getWriter(outputStream);
         List<List<Object>> data = new ArrayList<List<Object>>();
         for (int i = 0; i < content.size(); i++) {
            List<Object> item = new ArrayList<Object>();
            for (int j = 0; j < content.get(i).length; j++) {
               item.add(content.get(i)[j]);
            }
            data.add(item);
         }
         writer.write1(data, sheet);
         outputStream.flush();
         log.info("excel文件导出成功!");
      } catch (Exception e) {
         log.error("excel文件导出失败, 失败原因:{}", e);
      } finally {
         try {
            if (writer != null) {
               writer.finish();
            }
            if (outputStream != null) {
               outputStream.close();
            }
         } catch (IOException e) {
            log.error("excel文件导出失败, 失败原因:{}", e);
         }
      }

   }

前端请求:

  <button  type="primary" icon="ios-cloud-download"  @click="download_file"
                                 style="    color: rgb(255, 255, 255); background-color: rgb(45, 140, 240);border: 1px solid transparent;   display: inline-block; margin-bottom: 0px;    font-weight: 400;    text-align: center;    vertical-align: middle;    touch-action: manipulation;    cursor: pointer;    background-image: none;    white-space: nowrap;    line-height: 1.5;    user-select: none;    height: 32px;    line-height: 32px;    padding: 0px 15px;  font-size: 14px;border-radius: 4px;"
                        >导出数据</button>
download_file(){
                const url = this.exportUrl+"?orderCode="+this.orderCode+"&productName="+this.productName+"&state"+this.state+"&productCode="+this.productCode+"&mobile="+this.mobile+"&commitTime="+this.commitTime+"&size=10000&page=1";
                const token = store.getters.access_token
                var xhr = new XMLHttpRequest();
                xhr.open('GET', url, true);
                xhr.setRequestHeader("Authorization", 'Bearer ' + token);
                xhr.responseType = 'blob';
                xhr.onload = function (e) {
                    if (this.status == 200) {
                        var blob = e.currentTarget.response;
                        var filename = `productOrderList.xls`;//如123.xls
                        var a = document.createElement('a');
                        var url = URL.createObjectURL(blob);
                        a.href = url;
                        a.download=filename;
                        a.click();
                        window.URL.revokeObjectURL(url);
                    }
                };
                xhr.send();

            },

错误在于,,请求的数据时的名字为  var filename = `productOrderList.xls`;//如123.xls 是.xls 而java 中的response.setHeader("Content-disposition",
              "attachment;filename=" + new String(fileName.getBytes("gbk"), "iso8859-1") + ".xlsx");为.xlsx 格式不统一,,我的问题就处在这,,,两个后缀统一就没问题了

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值