寫在前面
最近因?yàn)闃I(yè)務(wù)需要像棘,需要在瀏覽器通過excel批量導(dǎo)入數(shù)據(jù)和快讀導(dǎo)出數(shù)據(jù)稽亏,因此簡單封裝了下xlsx庫,以便快速完成導(dǎo)入導(dǎo)出操作缕题。
如何使用
# Basic Node.JS installation
npm install extend-excel --save
源碼
場景1 Excel數(shù)據(jù)轉(zhuǎn)化為JSON數(shù)據(jù):
1)如果您需要將Excel文件中的所有數(shù)據(jù)轉(zhuǎn)換為JSON截歉,import_excel
函數(shù)將為您提供幫助。
測試原始文件內(nèi)容:
const { importExcel } = extendExcel
<!-- read file -->
const fileBuffer = fs.readFileSync(path.resolve(__dirname, './__mocks__/test.xlsx'))
const blob = new Blob([fileBuffer], { type : 'application/xlsx'});
const excelFile = new File([blob], 'test');
<!-- excel to json -->
const result = await importExcel(excelFile);
console.log(result)
2)如果您想將Excel文件的第一個(gè)工作表中的數(shù)據(jù)轉(zhuǎn)換為JSON烟零,您可以利用可選參數(shù)來實(shí)現(xiàn)這一點(diǎn)瘪松。
const { onlyFirst, XLSXReadOptions } = options
const result = await importExcel(excelFile, { onlyFirst: true })
3)如果您想將Excel文件的第一個(gè)工作表中的數(shù)據(jù)轉(zhuǎn)換為JSON,并映射Excel頭部名稱瓶摆,您可以利用可選參數(shù)來實(shí)現(xiàn)這一點(diǎn)凉逛。
const { onlyFirst, keyMapping, XLSXReadOptions } = options
const result = await importExcel(excelFile, {
keyMapping: {
測試1: 'test1',
測試2: 'test2',
測試3: 'test3',
測試sheet1: 'testsheet1',
測試sheet2: 'testsheet2',
測試sheet3: 'testsheet3'
}
})
結(jié)果:
{
name: 'test',
data: [
{ 'test1': 1, 'test2': 2, 'test3': 3 },
{ 'testsheet1': 1, 'testsheet2': 2, 'testsheet3': 3 }
]
}
4)如果您需要更多選項(xiàng),您可以傳遞XLSXReadOptions
參數(shù)群井,該參數(shù)與xlsx
選項(xiàng)兼容状飞。
場景2 JSON數(shù)據(jù)導(dǎo)出為xlsx:
1)如果您想要下載從JSON數(shù)據(jù)生成的Excel文件,export_excel
函數(shù)可以幫助您。
const { exportExcel } = extend-excel
<!-- json to excel -->
const headers = {
'test': '測試1',
'test2': '測試2',
'test3': '測試3',
'testsheet1': '測試sheet1',
'testsheet2': '測試sheet2',
'testsheet3': '測試sheet3',
}
const sourceData = [
{ 'test': 1, 'test2': 2, 'test3': 3 },
{ 'testsheet1': 1, 'testsheet2': 2, 'testsheet3': 3 }
]
const options = {}
const result = await exportExcel(headers, sourceData, options)
2)如果您想要自定義文件名并下載從JSON數(shù)據(jù)生成的Excel文件书斜,export_excel
函數(shù)可以幫助您诬辈。
const { exportExcel } = extend-excel
<!-- json to excel -->
const headers = {
'test': '測試1',
'test2': '測試2',
'test3': '測試3',
'testsheet1': '測試sheet1',
'testsheet2': '測試sheet2',
'testsheet3': '測試sheet3',
}
const sourceData = [
{ 'test': 1, 'test2': 2, 'test3': 3 },
{ 'testsheet1': 1, 'testsheet2': 2, 'testsheet3': 3 }
]
const options = { filename: 'test-export' }
const result = await exportExcel(headers, sourceData, options)
3)如果您想要自定義下載文件的屬性,您可以通過傳遞options
參數(shù)來實(shí)現(xiàn)荐吉。
const { fileName, sheetName, XLSXOption } = options
Attribute name | info |
---|---|
fileName | 自定義下載文件的名稱焙糟。 |
sheetName | 自定義下載文件的工作表名稱。 |
XLSXOption | 調(diào)整xlsx庫的選項(xiàng)样屠。 |
Typescript Support
export interface DynamicObject {
[key: string]: any
}
export interface ExportExcelOptions {
fileName: string,
sheetName: string,
XLSXOption: WritingOptions
}
export interface ImportExcelOptions {
onlyFirst: Boolean, // only first sheet
XLSXReadOptions: ParsingOptions
}
ps:
以上內(nèi)容僅為自己的學(xué)習(xí)過程穿撮,歡迎大家取其精華,丟其糟粕痪欲。若對以上內(nèi)容有不同簡介或看法悦穿,歡迎一起探討,歡迎提Issues业踢。