根据上传的Excel文件解析里面的数据。
yarn add @mmstudio/an000024 @mmstudio/an000041
export const config = { api: { bodyParser: false } } as PageConfig;
import { promises as fs } from 'fs'; import an41 from '@mmstudio/an000041'; import an24 from '@mmstudio/an000024';
// 解析文件 const [file] = await an41(req); const content = await fs.readFile(file.path); await fs.rm(file.path); /** * 文件内容json */ const data = (() => { interface IData { } const p1 = content; // 文件内容,Buffer return an24<IData>(p1); })(); console.log(data);
import { Workbook } from 'exceljs'; const wb = new Workbook(); await wb.xlsx.readFile('filepath'); const ws = wb.getWorksheet(0); for (let idx = 1; idx <= ws.actualColumnCount; idx++) { const column = ws.getColumn(idx); } for (let idx = 1; idx <= ws.actualRowCount; idx++) { const row = ws.getRow(idx); }
function getcellvaluestr(cell: Cell) { const value = cell.value; switch (cell.type) { case ValueType.Boolean: case ValueType.Date: case ValueType.Number: return value?.toString().trim(); case ValueType.Error: return (value as CellErrorValue).error as string; case ValueType.Formula: return ((value as CellFormulaValue).result as string).trim(); case ValueType.Hyperlink: return (value as CellHyperlinkValue).text.toString().trim(); case ValueType.Null: return ''; case ValueType.RichText: return (value as CellRichTextValue).richText.map((v) => { return v.text.toString(); }).join('').trim(); case ValueType.Merge: case ValueType.SharedString: case ValueType.String: default: return value?.toString().trim(); } }