-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathexport-excel.service.ts
146 lines (128 loc) · 3.75 KB
/
export-excel.service.ts
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { Injectable } from "@angular/core";
import fs from "file-saver";
import { Workbook } from "exceljs";
import { Router } from "@angular/router";
export interface ExcelData {
title: string;
headers: unknown;
footerText: string;
sheetTitle: string;
data: unknown[];
}
@Injectable({
providedIn: "root",
})
export class ExportExcelService {
constructor(private router: Router) {}
exportExcel(excelData: ExcelData) {
// Title, Header & Data
const title = excelData.title;
const parsedUrl = new URL(window.location.href);
const baseUrl = parsedUrl.origin;
const url = baseUrl + this.router.url;
const header = excelData.headers;
// const data = excelData.data;
const footerText = excelData.footerText;
const sheetTitle = excelData.sheetTitle;
// Create a workbook with a worksheet
const workbook = new Workbook();
const worksheet = workbook.addWorksheet(sheetTitle);
// Add Row and formatting
worksheet.mergeCells("C1", "H4");
const titleRow = worksheet.getCell("C1");
titleRow.value = title;
titleRow.font = {
name: "Calibri",
size: 16,
underline: "single",
bold: true,
color: { argb: "0085A3" },
};
titleRow.alignment = { vertical: "middle", horizontal: "center" };
// Date
worksheet.mergeCells("I1:J4");
const date = new Date();
const formattedDate =
date.getDate() + "-" + date.getMonth() + "-" + date.getFullYear();
const dateCell = worksheet.getCell("I1");
dateCell.value = formattedDate;
dateCell.font = {
name: "Calibri",
size: 12,
bold: true,
};
dateCell.alignment = { vertical: "middle", horizontal: "center" };
// Add Image
/* const myLogoImage = workbook.addImage({
base64: logo.imgBase64,
extension: "png",
});
worksheet.mergeCells("A1:B4");
worksheet.addImage(myLogoImage, "A1:B4"); */
// URL
worksheet.mergeCells("C5", "H6");
const urlRow = worksheet.getCell("C5");
urlRow.value = url;
urlRow.font = {
name: "Calibri",
size: 10,
underline: "single",
bold: false,
color: { argb: "0085A3" },
};
urlRow.alignment = { vertical: "middle", horizontal: "center" };
// Blank Row
worksheet.addRow([]);
// Adding Header Row
const headerRow = worksheet.addRow(header);
let icol = 0;
headerRow.eachCell((cell, number) => {
// define some default column width
icol++;
worksheet.getColumn(icol).width = 25;
cell.fill = {
type: "pattern",
pattern: "solid",
fgColor: { argb: "4167B8" },
bgColor: { argb: "" },
};
cell.font = {
bold: true,
color: { argb: "FFFFFF" },
size: 12,
};
});
// Adding Data
excelData.data.forEach((d) => {
worksheet.addRow(d);
// with Conditional Formatting
// let sales = row.getCell(6);
// let color = "FF99FF99";
// if (+sales.value < 200000) {
// color = "FF9999";
// }
// sales.fill = {
// type: "pattern",
// pattern: "solid",
// fgColor: { argb: color },
// };
});
worksheet.addRow([]);
// Footer Row
const footerRow = worksheet.addRow([footerText]);
footerRow.getCell(1).fill = {
type: "pattern",
pattern: "solid",
fgColor: { argb: "FFB050" },
};
// Merge Cells
worksheet.mergeCells(`A${footerRow.number}:F${footerRow.number}`);
// Generate & Save Excel File
workbook.xlsx.writeBuffer().then((writeData) => {
const blob = new Blob([writeData], {
type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
});
fs.saveAs(blob, title + ".xlsx");
});
}
}