export detail

This commit is contained in:
2024-06-28 08:07:17 +07:00
parent be40cea2a1
commit ca30a991ae
9 changed files with 680 additions and 35 deletions

View File

@@ -98,11 +98,17 @@ export class TableexcelService {
});
return filteredItem;
});
// Calculate totals for estimation_cost and total_use
const totalEstimationCost = filteredJson.reduce((sum, item) => sum + (item['estimation_cost'] || 0), 0);
const totalUse = filteredJson.reduce((sum, item) => sum + (item['total_use'] || 0), 0);
const totalEstimationCost = filteredJson.reduce(
(sum, item) => sum + (item["estimation_cost"] || 0),
0
);
const totalUse = filteredJson.reduce(
(sum, item) => sum + (item["total_use"] || 0),
0
);
// Create the worksheet
const worksheet: XLSX.WorkSheet = XLSX.utils.json_to_sheet(filteredJson);
const columnWidths = [
@@ -114,7 +120,7 @@ export class TableexcelService {
{ wch: 30 },
];
worksheet["!cols"] = columnWidths;
// Add header
const header = [
"Building",
@@ -125,7 +131,7 @@ export class TableexcelService {
"Date",
];
XLSX.utils.sheet_add_aoa(worksheet, [header]);
// Add totals row
const totalsRow = [
"Totals",
@@ -133,21 +139,73 @@ export class TableexcelService {
"",
`Total: ${totalUse} Kwh`,
`Total: ${totalEstimationCost}`,
""
"",
];
XLSX.utils.sheet_add_aoa(worksheet, [totalsRow], { origin: -1 });
// Create the workbook
const workbook: XLSX.WorkBook = {
Sheets: { data: worksheet },
SheetNames: ["data"],
};
// Write the workbook and save it
const excelBuffer: any = XLSX.write(workbook, {
bookType: "xlsx",
type: "array",
});
this.saveAsExcelFile(excelBuffer, excelFileName);
}
}
public exportAsExcelFileManageDetail(
json: any[],
excelFileName: string,
columns: string[]
): void {
const filteredJson = json.map((item) => {
const filteredItem = {};
columns.forEach((column) => {
filteredItem[column] = item[column];
});
return filteredItem;
});
const worksheet: XLSX.WorkSheet = XLSX.utils.json_to_sheet(filteredJson);
const columnWidths = [
{ wch: 40 },
{ wch: 30 },
{ wch: 30 },
{ wch: 20 },
{ wch: 20 },
{ wch: 20 },
{ wch: 20 },
{ wch: 20 },
{ wch: 30 },
];
worksheet["!cols"] = columnWidths;
const header = [
"Device",
"Room",
"Category",
"Estimation Cost",
"Total Kwh",
"Watt",
"Duration",
"Price Kwh",
"Periode",
];
XLSX.utils.sheet_add_aoa(worksheet, [header]);
const workbook: XLSX.WorkBook = {
Sheets: { data: worksheet },
SheetNames: ["data"],
};
const excelBuffer: any = XLSX.write(workbook, {
bookType: "xlsx",
type: "array",
});
this.saveAsExcelFile(excelBuffer, excelFileName);
}
}