2024-11-12 15:44:05 +08:00
|
|
|
// 新建 @/utils/excel.ts
|
|
|
|
|
|
|
|
import saveAs from 'file-saver'; // https://www.npmjs.com/package/file-saver
|
|
|
|
import ExcelJS from 'exceljs'; // https://github.com/exceljs/exceljs/blob/master/README_zh.md
|
|
|
|
import * as XLSX from 'xlsx'; // https://www.npmjs.com/package/xlsx
|
|
|
|
import { Message } from '@arco-design/web-vue'; // https://arco.design/vue/component/message
|
|
|
|
import { FileItem } from '@arco-design/web-vue/es/upload/interfaces'; // arco类型
|
|
|
|
|
|
|
|
export interface DownloadExcelPrams {
|
2024-12-24 17:13:37 +08:00
|
|
|
columns: { title: string; key: string }[];
|
|
|
|
rows: object[];
|
|
|
|
name: string;
|
2024-11-12 15:44:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 导出下载文件
|
2024-12-24 17:13:37 +08:00
|
|
|
export function downloadExcel({
|
|
|
|
columns,
|
|
|
|
rows,
|
|
|
|
name = '未命名文件',
|
|
|
|
}: DownloadExcelPrams) {
|
|
|
|
const workbook = new ExcelJS.Workbook();
|
|
|
|
workbook.creator = 'Start-front';
|
|
|
|
workbook.lastModifiedBy = 'Start-front';
|
|
|
|
workbook.created = new Date(1985, 8, 30);
|
|
|
|
workbook.modified = new Date();
|
|
|
|
workbook.lastPrinted = new Date(2016, 9, 27);
|
2024-11-12 15:44:05 +08:00
|
|
|
|
2024-12-24 17:13:37 +08:00
|
|
|
// 将工作簿添加一个sheet页sheet1
|
|
|
|
const sheet1 = workbook.addWorksheet(name);
|
|
|
|
// 表头数据添加
|
|
|
|
sheet1.columns = columns.map((item) => ({
|
|
|
|
header: item.title,
|
|
|
|
key: item.key,
|
|
|
|
width: 20,
|
|
|
|
}));
|
|
|
|
// 表格内容添加
|
|
|
|
rows.map((item) => sheet1.addRow(item));
|
|
|
|
workbook.xlsx.writeBuffer().then((buffer) => {
|
|
|
|
saveAs(
|
|
|
|
new Blob([buffer], { type: 'application/octet-stream' }),
|
|
|
|
`${name}.xlsx`
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
2024-11-12 15:44:05 +08:00
|
|
|
|
|
|
|
// 读取文件为json格式
|
2024-12-24 17:13:37 +08:00
|
|
|
export function readExcle(fileItem: FileItem) {
|
|
|
|
return new Promise((resove, reject) => {
|
|
|
|
try {
|
|
|
|
let workbook: XLSX.Sheet;
|
|
|
|
const reader = new FileReader();
|
|
|
|
reader.readAsBinaryString(fileItem.file as File); // 发起异步请求
|
|
|
|
reader.onload = function (ev) {
|
|
|
|
const data = ev.target?.result;
|
|
|
|
workbook = XLSX.read(data, { type: 'binary' });
|
|
|
|
const sheetNames = workbook.SheetNames; // 工作表名称集合
|
|
|
|
sheetNames.forEach((name: string) => {
|
|
|
|
const worksheet = workbook.Sheets[name]; // 只能通过工作表名称来获取指定工作表
|
|
|
|
const jsonres = XLSX.utils.sheet_to_json(worksheet);
|
|
|
|
resove(jsonres);
|
|
|
|
});
|
|
|
|
}; // onload
|
|
|
|
} catch (error) {
|
|
|
|
Message.error('读取失败,请选择正确文件');
|
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
});
|
2024-11-12 15:44:05 +08:00
|
|
|
}
|