React 纯前端导出Excel (2)

之前写过一篇关于纯前端导出excel的文章 但是之前用的那个依赖不兼容ie 现在重新发表一个兼容ie的;

1.安装依赖  npm install xlsx

2引用公共方法 ,方法已经封装,直接禁用即可,

公共方法

/**
 * 纯前端导出Excel工具
 */
import * as XLSX from "xlsx";
import {saveAs} from '@progress/kendo-file-saver';

export function formatJson(filterVal, jsonData) {
    return jsonData.map(v => filterVal.map(j => v[j]))
}

function s2ab(s) {
    let buf = new ArrayBuffer(s.length)
    let view = new Uint8Array(buf)
    for (let i = 0; i !== s.length; ++i) {
        view[i] = s.charCodeAt(i) & 0xFF
    }
    return buf
}

function data2ws(data) {
    const ws = {}
    const range = {s: {c: 10000000, r: 10000000}, e: {c: 0, r: 0}}
    for (let R = 0; R !== data.length; ++R) {
        for (let C = 0; C !== data[R].length; ++C) {
            if (range.s.r > R) range.s.r = R
            if (range.s.c > C) range.s.c = C
            if (range.e.r < R) range.e.r = R
            if (range.e.c < C) range.e.c = C
            const cell = {v: data[R][C]}
            if (cell.v == null) continue
            const cellRef = XLSX.utils.encode_cell({c: C, r: R})
            if (typeof cell.v === 'number') cell.t = 'n'
            else if (typeof cell.v === 'boolean') cell.t = 'b'
            else if (cell.v instanceof Date) {
                cell.t = 'n'
                cell.z = XLSX.SSF._table[14]
            } else {
                cell.t = 's'
            }
            ws[cellRef] = cell
        }
    }
    if (range.s.c < 10000000) {
        ws['!ref'] = XLSX.utils.encode_range(range)
    }
    return ws
}

function Workbook() {
    if (!(this instanceof Workbook)) {
        return new Workbook()
    }
    this.SheetNames = []
    this.Sheets = {}
}

/*
* th => 表头
* data => 数据
* fileName => 文件名
* fileType => 文件类型
* sheetName => sheet页名
*/

//导出封装好的方法
export function toExcel({th, data, fileName, fileType, sheetName}) {
    data.unshift(th)
    const wb = new Workbook()
    const ws = data2ws(data)
    sheetName = sheetName || 'sheet1'
    wb.SheetNames.push(sheetName)
    wb.Sheets[sheetName] = ws
    fileType = fileType || 'xlsx'
    var wbout = XLSX.write(wb, {bookType: fileType, bookSST: false, type: 'binary'})
    fileName = fileName || '列表'
    saveAs(new Blob([s2ab(wbout)], {type: 'application/octet-stream'}), `${fileName}.${fileType}`)
}

如何引用:

 downloadData = () => {
        let {chooseData, searchValue} = this.state;
        if(chooseData.length === 0) {
            message.warning('无数据可导出');
            return;
        }
        const th = ["行号", "物料代码", "物料描述", "计量单位", "价格单位", "合同可用余额", "采购申请号", "含税单价", "行数量", "行未下单数量", "订单数量", "合同创建人", "销售人员", "计价方式", "采购申请行号", "WBS", ];
        const filterVal = ["contractLineNumNew", "materialCode", "materialRemark", "measureUnitName", "priceUnit", "availabMoney", "purchaseApplyNumber", "purchasePriceInclusive", "amount", "unorderCount", "orderCount", "creatorName", "employeeName", "valMethodName", "purchaseApplyLineNumber", "wbsNo", ];
        const data = formatJson(filterVal, chooseData);
        toExcel({th, data, fileName: `${searchValue.contractCode} 合同数据`, fileType: "xlsx", sheetName: `${searchValue.contractCode} 合同数据`})
    };

这里面chooseData就是table的数据,各位仿照这个使用即可

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章