docs.sheetjs.com/docz/static/mui/table/App.tsx

48 lines
1.4 KiB
TypeScript
Raw Normal View History

2024-06-13 04:31:40 +00:00
import { TableContainer, Table, TableRow, TableCell, TableBody, TableHead } from '@mui/material';
import { WorkBook, WorkSheet, read, utils, writeFileXLSX } from "xlsx";
import { useRef, useState, useEffect } from "react";
2023-05-11 06:17:10 +00:00
2024-06-13 04:31:40 +00:00
import './App.css'
2023-05-11 06:17:10 +00:00
2024-06-13 04:31:40 +00:00
interface President {
Name: string;
Index: number;
}
2023-05-11 06:17:10 +00:00
2024-06-13 04:31:40 +00:00
function App() {
2023-05-11 06:17:10 +00:00
const tbl = useRef<HTMLTableElement>(null);
2024-06-13 04:31:40 +00:00
const xport = () => {
const wb: WorkBook = utils.table_to_book(tbl.current);
writeFileXLSX(wb, "SheetJSMaterialUI.xlsx");
};
const [pres, setPres] = useState<President[]>([]);
useEffect(() => {
let active = true;
(async() => {
const ab: ArrayBuffer = await (await fetch("https://docs.sheetjs.com/pres.xlsx")).arrayBuffer();
const wb: WorkBook = read(ab);
const ws: WorkSheet = wb.Sheets[wb.SheetNames[0]];
const aoo: President[] = utils.sheet_to_json<President>(ws);
if(active) setPres(aoo);
})();
return () => { active = false };
}, []);
2023-05-11 06:17:10 +00:00
return ( <>
2024-06-13 04:31:40 +00:00
<button onClick={xport}>Export</button>
<TableContainer><Table ref={tbl}>
<TableHead><TableRow><TableCell>Name</TableCell><TableCell>Index</TableCell></TableRow></TableHead>
<TableBody>
{pres.map((row, idx) => (
<TableRow key={idx}>
<TableCell>{row.Name}</TableCell>
<TableCell>{row.Index}</TableCell>
</TableRow>
))}
</TableBody>
</Table></TableContainer>
2023-05-11 06:17:10 +00:00
</> );
2024-06-13 04:31:40 +00:00
}
export default App