React 之导入 Excel

5 篇文章 0 订阅
2 篇文章 0 订阅

前言:

 最近做的后台管理系统,有一个导入 Excel 的功能,从网上查找了一些别人写的代码,直接借鉴过来,希望可以帮助到你。

废话不多,直接上代码

第一步:

安装 xlsx 插件

yarn add xlsx

第二步:

使用 Antd 的上传组件,完成页面样式,主要代码

<Upload {...uploadProps}>
    <Button type="primary">导入工资单</Button>
</Upload>
const uploadProps = {
    accept: ".xls,.xlsx,application/vnd.ms-excel",
    beforeUpload: (file: any) => {
      const f = file;
      const reader = new FileReader();
      reader.onload =  (e: any) => {
        const datas = e.target.result;
        const workbook = XLSX.read(datas, {
          type: 'binary'
        });
        const first_worksheet = workbook.Sheets[workbook.SheetNames[0]];
        const jsonArr = XLSX.utils.sheet_to_json(first_worksheet, { header: 1 });
        handleImpotedJson(jsonArr, file);
      };
      reader.readAsBinaryString(f);
      return false;
    },
    onRemove: () => {
      setWageTableData([]);
    }
  }

const handleImpotedJson = (jsonArr: any, file: any) => {
    jsonArr.splice(0, 1); // 去掉表头
    const jsonArrData = jsonArr.map((item: any, index: number) => {
      let jsonObj: any = {};
      jsonObj.index = index + 1;
      jsonObj.key = 'user-wage-' + index;
      item.forEach((im: any, i: number) => {
        jsonObj[tableColumns[i].dataIndex] = im;
      })
      return jsonObj;
    });
    setWageTableData(jsonArrData)
  }

Excel 文件数据如下:

最后贴上完整代码:

import React, { useState } from 'react';
import { Upload, Button } from 'antd';
import * as XLSX from 'xlsx';
const tableColumns = [
  {
    title: '项',
    dataIndex: 'index',
  },
  {
    title: '员工ID',
    dataIndex: 'userId',
  },
  {
    title: '员工姓名',
    dataIndex: 'userName'
  },
  {
    title: '应发工资',
    dataIndex: 'userPayable',
  },
  {
    title: '绩效奖金',
    dataIndex: 'performanceBonus'
  },
  {
    title: '社保扣除',
    dataIndex: 'socialSecurityDeduction'
  },
  {
    title: '个税扣除',
    dataIndex: 'taxDeduction'
  },
  {
    title: '实发工资',
    dataIndex: 'userPaidWages',
  },
]

const WageManage = () => {
    const [wageTableData, setWageTableData] = useState<any[]>([]);
    const uploadProps = {
        accept: ".xls,.xlsx,application/vnd.ms-excel",
        beforeUpload: (file: any) => {
            const f = file;
            const reader = new FileReader();
            reader.onload =  (e: any) => {
                const datas = e.target.result;
                const workbook = XLSX.read(datas, {
                type: 'binary'
            });
            const first_worksheet = workbook.Sheets[workbook.SheetNames[0]];
            const jsonArr = XLSX.utils.sheet_to_json(first_worksheet, { header: 1 });
            handleImpotedJson(jsonArr, file);
        };
        reader.readAsBinaryString(f);
            return false;
        },
        onRemove: () => {
            setWageTableData([]);
        }
    }
  
    const handleImpotedJson = (jsonArr: any, file: any) => {
        jsonArr.splice(0, 1); // 去掉表头
        const jsonArrData = jsonArr.map((item: any, index: number) => {
            let jsonObj: any = {};
            jsonObj.index = index + 1;
            jsonObj.key = 'user-wage-' + index;
            item.forEach((im: any, i: number) => {
                jsonObj[tableColumns[i].dataIndex] = im;
            })
            return jsonObj;
        });
        setWageTableData(jsonArrData)
    }
    return (
        <div>
            <Upload {...uploadProps}>
              <Button type="primary">导入工资单</Button>
            </Upload>
            <Table
              columns={tableColumns}
              dataSource={wageTableData}
            />
        </div>    
    )
}

如果帮到你,请帮忙点个赞哦~谢谢~

Cikayo个人博客icon-default.png?t=LA92https://www.cikayo.com/

  • 13
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值