效果图:

导入:

react 基于Ant Desgin Upload简单实现导入 导出功能-LMLPHP

导出:

react 基于Ant Desgin Upload简单实现导入 导出功能-LMLPHP

导入代码:

    const propsConfig = {
    name: 'file',
    action: importDataExcelApi,   //后端接口
    headers: {
        authorization: 'authorization-text',
        loginUserId: sessionStorage.getItem('userLogin')
            ? JSON.parse(sessionStorage.getItem('userLogin')).userId : null
    },
    onChange(info) {
        if (info.file.status !== 'uploading') {
            console.log("++++++", info.file, "-------------" + info.fileList);
            console.log(info.file.response.success);
        }
        if (info.file.status === 'done') {
            message.success(`${info.file.name} 文件上传成功!`).then(r => {
            });
            importDataRef.current(); // 调用 importData 方法
            console.warn(sessionStorage.getItem('userLogin'))
        } else if (info.file.status === 'error') {
            // 不再触发 importDataExcelApi,因为文件格式有误
            message.error('文件格式有误,导入失败!').then(r => {
            });
        }
    },
};


const isUpload = () => {
        Modal.warn({
            title: '重新导入',
            content: (
                <div style={{width: '500px'}}>
                    <div style={{marginBottom: '20px'}}>
                        点击上传 会重新导入文件数据
                    </div>
                    <Upload
                        {...propsConfig}
                        direction="vertical" maxCount={1}
                        showUploadList={false}
                    >
                        <Button
                            onClick={againImport}
                            icon={<UploadOutlined/>}>
                            上传
                        </Button>
                    </Upload>
                </div>
            ),
            onOk() {
                // 在弹窗点击确认后执行的操作

            },
            okText: '确定', // 修改确定按钮的文字
            // style: {width: '1000px', height: '500px'}, // 设置宽度和高度
        });
    };


           <Button type="primary" onClick={isUpload} icon={<UploadOutlined/>}
                    >
                        重新导入
                    </Button>



导出代码

                 


const config2 = {
    title: '导出配置人员信息',
    content: (
        <>
            <ReachableContext.Consumer>{() => `是否要导出配置人员信息`}</ReachableContext.Consumer>
        </>
    ),
}






    const exportData = () => {
        exportDataExcel(screeningDate).then((res) => {
            if (res.data === 'false') {
                message.error("导出失败").then(r => {})
                return;
            }
            console.log('Export response:', res);
            //设置下载文件类型为xlsx 不同的类型type也不一样,创建URL对象
            let url = window.URL.createObjectURL(new Blob([res],
                {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'}))
            // 创建A标签
            let link = document.createElement('a')
            link.style.display = 'none'
            link.href = url
            // 设置的下载文件文件名
            const fileName = "配置人员信息";
            // 触发点击方法
            link.setAttribute('download', fileName)
            document.body.appendChild(link)
            link.click()
            message.success("导出成功").then(r => {
            });

        });
    }




  <Button type="primary" style={{marginBottom: '30px'}}
                                        onClick={async () => {
                                            const confirmed = await modal.confirm(config2);
                                            if (confirmed) {
                                                // 调用另一个方法
                                                exportData();
                                            }
                                        }}
                                >
                                    导出
                                </Button>
01-07 08:03