Ant-Design-Pro-V5: ProTable前端导出excel表格。

这篇具有很好参考价值的文章主要介绍了Ant-Design-Pro-V5: ProTable前端导出excel表格。。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Prtable表格中根据搜索条件实现excel表格导出。
antdesign table 导出,Ant Design,前端,react.js
代码展示:

index.jsx

import React, { useRef, useState, Fragment, useEffect } from 'react';
import { getLecturerList, lecturerExportExcel } from '@/services/train/personnel';
import { getOrgList, getSelectType } from '@/services/globalServices';
import { PlusOutlined, EditTwoTone, ExportOutlined } from '@ant-design/icons';
import { PageContainer, ProTable, ProFormSelect } from '@ant-design/pro-components';
import { Button, message, Form, Space, Tooltip, Spin } from 'antd';
import { FormattedMessage } from 'umi';

const InsideLecturer = () => {
  const [orgList, setOrgList] = useState([]); // 管理机构
  const [downloadParams, setDownloadParams] = useState({}); //导出参数
  const [downloadTotal, setDownloadTotal] = useState({}); //导出数量总数以及页码
  const [exportExcelLoading, setExportExcelLoading] = useState(false); //导出loading
  const actionRef = useRef(); // 表格组件ProTable ref
  const [form] = Form.useForm(); // 创建表单
  const formRef = useRef();
  const [currentRow, setCurrentRow] = useState(); // 当前行数据
  const [teacherstateArray, setTeacherstateArray] = useState([]); // 在岗状态
  const [teacherstateObj, setTeacherstateObj] = useState({}); // 在岗状态
  const [teacherTypeArray, setTeacherTypeArray] = useState([]); // 讲师类型
  const [teacherTypeObj, setTeacherTypeObj] = useState({}); // 讲师类型
  const [idtypeArray, setIdtypeArray] = useState([]); // 证件类型

  const teacherType = 2; // 内勤讲师类型
  useEffect(() => {
    getSelectTypeFun();
  }, []);
  const getSelectTypeFun = async () => {
    await getSelectType('teacherstate').then((res) => {
      if (res && res.data) {
        setTeacherstateArray(res.data);
        let Obj = {};
        res.data.forEach((item) => {
          Obj[item.code] = item.codeName;
        });
        setTeacherstateObj(Obj);
      }
    });
    await getSelectType('teachertype').then((res) => {
      if (res && res.data && res.data.length > 0) {
        let removedArr = res.data.filter((item) => {
          return item.code != '1' && item.code != '3';
        });
        setTeacherTypeArray(removedArr);
        let Obj = {};
        removedArr.forEach((item) => {
          Obj[item.code] = item.codeName;
        });
        setTeacherTypeObj(Obj);
      }
    });
    await getSelectType('idnotype').then((res) => {
      if (res && res.data) {
        setIdtypeArray(res.data);
      }
    });
  };
  const fieldNames = {
    label: 'codeName',
    value: 'code',
  };
  const columns = [
    {
      title: '姓名',
      dataIndex: 'name',
    },
    {
      title: '讲师类别',
      dataIndex: 'teacherType',
      valueType: 'select',
      fieldProps: {
        options: teacherTypeArray,
        fieldNames,
      },
    },
    {
      title: '证件号码',
      dataIndex: 'idNo',
    },
    {
      title: '工号',
      dataIndex: 'staffCode',
      search: false,
      editable: false,
    },
    {
      title: '讲师状态',
      dataIndex: 'state',
      valueType: 'select',
      fieldProps: {
        options: teacherstateArray,
        fieldNames,
      },
    },
    {
      title: '移动电话',
      dataIndex: 'mobile',
      search: false,
      width: 150,
    },

    {
      title: <FormattedMessage id="pages.searchTable.titleOption" />,
      dataIndex: 'option',
      valueType: 'option',
      width: 100,
      render: (_, record) => [
        <div key={record.staffCode}>
          <Fragment>
            <a
              onClick={() => {
                setCurrentRow(record);
              }}
            >
              <EditTwoTone />
              <FormattedMessage id="pages.searchTable.edit" />
            </a>
            <span style={{ marginRight: '10px' }}></span>
          </Fragment>
        </div>,
      ],
    },
  ];

  return (
    <Spin spinning={exportExcelLoading} tip="数据导出中...">
      <PageContainer breadcrumb={false}>
        <ProTable
          headerTitle="查询列表"
          rowKey={'staffCode'}
          actionRef={actionRef}
          options={false}
          formRef={formRef}
          search={{
            labelWidth: 120,
          }}
          params={{
            teacherType: 2,
          }}
          pagination={{
            pageSize: 10,
          }}
          tableAlertOptionRender={() => {
            return false;
          }}
          toolBarRender={() => [
            <Tooltip placement="topLeft" title="点击导出内容">
              <Button
                type="default"
                key="export"
                loading={exportExcelLoading}
                onClick={() => {
                  lecturerExportExcel({
                    downloadTotal,
                    downloadParams,  //  ...formRef.current?.getFieldsValue(), 
                    orgObj2,
                    teacherstateObj,
                    teacherType,
                    teacherTypeObj,
                    setExportExcelLoading,
                  });
                }}
              >
                <ExportOutlined /> 导出数据
              </Button>
            </Tooltip>,
          ]}
          revalidateOnFocus={false}
          onSubmit={(params) => {
            // 导出参数设置
            setDownloadParams(params);
          }}
          onReset={() => setDownloadParams(undefined)}
          request={(params) => {
            const res = getLecturerList({ ...params });
            res.then((value) => {
              // 设置导出总数以及页码
              setDownloadTotal({
                current: 1,
                pageSize: value?.total || 100000,
              });
            });
            return res;
          }}
          columns={columns}
        />
      </PageContainer>
    </Spin>
  );
};

