js通过Excel导出数据

184 阅读1分钟

1.下载插件

"js-export-excel": "^1.0.10"

2.引入插件

import ExportJsonExcel from 'js-export-excel';

3.使用插件

<div onClick={() => this.ExportJsonExcel()}><div>

ExportJsonExcel = () => {
    let {items} = this.props;
    if (items && items.length > 0) {
        items.map((item, index) => {
            item.status = LM_ITEM_STATUS[item.status];
    })
        let option = {};
        option.fileName = '导购商品组数据';
        option.datas = [
            {
                sheetData: items,
                sheetFilter: ['lmItemId', 'itemId', 'itemName', 'status'],
                sheetHeader: ['LM商品ID', '商品ID', '商品名称', '商品状态'],
                columnWidths: [15, 10, 20, 10]
            }
        ];
        if (items != null) {
            const toExcel = new ExportJsonExcel(option);
            toExcel.saveExcel();
            Toast.success("导出Excel成功");
        }
    } else {
        Toast.eerror("导出Excels失败");
    }
  }