前言
- 本文所提及的导出方法是基于本人自己使用的项目,若要满足自己的项目需求,需要对方法做适量的调整
- 本文知识学习于网上,经过整理所得,时间跨度较长,已遗忘参考于哪个博主,如有侵权,联系即删!
一、导出 Excel
1、按装依赖
npm install -S xlsx
2、方法
import * as XLSX from 'xlsx';
exportExcel(title, data, outFileName, orderProp) {
let prop = 'prop'
let titleList = this.toolTitleList(title)
let tableData = [
[...titleList]
]
data.forEach((tableItem, tableIndex) => {
let rowData = []
title.forEach((titleItem) => {
if (titleItem[prop] === orderProp) {
rowData.push(tableIndex + 1)
} else {
rowData.push(tableItem[titleItem[prop]])
}
})
tableData.push(rowData)
})
let aoaToSheet = XLSX.utils.aoa_to_sheet(tableData)
let bookNew = XLSX.utils.book_new()
XLSX.utils.book_append_sheet(bookNew, aoaToSheet, outFileName)
XLSX.writeFile(bookNew, outFileName + '.xlsx')
},
toolTitleList(data) {
let result = []
data.forEach(item => {
result.push(item['label'])
})
return result
},
3、在本文中的使用示例
- 在 Vue 中 methods 中使用
- 由于后端返回的数据没有“序号”对应的字段,前端又需要展示,于是最后一个参数用于判断是否有“序号”字段,不需要可以将该参数置为空字符串
methods: {
handleDownloadClick() {
let titleList = [
{
label: "序号",
prop: "order_id",
width: "4%",
},
{
label: "区域名称",
prop: "area_name",
width: "9%",
},
{
label: "厂站名称",
prop: "st_name",
width: "20%",
},
]
let tableList = [
{
area_name: "AREANAME",
st_name: "STNAME"
},
]
let outfilename = "数据表";
let orderProp = "order_id";
exportWordExcelPdf.exportExcel(
titleList,
tableList,
outfilename,
orderProp
);
},
}
二、导出 Word
1、按装依赖
npm install -S docxtemplater file-saver jszip-utils pizzip
2、方法
import docxtemplater from 'docxtemplater';
import { saveAs } from 'file-saver';
import JSZipUtils from 'jszip-utils';
import PizZip from 'pizzip';
exportWord(tempDocxPath, wordData, fileName) {
JSZipUtils.getBinaryContent(tempDocxPath, function (error, content) {
if (error) {
throw error;
}
let zip = new PizZip(content);
let doc = new docxtemplater();
doc.loadZip(zip);
doc.setData(wordData);
try {
doc.render();
} catch (error) {
let e = {
message: error.message,
name: error.name,
stack: error.stack,
properties: error.properties
};
console.log(JSON.stringify({
error: e
}));
throw error;
}
let out = doc.getZip().generate({
type: "blob",
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
});
saveAs(out, fileName + ".docx");
});
},
3、在本文中的使用示例
- 第一个参数是模版文件的路径,在 Vue3 项目中放在 public 目录下
- 第二个参数是在模版中展示的数据内容,默认是一个对象的形式,模版中使用示例如下图
- 第三个参数是导出的文件名
methods: {
exportWordFile() {
let param = {
message: "这是一段文本信息"
}
exportWord("/word.docx", param, "exportTestWord");
},
},
三、导出 PDF
1、安装依赖
npm install -S html2canvas jspdf
2、方法
import html2Canvas from 'html2canvas'
import JsPDF from 'jspdf'
exportPdf(dom, outFileName) {
var element = document.querySelector(dom);
setTimeout(() => {
html2Canvas(element).then(function (canvas) {
var contentWidth = canvas.width;
var contentHeight = canvas.height;
var pageHeight = contentWidth / 592.28 * 841.89;
var leftHeight = contentHeight;
var position = 0;
var imgWidth = 595.28;
var imgHeight = 592.28 / contentWidth * contentHeight;
var pageData = canvas.toDataURL('image/jpeg', 1.0);
var pdf = new JsPDF('', 'pt', 'a4');
if (leftHeight < pageHeight) {
pdf.addImage(pageData, 'JPEG', 0, 0, imgWidth, imgHeight);
} else {
while (leftHeight > 0) {
pdf.addImage(pageData, 'JPEG', 0, position, imgWidth, imgHeight)
leftHeight -= pageHeight;
position -= 841.89;
if (leftHeight > 0) {
pdf.addPage();
}
}
}
pdf.save(outFileName + '.pdf');
});
}, 0);
}
3、在本文中的使用示例
methods: {
handleDownloadPdfClick() {
let dom = "#pdfDom"
let outFileName = "Pdf导出"
this.getPdf(dom, outFileName);
},
},
四、综合文件
1、安装依赖
npm install -S xlsx
npm install -S docxtemplater file-saver jszip-utils pizzip
npm install -S html2canvas jspdf
2、工具文件
import docxtemplater from 'docxtemplater';
import { saveAs } from 'file-saver';
import html2Canvas from 'html2canvas';
import JsPDF from 'jspdf';
import JSZipUtils from 'jszip-utils';
import PizZip from 'pizzip';
export default {
exportExcel(title, data, outFileName, orderProp) {
let prop = 'prop'
let titleList = this.toolTitleList(title)
let tableData = [
[...titleList]
]
data.forEach((tableItem, tableIndex) => {
let rowData = []
title.forEach((titleItem) => {
if (titleItem[prop] === orderProp) {
rowData.push(tableIndex + 1)
} else {
rowData.push(tableItem[titleItem[prop]])
}
})
tableData.push(rowData)
})
let aoaToSheet = XLSX.utils.aoa_to_sheet(tableData)
let bookNew = XLSX.utils.book_new()
XLSX.utils.book_append_sheet(bookNew, aoaToSheet, outFileName)
XLSX.writeFile(bookNew, outFileName + '.xlsx')
},
toolTitleList(data) {
let result = []
data.forEach(item => {
result.push(item['label'])
})
return result
},
exportWord(tempDocxPath, wordData, fileName) {
JSZipUtils.getBinaryContent(tempDocxPath, function (error, content) {
if (error) {
throw error;
}
let zip = new PizZip(content);
let doc = new docxtemplater();
doc.loadZip(zip);
doc.setData(wordData);
try {
doc.render();
} catch (error) {
let e = {
message: error.message,
name: error.name,
stack: error.stack,
properties: error.properties
};
console.log(JSON.stringify({
error: e
}));
throw error;
}
let out = doc.getZip().generate({
type: "blob",
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
});
saveAs(out, fileName);
});
},
exportPdf(dom, outFileName) {
var element = document.querySelector(dom);
setTimeout(() => {
html2Canvas(element).then(function (canvas) {
var contentWidth = canvas.width;
var contentHeight = canvas.height;
var pageHeight = contentWidth / 592.28 * 841.89;
var leftHeight = contentHeight;
var position = 0;
var imgWidth = 595.28;
var imgHeight = 592.28 / contentWidth * contentHeight;
var pageData = canvas.toDataURL('image/jpeg', 1.0);
var pdf = new JsPDF('', 'pt', 'a4');
if (leftHeight < pageHeight) {
pdf.addImage(pageData, 'JPEG', 0, 0, imgWidth, imgHeight);
} else {
while (leftHeight > 0) {
pdf.addImage(pageData, 'JPEG', 0, position, imgWidth, imgHeight)
leftHeight -= pageHeight;
position -= 841.89;
if (leftHeight > 0) {
pdf.addPage();
}
}
}
pdf.save(outFileName + '.pdf');
});
}, 0);
}
}
3、在本文中的使用示例
import exportWordExcelPdf from "./../../utils/exportWordExcelPdf";
methods: {
handleDownloadClick() {
let outfilename = "厂站数据表";
let orderProp = "order_id";
exportWordExcelPdf.exportExcel(
this.factoryTableTitle,
this.factoryTableList,
outfilename,
orderProp
);
},
}
总结
|