export default InsideLecturer;

数据字典格式返回:
antdesign table 导出,Ant Design,前端,react.js
以 teacherTypeObj 为例 向services.js中 lecturerExportExcel 方法中传入的格式为:
antdesign table 导出,Ant Design,前端,react.js

services.js中 lecturerExportExcel 导出方法:

import { request } from 'umi';
import { paramsFilter } from '../../utils/utils';
import ExportJsonExcel from 'js-export-excel';
import { message } from 'antd';


export async function lecturerExportExcel(params) {
  const { downloadTotal, downloadParams, orgObj2, teacherstateObj, teacherType, teacherTypeObj, setExportExcelLoading } = params;
  setExportExcelLoading(true);
  // 参数合并
  let newParams = Object.assign(downloadTotal, downloadParams);
  newParams.teacherType = teacherType;
  const result = await request('/api/Info/list2', {
    params: paramsFilter(newParams),
  });
  // 根据实际情况注释不需要导出的字段
  const allowExportColumns = [
    'manageCom',
    'name',
    'staffCode',
    'idNo',
    'mobile',
    "state",
    "teacherType"
  ];
  const TableItemObject = {
    manageCom: "管理机构",
    name: "姓名",
    staffCode: "工号",
    idNo: "证件号码",
    mobile: "移动电话",
    state: "讲师状态",
    teacherType: '讲师类别'
  };

  const cnColumns = allowExportColumns.map(kk => TableItemObject[kk]);
  if (Array.isArray(result?.data) && result?.data.length > 0) {
    const tableData = [];
    result?.data.forEach((item) => {
      const kv = {};
      Object.keys(item).map(vv => {
        if (allowExportColumns.includes(vv)) {
          // 数据字典字段汉化
          if (vv === 'manageCom') {
            kv[TableItemObject[vv]] = orgObj2[item[vv]] || '';
          } else if (vv === 'state') {
            kv[TableItemObject[vv]] = teacherstateObj[item[vv]] || '';
          } else if (vv === 'teacherType') {
            kv[TableItemObject[vv]] = teacherTypeObj[item[vv]] || '';
          } else {
            // 不用汉化直接输出
            kv[TableItemObject[vv]] = item[vv];
          }
        }
      })
      tableData.push(kv);
    });
    const option = {
      fileName: "讲师信息",
      datas: [
        {
          sheetData: tableData,// 数据
          sheetName: 'sheet1',
          sheetFilter: cnColumns,// 表头
          sheetHeader: cnColumns,// 表头
        }
      ]
    }
    const toExcel = new ExportJsonExcel(option);
    toExcel.saveExcel();
    setExportExcelLoading(false);
    return true;
  }
  message.error('数据为空!');
  setExportExcelLoading(false);
  return false;
}

utils 中 paramsFilter 方法:过滤无用参数文章来源地址https://www.toymoban.com/news/detail-701167.html

// 接口请求参数中 删除对象中值为空或null或undefined或者为空的所有属性
export function paramsFilter(requestParams) {
  let newParams = Object.keys(requestParams)
    .filter((key) => requestParams[key] !== null && requestParams[key] !== undefined && requestParams[key] !== '')
    .reduce((acc, key) => ({ ...acc, [key]: requestParams[key] }), {});
  return newParams;
}

