Skip to content

Commit 11b0252

Browse files
committed
feat(helper): add excel helper
1 parent 19f028b commit 11b0252

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

src/helpers/Excel.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import * as ExcelJS from 'exceljs'
2+
3+
class ExcelHelper {
4+
/**
5+
*
6+
* @param headers
7+
* @param data
8+
*/
9+
public static async generate(
10+
headers: Partial<ExcelJS.Column>[],
11+
data: any[]
12+
): Promise<Buffer> {
13+
const workBook = new ExcelJS.stream.xlsx.WorkbookWriter({})
14+
const sheet = workBook.addWorksheet('My Worksheet')
15+
16+
// @ts-ignore
17+
sheet.columns = headers
18+
for (let i = 0; i < data.length; i += 1) {
19+
const tempData = { no: i + 1, ...data[i] }
20+
sheet.addRow(tempData)
21+
}
22+
sheet.getRow(1).font = { bold: true }
23+
sheet.commit()
24+
25+
return new Promise((resolve, reject) => {
26+
workBook
27+
.commit()
28+
.then(() => {
29+
const { stream } = workBook as any
30+
const result = stream.read()
31+
resolve(result)
32+
})
33+
.catch((err: any) => {
34+
reject(err)
35+
})
36+
})
37+
}
38+
}
39+
40+
export default ExcelHelper

0 commit comments

Comments
 (0)