1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| import * as ExcelJS from 'exceljs';
async exportToExcel(deviceId: string, start: Date, end: Date): Promise<Buffer> { const historyData = await this.historyRepo.find({ where: { deviceId, timestamp: Between(start, end) }, order: { timestamp: 'ASC' }, });
const workbook = new ExcelJS.Workbook(); const worksheet = workbook.addWorksheet('历史数据');
worksheet.columns = [ { header: '时间', key: 'timestamp' }, { header: '温度', key: 'temperature' }, { header: '压力', key: 'pressure' }, ];
historyData.forEach(item => { worksheet.addRow({ timestamp: item.timestamp, temperature: item.data.temperature, pressure: item.data.pressure, }); });
return workbook.xlsx.writeBuffer() as Promise<Buffer>; }
|