add read excel file
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
import { Button, Upload } from 'antd';
|
||||
import { UploadOutlined } from '@ant-design/icons';
|
||||
import { FC } from 'react';
|
||||
import { readExcelFile } from '../utils/excel';
|
||||
|
||||
const DutyTablePage: FC = () => {
|
||||
return (
|
||||
<Upload
|
||||
name='file'
|
||||
action=''
|
||||
beforeUpload={(file) => {
|
||||
let data = readExcelFile(file, 0);
|
||||
data
|
||||
.then((d) => console.log('read data', d))
|
||||
.catch((e) => console.log('got error', e));
|
||||
return false;
|
||||
}}
|
||||
>
|
||||
<Button icon={<UploadOutlined />} />
|
||||
</Upload>
|
||||
);
|
||||
};
|
||||
|
||||
export default DutyTablePage;
|
||||
+2
-1
@@ -1,5 +1,6 @@
|
||||
import { Routes, Route } from 'react-router-dom';
|
||||
import Todo from './components/Todo';
|
||||
import DutyTablePage from './pages/DutyTable';
|
||||
|
||||
export const routes = {
|
||||
main: '/',
|
||||
@@ -25,7 +26,7 @@ export const AppRouter: React.FC = () => {
|
||||
return (
|
||||
<Routes>
|
||||
<Route path={routes.main} element={<Todo />} />
|
||||
<Route path={routes.admin.dutyTable} element={<Todo />} />
|
||||
<Route path={routes.admin.dutyTable} element={<DutyTablePage />} />
|
||||
<Route path={routes.admin.userControl} element={<Todo />} />
|
||||
<Route path={routes.admin.dutyInfo} element={<Todo />} />
|
||||
<Route path='/duty/hi1' element={<Todo />} />
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
import { read as xlsxRead, utils as xlsxUtils } from 'xlsx';
|
||||
import { readFile } from './readfile';
|
||||
|
||||
export async function readExcelFile(file: File, sheetIndex: number) {
|
||||
let data = await readFile(file);
|
||||
let workbook = xlsxRead(data, { type: 'binary' });
|
||||
let worksheet = workbook.Sheets[workbook.SheetNames[sheetIndex]];
|
||||
data = xlsxUtils.sheet_to_json(worksheet);
|
||||
return data;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
export function readFile(file: File) {
|
||||
return new Promise((resolve) => {
|
||||
let reader = new FileReader();
|
||||
reader.readAsBinaryString(file);
|
||||
reader.onload = (ev) => {
|
||||
resolve(ev.target?.result);
|
||||
};
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user