shaokel
2021-10-21 10:11
采纳率: 98.6%
浏览 19
已结题

Java中使用easypoi的导出的文件打开报错

上传到ftp显示的是xls后缀,下载后变成了xlsx后缀,导致打开excel报错
如果直接把xlsx后缀改为xls后缀,文件是可以正常打开的
下面是我导出的代码

@PostMapping("/spExport")
    public void spExport(@RequestBody Map<String, Object> reqMap) throws Exception {
        try {
            Map<String, Object> condition = modalController.getInterfaceAndParam(String.valueOf(reqMap.get("id")));
            //设置表头
            Map<String, Object> headerMap = new HashMap<>();//存放表头
            List<Map<String, Object>> columns = (List) condition.get("columns");
            columns.forEach(item -> {
                String headerMc = StringHelper.toString(item.get("dataIndex"));
                String title = StringHelper.toString(item.get("title"));
                headerMap.put(headerMc, title);
            });
            List<ExcelExportEntity> colList = new ArrayList<>();

            Iterator<Map.Entry<String, Object>> iterator = headerMap.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, Object> next = iterator.next();
                String key = next.getKey();
                ExcelExportEntity zg = new ExcelExportEntity(StringHelper.toString(next.getValue()), key);
                colList.add(zg);
            }

            Map<String, Object> params = (Map) condition.get("params");
            List<Map<String, Object>> ywYwhxJbxxes = hxService.dcspQuery(params);
            org.apache.poi.ss.usermodel.Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("表", "表"), colList, ywYwhxJbxxes);
            modalController.uploadExcelToFtp(workbook, "/dcsp/" + reqMap.get("sqsj"), StringHelper.toString(reqMap.get("id")));
        } catch (Exception e) {
            throw new ControllerException("导出失败", e);
        }
    }
 /**
       * 下载文件
       * @param {Object} data
       */
      downloadExcel(sqXx) {
        axios({
          url: '/dcsp/downloadExcel/' + sqXx.id,
          method: 'GET',
          responseType: 'blob',
          headers: {
            'Content-Type': 'application/json'
          }
        }).then(res => {
          const blob = new Blob([res])
          const fileName = '申请记录导出.xls'
          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)
        })
      },

  • 好问题 提建议
  • 收藏

1条回答 默认 最新

  • shaokel 2021-10-21 10:40
    已采纳

    看了一下源码,发现ExportParams可以修改下载格式,
    ExportParams类有一个构造方法

    public ExportParams(String title, String sheetName, ExcelType type) {
            this.title = title;
            this.sheetName = sheetName;
            this.type = type;
        }
    

    ExcelType类中有两个格式
    一个是HSSF,一个是XSSF
    分别对应xlx,xlsx后缀,
    所以只需要修改你所需要的后缀即可

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题