File tree Expand file tree Collapse file tree 1 file changed +29
-2
lines changed
Expand file tree Collapse file tree 1 file changed +29
-2
lines changed Original file line number Diff line number Diff line change 1+ import fs from 'fs'
12import * as ExcelJS from 'exceljs'
3+ import excelToJson from 'convert-excel-to-json'
24
3- class ExcelHelper {
5+ interface OptionConvert {
6+ header ?: any
7+ columnToKey ?: any
8+ }
9+
10+ class Excel {
411 /**
512 *
613 * @param headers
@@ -35,6 +42,26 @@ class ExcelHelper {
3542 } )
3643 } )
3744 }
45+
46+ /**
47+ *
48+ * @param path
49+ * @param options
50+ * options is used when there is only 1 sheet
51+ */
52+ public static convertToJson ( path : string | Buffer , options ?: OptionConvert ) {
53+ const resultConvert = excelToJson ( {
54+ source : fs . readFileSync ( path ) , // fs.readFileSync return a Buffer
55+ header : options ?. header || {
56+ rows : 1 ,
57+ } ,
58+ columnToKey : options ?. columnToKey || {
59+ '*' : '{{columnHeader}}' ,
60+ } ,
61+ } )
62+
63+ return resultConvert
64+ }
3865}
3966
40- export default ExcelHelper
67+ export default Excel
You can’t perform that action at this time.
0 commit comments