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的數據,各位仿照這個使用即可

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