From 5cdd67f1f4f8e5bd300f407d14e0189ca5f48e99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=91=E9=A3=9E=E6=89=AC?= <15678871232@qq.com> Date: Sun, 25 Sep 2022 06:23:21 +0000 Subject: [PATCH] =?UTF-8?q?=E5=AF=BC=E5=87=BA=E5=8A=9F=E8=83=BD=EF=BC=8C?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=94=B1=E5=90=8E=E5=8F=B0=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=90=8D=EF=BC=8C=E5=89=8D=E7=AB=AF=E5=8F=AF?= =?UTF-8?q?=E4=BB=A5=E6=8C=87=E5=AE=9A=E6=88=96=E5=90=8E=E5=8F=B0=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E6=96=87=E4=BB=B6=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 云飞扬 <15678871232@qq.com> --- .../java/com/ruoyi/common/core/utils/poi/ExcelUtil.java | 7 +++++++ .../src/main/resources/vm/java/controller.java.vm | 4 ++-- .../ruoyi-gen/src/main/resources/vm/vue/index.vue.vm | 2 +- .../ruoyi-gen/src/main/resources/vm/vue/v3/index.vue.vm | 2 +- ruoyi-ui/src/utils/request.js | 8 ++++++++ 5 files changed, 19 insertions(+), 4 deletions(-) diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelUtil.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelUtil.java index 681f6cd7..051e03d9 100644 --- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelUtil.java +++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelUtil.java @@ -2,10 +2,12 @@ package com.ruoyi.common.core.utils.poi; import java.io.IOException; import java.io.InputStream; +import java.io.UnsupportedEncodingException; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.ParameterizedType; import java.math.BigDecimal; +import java.net.URLEncoder; import java.text.DecimalFormat; import java.time.LocalDate; import java.time.LocalDateTime; @@ -455,6 +457,11 @@ public class ExcelUtil public void exportExcel(HttpServletResponse response, List list, String sheetName, String title) { response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); + try { + response.addHeader("Content-Disposition", "attachment;FileName=" + URLEncoder.encode(title,"utf-8")); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); + } response.setCharacterEncoding("utf-8"); this.init(list, sheetName, title, Type.EXPORT); exportExcel(response); diff --git a/ruoyi-modules/ruoyi-gen/src/main/resources/vm/java/controller.java.vm b/ruoyi-modules/ruoyi-gen/src/main/resources/vm/java/controller.java.vm index e466592d..ee9512ed 100644 --- a/ruoyi-modules/ruoyi-gen/src/main/resources/vm/java/controller.java.vm +++ b/ruoyi-modules/ruoyi-gen/src/main/resources/vm/java/controller.java.vm @@ -63,12 +63,12 @@ public class ${ClassName}Controller extends BaseController */ @RequiresPermissions("${permissionPrefix}:export") @Log(title = "${functionName}", businessType = BusinessType.EXPORT) - @PostMapping("/export") + @PostMapping(value = "/export", produces = "application/octet-stream") public void export(HttpServletResponse response, ${ClassName} ${className}) { List<${ClassName}> list = ${className}Service.select${ClassName}List(${className}); ExcelUtil<${ClassName}> util = new ExcelUtil<${ClassName}>(${ClassName}.class); - util.exportExcel(response, list, "${functionName}数据"); + util.exportExcel(response, list, "${functionName}数据", "${functionName}数据"); } /** diff --git a/ruoyi-modules/ruoyi-gen/src/main/resources/vm/vue/index.vue.vm b/ruoyi-modules/ruoyi-gen/src/main/resources/vm/vue/index.vue.vm index 8daa05ec..45f30aec 100644 --- a/ruoyi-modules/ruoyi-gen/src/main/resources/vm/vue/index.vue.vm +++ b/ruoyi-modules/ruoyi-gen/src/main/resources/vm/vue/index.vue.vm @@ -591,7 +591,7 @@ export default { handleExport() { this.download('${moduleName}/${businessName}/export', { ...this.queryParams - }, `${businessName}_#[[${new Date().getTime()}]]#.xlsx`) + }) } } }; diff --git a/ruoyi-modules/ruoyi-gen/src/main/resources/vm/vue/v3/index.vue.vm b/ruoyi-modules/ruoyi-gen/src/main/resources/vm/vue/v3/index.vue.vm index f66cc3b8..59fbdd7b 100644 --- a/ruoyi-modules/ruoyi-gen/src/main/resources/vm/vue/v3/index.vue.vm +++ b/ruoyi-modules/ruoyi-gen/src/main/resources/vm/vue/v3/index.vue.vm @@ -589,7 +589,7 @@ function handle${subClassName}SelectionChange(selection) { function handleExport() { proxy.download('${moduleName}/${businessName}/export', { ...queryParams.value - }, `${businessName}_#[[${new Date().getTime()}]]#.xlsx`) + }) } getList(); diff --git a/ruoyi-ui/src/utils/request.js b/ruoyi-ui/src/utils/request.js index 4a0caa57..90e23268 100644 --- a/ruoyi-ui/src/utils/request.js +++ b/ruoyi-ui/src/utils/request.js @@ -73,6 +73,12 @@ service.interceptors.response.use(res => { const msg = errorCode[code] || res.data.msg || errorCode['default'] // 二进制数据则直接返回 if(res.request.responseType === 'blob' || res.request.responseType === 'arraybuffer'){ + // 响应返回的内联显示的标题 + let disposition = res.headers['content-disposition']; + // 根据标志,不区分大小写,获取文件名称 + let filename = disposition.split(/filename=/i).pop(); + // 文件名称解码 + res.data.filename = decodeURI(filename); return res.data } if (code === 401) { @@ -141,6 +147,8 @@ export function download(url, params, filename, config) { const isLogin = await blobValidate(data); if (isLogin) { const blob = new Blob([data]) + // 如果方法参数的文件名称为空,则使用响应请求返回的文件名称 + filename = filename || data.filename saveAs(blob, filename) } else { const resText = await data.text();