到了这里,关于Ant-Design-Pro-V5: ProTable前端导出excel表格。的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • [绍棠] Ant Design Pro of Vue打包有前缀静态资源访问不到

    缺点:需要和部署的路径保持一致,不是很灵活 1、在环境变量.env中定义url前缀 2、定义vue路由前缀路径router/index.js 3、vue配置公共路径前缀vue.config.js 4、打包部署到nginx或其他中间件,此时要保证前缀和部署的前缀保持一致 nginx 1、使用history模式 2、定义vue路由前缀路径rout

    2024年02月11日
    浏览(42)
  • ant design pro v5 - 03 动态菜单 动态路由(配置路由 动态登录路由 登录菜单)

    1 动态菜单         技术思路:配置路由,用户登录后根据用户信息获取后台菜单。 2 动态路由+动态菜单         技术思路: 使用umijs的运行时修改路由 patchRoutes({ routes })  UMIJS 参考文档 ,react umi 没有守护路由的功能 直接在 app.tsx  的 layout 下的 childrenRender 添加守护路

    2023年04月09日
    浏览(25)
  • ant-design-pro-cli 运行pro create myapp报错Error [ERR_REQUIRE_ESM]: require() of ES Module是什么原因?

    根据官方文档全局安装了npm i @ant-design/pro-cli -g,然后运行pro create myapp,命令行出现: 这是什么原因导致的?

    2024年02月12日
    浏览(35)
  • 前端实现真实可动态变化进度条,axios+ Ant Design Vue实现.

    最近有一个新需求,要求在前端实现真实的进度条展示,我首先想到了  Ant Design Vue的upload组件, 在antd官网里upload组件不仅有上传功能,并且还附带了 Progress 进度条组件, 还拥有上传成功和失败的两种状态的区分,可以说是十分贴心了,如图  但是很可惜这个组件上传文件的话,你要

    2024年02月09日
    浏览(22)
  • ant design pro集成阿里低代码引擎lowcode-engine,并实现与数据库结合实现低代码模块的创建、设计、发布等功能

    阿里低代码引擎是一款为低代码平台开发者提供的,具备强大定制扩展能力的低代码设计器研发框架。 本文主要介绍如何在ant design pro项目中集成阿里低代码引擎lowcode-engine。 根据官方文档,已有项目要集成低代码引擎,至少需要三步,以下我们以ant desigin pro5项目为例,来集

    2024年02月22日
    浏览(40)
  • 【ArcGIS Pro二次开发】(56):界址点导出Excel

    界址点成果表是地籍测绘中的一种表格,用于记录地块的界址点坐标和相关属性信息。 这个工具的目的就是为了将地块要素导出为界址点成果表。 如上图所示,在【数据处理】组—【Excel相关】面板下,点击【界址点导出Excel】工具。 在弹出的工具框中,分别输入参数: 1、

    2024年02月14日
    浏览(26)
  • 前端导出word文件的多种方式、前端导出excel文件

    先看效果: 这是页面中的table 这是导出后的效果: 需要的依赖: npm 自行安装,需要看官网的具体参数自行去github上面找对应的参数 具体代码:(先看word模板,在看代码,word中的变量和代码中 doc.setData() 是一一对应的) 包依赖: 代码 导出效果: 需要的依赖: node-xlsx 代码

    2024年03月24日
    浏览(83)
  • vue 前端导出Excel表格(基础版 + 多级标题)纯前端导出

    先看效果   1、安装依赖 2、在项目的入口 main.js  引入 3、直接使用 4、完整代码直接复制即可 ------------------------------------分割-------------------------------------------- 1、还是npm下载依赖 2、要新建一个文件,Export2Excel.js 文件,我是从某个网址下载的,我给忘了,这里我直接复制过

    2024年02月12日
    浏览(42)
  • 前端实现导出Excel

    1. 创建excel文件夹 2. Blob.js 文件夹内容 ↓ Export2Excel.js 文件夹内容 ↓

    2024年02月10日
    浏览(28)
  • 前端导入导出excel记录

    前端模块的导入导出excel功能,大体分为两个逻辑。 前端使用导入组件,获取excel,交给 后端处理 前端使用导入组件,获取excel,自己 解析数据 ,然后调用数据存储的方法。 我们分别对这两种方法进行记录。 导出 组件: 方法: api: util: 导入 组件: 方法: 工具方法: 导

    2024年02月12日
    浏览(26)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包