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