js-export-excel(純js 支持中文) ES6 module
當(dāng)頁面需要導(dǎo)出簡單的excel時(shí),如果需要純前端實(shí)現(xiàn)羡洛,可以使用js-export-excel
首先在項(xiàng)目當(dāng)前目錄下安裝 npm install js-export-excel
其次在需要導(dǎo)出表格的頁面引入該組件 import ExportJsonExcel from “js-export-excel”;
再然后就可以開始寫代碼了圆兵,以下為具體實(shí)現(xiàn)內(nèi)容:
// 點(diǎn)擊按鈕尝苇,在render中
<Button onClick={this.downloadExcel}> 導(dǎo)出excel表格 </Button>
// 調(diào)用下載excel方法
const downloadExcel = () => {
// dataHistory 是列表數(shù)據(jù)
var option = {};
var dataTable = [];
if (dataHistory) {
for (let i in dataHistory) {
if (dataHistory) {
let obj = {
價(jià)格: dataHistory[i].price,
數(shù)量: dataHistory[i].count,
累計(jì)金額: dataHistory[i].amounts,
時(shí)間: dataHistory[i].times,
};
dataTable.push(obj);
}
}
}
option.fileName = '歷史成交';
option.datas = [
{
sheetData: dataTable,
sheetName: 'sheet',
sheetFilter: ['價(jià)格', '數(shù)量', '累計(jì)金額', '時(shí)間'],
sheetHeader: ['價(jià)格', '數(shù)量', '累計(jì)金額', '時(shí)間'],
},
];
const toExcel = new ExportJsonExcel(option); // new
toExcel.saveExcel();
};