File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed
Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments