react生成excel

import * as XLSX from 'xlsx'
import { saveAs } from 'file-saver'; 

 const downloadTemplate = () => {
        // 创建Workbook
        const workbook = XLSX.utils.book_new();

        // 创建Worksheet
        const worksheet = XLSX.utils.aoa_to_sheet([
            ['地区', '通道类别', '_10度', '0度', '5度', '25度', '40度', '45度', '65度', '暂用', '拥有', '已使用', '剩余', '占用率'],
            // ['深圳', '5V/10A', '111', '10', '15', '13', '12', '11', '6', '5', '4', '3', '2', '0.5'],
            // ['广州', '20V/10A', '222', '100', '105', '103', '102', '101', '50', '94', '93', '92', '60', '0.15'],
        ]);

        // 将Worksheet添加到Workbook
        XLSX.utils.book_append_sheet(workbook, worksheet, 'Sheet1');

        // 生成Excel文件的二进制数据
        const excelData = XLSX.write(workbook, { type: 'binary', bookType: 'xlsx' });

        // 转换二进制数据为Blob对象
        const excelBlob = new Blob([s2ab(excelData)], { type: 'application/octet-stream' });

        // 保存Excel文件
        saveAs(excelBlob, '通道资源原始数据-模板.xlsx');
    };

    // 字符串转ArrayBuffer
    const s2ab = (s) => {
        const buf = new ArrayBuffer(s.length);
        const view = new Uint8Array(buf);
        for (let i = 0; i < s.length; i++) {
            view[i] = s.charCodeAt(i) & 0xff;
        }
        return buf;
    };

你可能感兴趣的:(react.js,excel,javascript)