import { exportExcel } from "/@/utils/exportExcelForDetail";
|
import { formatUtcToData } from "/@/utils/formate";
|
import { ElMessage } from "element-plus";
|
import { ref } from "vue";
|
/***
|
*
|
前端导出:因elementui没有提供方法获取表头字段:使用summary-method动态获取table表头,用于导出excell
|
1)
|
<!-- 按照搜索条件导出 -->
|
<el-button plain icon="el-icon-upload2" @click="handleExportExcell" class="btn-export">导出</el-button>
|
|
|
2) :summary-method="getExportTitle" show-summary class="tableBox">
|
|
3) import { ExcellTableDataExport} from "@/hooks/exportTableDataExcell";
|
4) // getTabelData 方法中添加
|
// 导出参数
|
formExport.value = JSON.parse(JSON.stringify(formModel.value))
|
5)
|
// ------------封装导出功能1--前端导出--根据搜索条件导出---------------
|
const jsonExcellTableExport = {
|
titleName: "用户信息表", //导出表格名称
|
interfaceListName:getUsersList,//导出接口名称
|
changeTitleName:((arr:any)=>{ // 需要前端转义的字段
|
arr.forEach((item:any)=>{
|
item.status =( item.status == 0 ? '启用' : '冻结')
|
})
|
})
|
}
|
const { getExportTitle , handleExportExcell, formExport} = ExcellTableDataExport(jsonExcellTableExport)
|
// --------------end 封装导出功能1--根据搜索条件导出-----------------
|
|
***/
|
export function ExcellTableDataExport(_paramObj: any) {
|
const {
|
titleName, //导出表格名称
|
interfaceListName, //导出接口名称
|
changeTitleName //需要前端转中文
|
} = _paramObj;
|
const formExport = ref({
|
PageSize: 10,
|
Page: 1,
|
OrderType:""
|
});
|
const entozhExcell:any = {
|
// id: "序号"
|
};
|
// 获取表格标题
|
const getExportTitle = (param: any) => {
|
const { columns } = param;
|
columns.forEach((column: any, index: any) => {
|
if (column.label && column.label != "操作" && column.label != "序号") {
|
entozhExcell[column.property] = column.label;
|
}
|
});
|
// console.log(entozhExcell);
|
return "";
|
};
|
|
const handleExportExcell = () => {
|
const { PageSize, PageNo, OrderType, ...rest } = formExport.value;
|
// const arrZ: any = Object.values(rest).join("");
|
// if (arrZ.length <= 0) {
|
// ElMessage.warning(`请输入查询条件!`);
|
// return;
|
// }
|
formExport.value.PageNo = 1
|
formExport.value.PageSize = 100000
|
|
interfaceListName(formExport.value)
|
.then((res: { code?: any; message?: any; data?: any }) => {
|
if (res.code == 200) {
|
const { data } = res;
|
const result = data;
|
if (changeTitleName) {
|
changeTitleName(result.rows);
|
}
|
exportExcel(
|
result.rows,
|
entozhExcell,
|
"xlsx",
|
`${titleName} ${formatUtcToData(
|
new Date() + "",
|
"YYYY-MM-DD hh:mm:ss"
|
)}`
|
);
|
} else {
|
;
|
}
|
})
|
.catch((err: any) => console.log(err));
|
};
|
return {
|
getExportTitle,
|
handleExportExcell,
|
formExport,
|
entozhExcell
|
};
|
}
|
|
|
/**
|
*
|
* 后端导出 - 获取所有单号
|
*
|
1)
|
<!-- 按照查询条件导出 -->
|
<el-button plain icon="el-icon-upload2" @click="handExportOrders" class="btn-export">导出</el-button>
|
2) import { backExportOrders} from "@/hooks/exportTableDataExcell";
|
3) // getTabelData 方法中添加
|
// 导出参数
|
formExport.value = JSON.parse(JSON.stringify(formModel.value))
|
4)
|
// ------------封装导出功能2-- 获取所有单号--根据搜索条件导出---------------
|
const jsonBackExportOrders = {
|
title:'PO单', //导出文件名称
|
paramName:'PurchaseNo', //导出接口参数名称
|
interfaceListName:getPurchasePage,//查询所有单号接口名称
|
interfaceListNameExport:puchaseExport, //后端导出接口名称entozhTitle:{
|
id: "序号",
|
orderNo: "单号",
|
}
|
}
|
const { handExportOrders ,formExport} = backExportOrders(jsonBackExportOrders)
|
// --------------end 封装导出功能2-- 获取所有单号--根据搜索条件导出-----------------
|
|
*/
|
export function backExportOrders(_paramObj: any) {
|
const {
|
title,
|
paramName,
|
interfaceListName, //导出接口名称
|
interfaceListNameExport,
|
entozhTitle
|
} = _paramObj;
|
// 查询参数
|
const formExport = ref({
|
PageSize: 1,
|
PageNo: 10,
|
pagesize: 1,
|
pageno: 10,
|
MaterialTypeStaus: "",
|
OrderType:""
|
});
|
// 导出方法
|
const handExportOrders = () => {
|
const { PageSize, PageNo, pagesize, pageno, MaterialTypeStaus, OrderType, ...rest } =
|
formExport.value;
|
// const arrZ: any = Object.values(rest).join("");
|
// console.log(arrZ);
|
// if (arrZ.length <= 0) {
|
// ElMessage.warning(`请输入查询条件!`);
|
// return;
|
// }
|
formExport.value.PageSize = 1000;
|
formExport.value.PageNo = 1;
|
// 查询列表接口
|
interfaceListName({
|
...formExport.value
|
})
|
.then((res: { code?: any; message?: any; data?: any }) => {
|
if (res.code == 200) {
|
const { data } = res;
|
const result = data;
|
if (result.rows.length <= 0) {
|
ElMessage.warning(`没有需要导出的数据!`);
|
return;
|
}
|
let arr = [];
|
if (paramName == "PurchaseNo") {
|
arr = result.rows.reduce((curr: any, item: any) => {
|
curr.push(item.purchaseNo);
|
return curr;
|
}, []);
|
}else{
|
arr = result.rows.reduce((curr: any, item: any) => {
|
curr.push(item.orderNo);
|
return curr;
|
}, []);
|
}
|
const paramAll:any ={}
|
paramAll[paramName] = arr
|
// 导出接口
|
if(arr.length<=0){
|
ElMessage.warning(`单号不能为空!`);
|
return;
|
}
|
interfaceListNameExport(paramAll)
|
.then((res: any) => {
|
if(entozhTitle){ //后端导出接口,返回list非文件,需要传入中文表头
|
exportExcel(res.data, entozhTitle, "xlsx", `${title}${formatUtcToData(new Date()+'','YYYY-MM-DD hh:mm:ss')}`);
|
}else{ //后端导出返回文件
|
const link = document.createElement("a"); //创建a标签
|
const blob = new Blob([res], {
|
type: "application/vnd.ms-excel"
|
}); // response就是接口返回的文件流
|
const objectUrl = URL.createObjectURL(blob);
|
link.href = objectUrl;
|
link.download = `${title}导出${formatUtcToData(
|
new Date().toString(),
|
"YYYY-MM-DD hh:mm:ss"
|
)}`; // 自定义文件名
|
link.click(); // 下载文件
|
URL.revokeObjectURL(objectUrl); // 释放内存
|
}
|
|
})
|
.catch((err: any) => ElMessage.error(JSON.stringify(err)));
|
} else {
|
;
|
}
|
})
|
.catch((err: any) => console.log(err));
|
};
|
return {
|
handExportOrders,
|
formExport
|
};
|
}
|