feat: 导出表格工具
This commit is contained in:
@@ -1,10 +1,8 @@
|
|||||||
import { read, utils } from 'xlsx';
|
import { read, utils, write } from 'xlsx';
|
||||||
|
|
||||||
/**
|
// 静态资源路径
|
||||||
* <20><><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC><EFBFBD>ļ<EFBFBD>-<2D><>̬<EFBFBD><CCAC>Դ<EFBFBD>ļ<EFBFBD>·<EFBFBD><C2B7>
|
|
||||||
*/
|
|
||||||
const baseUrl = import.meta.env.VITE_HISTORY_BASE_URL;
|
const baseUrl = import.meta.env.VITE_HISTORY_BASE_URL;
|
||||||
export const scriptUrl = `${
|
export const xlsxUrl = `${
|
||||||
baseUrl.length === 1 && baseUrl.indexOf('/') === 0
|
baseUrl.length === 1 && baseUrl.indexOf('/') === 0
|
||||||
? ''
|
? ''
|
||||||
: baseUrl.indexOf('/') === -1
|
: baseUrl.indexOf('/') === -1
|
||||||
@@ -12,19 +10,54 @@ export const scriptUrl = `${
|
|||||||
: baseUrl
|
: baseUrl
|
||||||
}/alarmHelp`;
|
}/alarmHelp`;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 读取本地文件
|
||||||
|
* @param id 表格ID
|
||||||
|
* @returns 数据数组
|
||||||
|
* @example
|
||||||
|
* readLoalXlsx('20001').then(res=>{
|
||||||
|
* console.log(res)
|
||||||
|
* });
|
||||||
|
*/
|
||||||
|
export async function readLoalXlsx(id: string): Promise<Record<string, any>[]> {
|
||||||
|
const fileBolb = await (await fetch(`${xlsxUrl}/${id}.xlsx`)).arrayBuffer();
|
||||||
|
return readSheet(fileBolb, 0);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <EFBFBD><EFBFBD>ʽʱ<EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>
|
* 读取表格数据 工作表
|
||||||
* @param dateStr ʱ<><CAB1><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
|
* @param fileBolb 文件对象
|
||||||
* @param formatStr ʱ<><CAB1><EFBFBD><EFBFBD>ʽ Ĭ<><C4AC>YYYY-MM-DD HH:mm:ss
|
* @param index 文件保存路径
|
||||||
* @returns Date<74><65><EFBFBD><EFBFBD>
|
* @return 表格对象列表
|
||||||
*/
|
*/
|
||||||
export async function parseStrToDate(): Promise<Record<string, any>[]> {
|
export async function readSheet(
|
||||||
|
fileBolb: Blob | ArrayBuffer,
|
||||||
|
index: number = 0
|
||||||
|
): Promise<Record<string, string>[]> {
|
||||||
|
const workBook = read(fileBolb);
|
||||||
|
let workSheet = workBook.Sheets[workBook.SheetNames[index]];
|
||||||
|
return utils.sheet_to_json<Record<string, string>>(workSheet);
|
||||||
|
}
|
||||||
|
|
||||||
const f = await (await fetch(scriptUrl+"/20001.xlsx")).arrayBuffer();
|
/**
|
||||||
const wb = read(f);1
|
* 写入表格数据,一般用于导出
|
||||||
const data = utils.sheet_to_json< Record<string, any>>(wb.Sheets[wb.SheetNames[0]]);
|
* @param filePath 文件路径
|
||||||
console.log(data);
|
* @param sheetName 工作表名称
|
||||||
return data;
|
* @return xlsx文件流, 使用saveAs函数保存
|
||||||
}
|
* @example
|
||||||
|
* writeSheet(res.data, from.logType).then(fileBlob =>
|
||||||
|
* saveAs(fileBlob, `${from.logType}_${Date.now()}.xlsx`)
|
||||||
|
* );
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
export async function writeSheet(data: any[], sheetName: string) {
|
||||||
|
const workSheet = utils.json_to_sheet(data);
|
||||||
|
// 设置列宽度,单位厘米
|
||||||
|
workSheet['!cols'] = Object.keys(data[0]).map(() => {
|
||||||
|
return { wch: 20 };
|
||||||
|
});
|
||||||
|
const workBook = utils.book_new();
|
||||||
|
utils.book_append_sheet(workBook, workSheet, sheetName);
|
||||||
|
const excelBuffer = write(workBook, { type: 'array', bookType: 'xlsx' });
|
||||||
|
return new Blob([excelBuffer], { type: 'application/octet-stream' });
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user