perbaikan UI device dan penambahan export
This commit is contained in:
@@ -1,37 +1,87 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import * as FileSaver from 'file-saver';
|
||||
import * as XLSX from 'xlsx';
|
||||
import { Injectable } from "@angular/core";
|
||||
import * as FileSaver from "file-saver";
|
||||
import * as XLSX from "xlsx";
|
||||
|
||||
const EXCEL_TYPE =
|
||||
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8';
|
||||
const EXCEL_EXTENSION = '.xlsx';
|
||||
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8";
|
||||
const EXCEL_EXTENSION = ".xlsx";
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
providedIn: "root",
|
||||
})
|
||||
export class TableexcelService {
|
||||
constructor() { }
|
||||
constructor() {}
|
||||
public exportAsExcelFile(json: any[], excelFileName: string): void {
|
||||
const worksheet: XLSX.WorkSheet = XLSX.utils.json_to_sheet(json);
|
||||
console.log('worksheet', worksheet);
|
||||
console.log("worksheet", worksheet);
|
||||
const workbook: XLSX.WorkBook = {
|
||||
Sheets: { data: worksheet },
|
||||
SheetNames: ['data']
|
||||
SheetNames: ["data"],
|
||||
};
|
||||
const excelBuffer: any = XLSX.write(workbook, {
|
||||
bookType: 'xlsx',
|
||||
type: 'array'
|
||||
bookType: "xlsx",
|
||||
type: "array",
|
||||
});
|
||||
this.saveAsExcelFile(excelBuffer, excelFileName);
|
||||
}
|
||||
|
||||
private saveAsExcelFile(buffer: any, fileName: string): void {
|
||||
const data: Blob = new Blob([buffer], {
|
||||
type: EXCEL_TYPE
|
||||
type: EXCEL_TYPE,
|
||||
});
|
||||
FileSaver.saveAs(
|
||||
data,
|
||||
fileName + '_export_' + new Date().getTime() + EXCEL_EXTENSION
|
||||
fileName + "_export_" + new Date().getTime() + EXCEL_EXTENSION
|
||||
);
|
||||
}
|
||||
|
||||
public exportAsExcelFileDevice(
|
||||
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: 10 },
|
||||
{ wch: 20 },
|
||||
{ wch: 20 },
|
||||
{ wch: 20 },
|
||||
{ wch: 20 },
|
||||
];
|
||||
worksheet["!cols"] = columnWidths;
|
||||
|
||||
const header = [
|
||||
"Device",
|
||||
"Building",
|
||||
"Room",
|
||||
"Watt",
|
||||
"Category",
|
||||
"Status",
|
||||
"Type",
|
||||
"Voltage",
|
||||
];
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user