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

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

public static void exportExcel(HttpServletResponse response, String sheetName, String fileName, String[] header, List 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 = 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> data = https://www.it610.com/article/new ArrayList>(); for (int i = 0; i < content.size(); i++) { List item = new ArrayList(); 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); } }}
【java|Java导出Excel,提示格式与文件扩展名不一致】前端请求:
导出数据

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 = https://www.it610.com/article/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 格式不统一,,我的问题就处在这,,,两个后缀统一就没问题了

    推荐阅读