导出功能,增加由后台配置文件名,前端可以指定或后台返回文件名
Signed-off-by: 云飞扬 <15678871232@qq.com>
This commit is contained in:
parent
e03cebac57
commit
5cdd67f1f4
|
|
@ -2,10 +2,12 @@ package com.ruoyi.common.core.utils.poi;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.lang.reflect.ParameterizedType;
|
import java.lang.reflect.ParameterizedType;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
import java.net.URLEncoder;
|
||||||
import java.text.DecimalFormat;
|
import java.text.DecimalFormat;
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
|
@ -455,6 +457,11 @@ public class ExcelUtil<T>
|
||||||
public void exportExcel(HttpServletResponse response, List<T> list, String sheetName, String title)
|
public void exportExcel(HttpServletResponse response, List<T> list, String sheetName, String title)
|
||||||
{
|
{
|
||||||
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
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");
|
response.setCharacterEncoding("utf-8");
|
||||||
this.init(list, sheetName, title, Type.EXPORT);
|
this.init(list, sheetName, title, Type.EXPORT);
|
||||||
exportExcel(response);
|
exportExcel(response);
|
||||||
|
|
|
||||||
|
|
@ -63,12 +63,12 @@ public class ${ClassName}Controller extends BaseController
|
||||||
*/
|
*/
|
||||||
@RequiresPermissions("${permissionPrefix}:export")
|
@RequiresPermissions("${permissionPrefix}:export")
|
||||||
@Log(title = "${functionName}", businessType = BusinessType.EXPORT)
|
@Log(title = "${functionName}", businessType = BusinessType.EXPORT)
|
||||||
@PostMapping("/export")
|
@PostMapping(value = "/export", produces = "application/octet-stream")
|
||||||
public void export(HttpServletResponse response, ${ClassName} ${className})
|
public void export(HttpServletResponse response, ${ClassName} ${className})
|
||||||
{
|
{
|
||||||
List<${ClassName}> list = ${className}Service.select${ClassName}List(${className});
|
List<${ClassName}> list = ${className}Service.select${ClassName}List(${className});
|
||||||
ExcelUtil<${ClassName}> util = new ExcelUtil<${ClassName}>(${ClassName}.class);
|
ExcelUtil<${ClassName}> util = new ExcelUtil<${ClassName}>(${ClassName}.class);
|
||||||
util.exportExcel(response, list, "${functionName}数据");
|
util.exportExcel(response, list, "${functionName}数据", "${functionName}数据");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -591,7 +591,7 @@ export default {
|
||||||
handleExport() {
|
handleExport() {
|
||||||
this.download('${moduleName}/${businessName}/export', {
|
this.download('${moduleName}/${businessName}/export', {
|
||||||
...this.queryParams
|
...this.queryParams
|
||||||
}, `${businessName}_#[[${new Date().getTime()}]]#.xlsx`)
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -589,7 +589,7 @@ function handle${subClassName}SelectionChange(selection) {
|
||||||
function handleExport() {
|
function handleExport() {
|
||||||
proxy.download('${moduleName}/${businessName}/export', {
|
proxy.download('${moduleName}/${businessName}/export', {
|
||||||
...queryParams.value
|
...queryParams.value
|
||||||
}, `${businessName}_#[[${new Date().getTime()}]]#.xlsx`)
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
getList();
|
getList();
|
||||||
|
|
|
||||||
|
|
@ -73,6 +73,12 @@ service.interceptors.response.use(res => {
|
||||||
const msg = errorCode[code] || res.data.msg || errorCode['default']
|
const msg = errorCode[code] || res.data.msg || errorCode['default']
|
||||||
// 二进制数据则直接返回
|
// 二进制数据则直接返回
|
||||||
if(res.request.responseType === 'blob' || res.request.responseType === 'arraybuffer'){
|
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
|
return res.data
|
||||||
}
|
}
|
||||||
if (code === 401) {
|
if (code === 401) {
|
||||||
|
|
@ -141,6 +147,8 @@ export function download(url, params, filename, config) {
|
||||||
const isLogin = await blobValidate(data);
|
const isLogin = await blobValidate(data);
|
||||||
if (isLogin) {
|
if (isLogin) {
|
||||||
const blob = new Blob([data])
|
const blob = new Blob([data])
|
||||||
|
// 如果方法参数的文件名称为空,则使用响应请求返回的文件名称
|
||||||
|
filename = filename || data.filename
|
||||||
saveAs(blob, filename)
|
saveAs(blob, filename)
|
||||||
} else {
|
} else {
|
||||||
const resText = await data.text();
|
const resText = await data.text();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue