add
This commit is contained in:
parent
91e45f523f
commit
a022912ad0
|
@ -1,11 +1,14 @@
|
|||
import { Button, Select, Table, Modal, Form, Input, InputNumber } from "antd";
|
||||
import { MagnifyingGlassIcon } from "@heroicons/react/24/outline";
|
||||
import { Button, Select, Table, Modal, Form, Input, InputNumber, Upload, message } from "antd";
|
||||
import { MagnifyingGlassIcon, ArrowUpTrayIcon, ArrowDownTrayIcon } from "@heroicons/react/24/outline";
|
||||
import { useEffect, useState, useCallback, useContext } from "react";
|
||||
import toast from "react-hot-toast";
|
||||
import React from "react";
|
||||
import _ from "lodash";
|
||||
import { useMainContext } from "../layout/MainProvider";
|
||||
import { EditableRow, EditableContext, EditableCell } from '../sport/context/EditableContext';
|
||||
import * as XLSX from 'xlsx';
|
||||
import { UploadOutlined, DownloadOutlined } from '@ant-design/icons';
|
||||
|
||||
export default function SportPage() {
|
||||
const { form, setVisible, searchValue, setSearchValue } = useMainContext();
|
||||
const { editingRecord, setEditingRecord } = useMainContext();
|
||||
|
@ -326,6 +329,61 @@ export default function SportPage() {
|
|||
},
|
||||
};
|
||||
|
||||
// 导出成绩为Excel
|
||||
const handleExport = () => {
|
||||
// 创建工作簿
|
||||
const wb = XLSX.utils.book_new();
|
||||
|
||||
// 准备导出数据(移除id字段)
|
||||
const exportData = sportsData.map(({ id, ...rest }) => rest);
|
||||
|
||||
// 转换为工作表
|
||||
const ws = XLSX.utils.json_to_sheet(exportData);
|
||||
|
||||
// 将工作表添加到工作簿
|
||||
XLSX.utils.book_append_sheet(wb, ws, "成绩数据");
|
||||
|
||||
// 生成Excel文件并下载
|
||||
XLSX.writeFile(wb, "体育成绩表.xlsx");
|
||||
|
||||
toast.success("成绩导出成功");
|
||||
};
|
||||
|
||||
// 导入成绩
|
||||
const handleImport = (file) => {
|
||||
const reader = new FileReader();
|
||||
|
||||
reader.onload = (e) => {
|
||||
try {
|
||||
const data = new Uint8Array(e.target.result);
|
||||
const workbook = XLSX.read(data, { type: 'array' });
|
||||
// 获取第一个工作表
|
||||
const worksheet = workbook.Sheets[workbook.SheetNames[0]];
|
||||
// 转换为JSON
|
||||
const jsonData = XLSX.utils.sheet_to_json(worksheet);
|
||||
// 添加id字段
|
||||
const importedData = jsonData.map((item, index) => ({
|
||||
id: sportsData.length + index + 1,
|
||||
...item,
|
||||
// 确保totalScore是计算所有项目成绩的和
|
||||
totalScore: calculateTotalScore(item)
|
||||
}));
|
||||
|
||||
// 更新数据
|
||||
setSportsData([...sportsData, ...importedData]);
|
||||
toast.success(`成功导入${importedData.length}条记录`);
|
||||
} catch (error) {
|
||||
console.error('导入失败:', error);
|
||||
toast.error("导入失败,请检查文件格式");
|
||||
}
|
||||
};
|
||||
|
||||
reader.readAsArrayBuffer(file);
|
||||
|
||||
// 防止自动上传
|
||||
return false;
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="p-2 min-h-screen bg-gradient-to-br">
|
||||
<Form>
|
||||
|
@ -341,13 +399,28 @@ export default function SportPage() {
|
|||
/>
|
||||
<MagnifyingGlassIcon className="absolute left-3 top-1/2 transform -translate-y-1/2 h-5 w-5 " />
|
||||
</div>
|
||||
<Button
|
||||
type="primary"
|
||||
onClick={handleNew}
|
||||
className="font-bold py-2 px-4 rounded"
|
||||
>
|
||||
新建成绩
|
||||
</Button>
|
||||
<div className="flex space-x-2">
|
||||
<Upload
|
||||
beforeUpload={handleImport}
|
||||
showUploadList={false}
|
||||
accept=".xlsx,.xls"
|
||||
>
|
||||
<Button icon={<UploadOutlined />}>导入成绩</Button>
|
||||
</Upload>
|
||||
<Button
|
||||
icon={<DownloadOutlined />}
|
||||
onClick={handleExport}
|
||||
>
|
||||
导出成绩
|
||||
</Button>
|
||||
<Button
|
||||
type="primary"
|
||||
onClick={handleNew}
|
||||
className="font-bold py-2 px-4 rounded"
|
||||
>
|
||||
新建成绩
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{isLoading ? (
|
||||
|
|
Loading…
Reference in New Issue