update export xls

This commit is contained in:
2024-12-26 15:42:39 +07:00
parent febd8e0560
commit 1c5d586f7b
2 changed files with 19 additions and 16 deletions

View File

@ -21,6 +21,7 @@
"cors": "^2.8.5",
"dayjs": "^1.11.13",
"entity": "file:../entity",
"exceljs": "^4.4.0",
"express": "^4.21.1",
"express-jwt": "^8.4.1",
"fast-csv": "^5.0.2",

View File

@ -11,6 +11,7 @@ import { ActivityType } from "entity";
import * as fastcsv from 'fast-csv';
import dayjs from "dayjs";
import { UserActivity } from "entity";
import exceljs from "exceljs";
const log: Logger<ILogObj> = new Logger({ name: '[UserActivityController]', type: 'pretty' });
@ -185,21 +186,20 @@ export class UserActivityController {
whereVal['id_user'] = req.auth.data.id;
}
const filename = "user_activity.csv";
res.setHeader('Content-Type', 'text/csv');
const filename = "user_activity.xlsx";
res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
res.setHeader('Content-Disposition', 'attachment; filename=' + filename);
const csvStream = fastcsv.format({
headers: true,
writeHeaders: true,
transform: (row: UserActivity): any => ({
...row,
action: CommonHelper.getKeyEnum(ActivityType, row.action),
created_at: dayjs(row.created_at).format('DD-MM-YYYY HH:MM:ss'),
})
});
csvStream.pipe(res);
const workbook = new exceljs.stream.xlsx.WorkbookWriter({ stream: res });
const sheet = workbook.addWorksheet('Data');
sheet.columns = [
{ header: "Module", key: "module", width: 15 },
{ header: "Description", key: "description", width: 25 },
{ header: "Action", key: "action", width: 10 },
{ header: "Url", key: "url", width: 25 },
{ header: "Created At", key: "created_at", width: 15 }
];
const limit = 50;
@ -220,14 +220,16 @@ export class UserActivityController {
.limit(limit).getRawMany();
if (CommonHelper.countObject(data) === 0) {
csvStream.end();
sheet.commit();
workbook.commit();
} else {
data.forEach((item: any) => csvStream.write(item));
data.forEach((item: any) => sheet.addRow(item).commit());
if (CommonHelper.countObject(data) == limit) {
fetchAndWrite(page + 1);
} else {
csvStream.end();
sheet.commit();
workbook.commit();
}
}
};