text 导出

This commit is contained in:
2023-11-03 11:48:11 +08:00
parent 61f610ddd9
commit baf1ae2854
44 changed files with 45612 additions and 4701 deletions

View File

@@ -5,6 +5,8 @@ export default {
</script>
<script setup lang="ts">
import { utils, writeFile } from "xlsx";
import "./index.less";
import {
ref,
@@ -390,69 +392,107 @@ const print = () => {
document.body.innerHTML = oldContent;
};
/**
* 导出excel
* @param list 导出的信息
* @param header 信息的键名
* @param name 导出文件名称
*/
function exportElcel(list:any,header:any,name:string){
const ws = utils.json_to_sheet(list, {
header,
skipHeader: true
})
console.log(ws)
var tmpWB = {
SheetNames: ['sheet'], //保存的表标题
Sheets: {
sheet: Object.assign(
{},
ws, //内容
{}
)
}
}
writeFile(tmpWB,name + ".xlsx",{
bookType: 'xlsx',
type: 'binary'
})
}
// 报表导出
const exportData = () => {
var tableStr = ``;
let head:any = []
let list:any = []
let title:any = {}
for (let tableHeadColumn of tableHeadColumns.value) {
tableStr += "<tr>";
// tableStr += "<tr>";
for (let column of tableHeadColumn) {
tableStr += `<td colspan=${column.colspan} rowspan=${column.rowspan}>${column.title}</td>`;
// tableStr += `<td colspan=${column.colspan} rowspan=${column.rowspan}>${column.title}</td>`;
title[column.key] = column.title
head.push(column.key)
}
tableStr += "</tr>";
// tableStr += "</tr>";
}
list.push(title)
tableDataSource.value.forEach((item, rowIndex) => {
tableStr += "<tr>";
let info:any = {}
tableBodyColumns.value.forEach((tableColumn, columnIndex) => {
Object.keys(item).forEach((name) => {
if (tableColumn.key === name) {
const rowColSpan = props.spanMethod(
item,
tableColumn,
rowIndex,
columnIndex
);
const rowspan = rowColSpan ? rowColSpan[0] : 1;
const colspan = rowColSpan ? rowColSpan[1] : 1;
if (rowspan != 0 && colspan != 0) {
tableStr += `<td colspan=${colspan} rowspan=${rowspan}${tableColumn.valueType == 'str' ? ' x:str' : ''}>${item[name] || ''}</td>`;
}
// const rowColSpan = props.spanMethod(
// item,
// tableColumn,
// rowIndex,
// columnIndex
// );
// const rowspan = rowColSpan ? rowColSpan[0] : 1;
// const colspan = rowColSpan ? rowColSpan[1] : 1;
// if (rowspan != 0 && colspan != 0) {
// tableStr += `<td colspan=${colspan} rowspan=${rowspan}${tableColumn.valueType == 'str' ? ' x:str' : ''}>${item[name] || ''}</td>`;
// }
info[name] = item[name]
}
});
if(tableColumn.type == "number"){
const rowColSpan = props.spanMethod(
item,
tableColumn,
rowIndex,
columnIndex
);
const rowspan = rowColSpan ? rowColSpan[0] : 1;
const colspan = rowColSpan ? rowColSpan[1] : 1;
if (rowspan != 0 && colspan != 0) {
tableStr += `<td colspan=${colspan} rowspan=${rowspan}>${rowIndex + 1}</td>`;
}
// const rowColSpan = props.spanMethod(
// item,
// tableColumn,
// rowIndex,
// columnIndex
// );
// const rowspan = rowColSpan ? rowColSpan[0] : 1;
// const colspan = rowColSpan ? rowColSpan[1] : 1;
// if (rowspan != 0 && colspan != 0) {
// tableStr += `<td colspan=${colspan} rowspan=${rowspan}>${rowIndex + 1}</td>`;
// }
info[item.key] = rowIndex + 1
}
});
tableStr += "</tr>";
// tableStr += "</tr>";
list.push(info)
});
var worksheet = "Sheet1";
var uri = "data:application/vnd.ms-excel;base64,";
var exportTemplate = `<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns="http://www.w3.org/TR/REC-html40">
<head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet>
<x:Name>${worksheet}</x:Name>
<x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet>
</x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]-->
</head>
<body>
<table syle="table-layout: fixed;word-wrap: break-word; word-break: break-all;">${tableStr}</table>
</body>
</html>`;
let a = document.createElement("a");
a.href = uri + base64(exportTemplate);
a.download = (props.download || "下载文件") + ".xls";
a.click();
// var worksheet = "Sheet1";
// var uri = "data:application/vnd.ms-excel;base64,";
// var exportTemplate = `<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel"
// xmlns="http://www.w3.org/TR/REC-html40">
// <head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet>
// <x:Name>${worksheet}</x:Name>
// <x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet>
// </x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]-->
// </head>
// <body>
// <table syle="table-layout: fixed;word-wrap: break-word; word-break: break-all;">${tableStr}</table>
// </body>
// </html>`;
// let a = document.createElement("a");
// a.href = uri + base64(exportTemplate);
// a.download = (props.download || "下载文件") + ".xls";
// a.click();
// window.location.href =
exportElcel(list,head,(props.download || "下载文件"))
return;
};