I used react-excel-renderer
to get data from an excel file. The excel file has data like the following image:
In the frontend, I got json data
like this:
How do I make group them as one based on the accId
? I plan to have a table to render their id
and name
and there will button to view the rest of the data.
import React, { useState } from "react";
import { Table, Button, message, Upload } from "antd";
import { ExcelRenderer } from "react-excel-renderer";
export const ExcelPageMod = () => {
const [selected, setSelected] = useState([]);
const [cols, setCols] = useState([]);
const [rows, setRows] = useState([]);
const { Column } = Table;
const fileHandler = (fileList) => {
let fileObj = fileList;
if (!fileObj) {
message.error("No file uploaded!");
return false;
}
console.log("fileObj.type:", fileObj.type);
if (
!(
fileObj.type === "application/vnd.ms-excel" ||
fileObj.type ===
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
)
) {
message.error("Unknown file format. Only Excel files are uploaded!");
}
ExcelRenderer(fileObj, (err, resp) => {
if (err) {
console.log(err);
} else {
let newRows = [];
resp.rows.slice(1).map((row, index) => {
if (row && row !== "undefined") {
newRows.push({
key: index,
accId: row[0],
accName: row[1],
productClass: row[2],
accYearPrev: row[3],
accYearCurr: row[4],
});
}
});
if (newRows.length === 0) {
message.error("No data found in file!");
return false;
} else {
console.log(newRows);
console.log(resp)
setCols(resp.cols);
setRows(newRows);
}
}
});
return false;
};
const handleDownload = (key) => {
const selectedRow = [...rows];
console.log(selectedRow[key]);
setSelected(selectedRow[key]);
};
return (
<div style={{ padding: "20px" }}>
<h1>Production PDF Generator</h1>
<div>
<Upload
name="file"
multiple={false}
beforeUpload={fileHandler}
onRemove={() => setRows([])}
>
<Button type="primary">Upload</Button>
</Upload>
</div>
<div style={{ marginTop: 20 }}>
<Table dataSource={rows}>
<Column title="ID" dataIndex="accId" key="accId" />
<Column title="Name" dataIndex="accName" key="accName" />
<Column title="Product Class" dataIndex="productClass" key="productClass" />
<Column
title="Year Prev"
dataIndex="accYearPrev"
key="accYearPrev"
render={(accYearPrev) => {
return (
<>{"Rp. " + parseFloat(accYearPrev).toLocaleString("id")}</>
);
}}
/>
<Column
title="Year Curr"
dataIndex="accYearCurr"
key="accYearCurr"
render={(accYearCurr) => {
return (
<>{"Rp. " + parseFloat(accYearCurr).toLocaleString("id")}</>
);
}}
/>
<Column
title="Action"
key="action"
render={(text, record) => (
<Button type="primary" onClick={() => handleDownload(record.key)}>
Get Data
</Button>
)}
/>
</Table>
</div>
</div>
);
};
Edit: Here's is the JSON I want
{
"accId": "ABC001",
"accName": "John Doe",
"production": [
{ "productClass": "General", "accYearPrev": 2000, "accYearCurr": 2500 },
{
"productClass": "Engineering",
"accYearPrev": 7000,
"accYearCurr": 5500
}
]
},
{
"accId": "ABC002",
"accName": "Jane Doe",
"production": [
{ "productClass": "General", "accYearPrev": 2000, "accYearCurr": 2500 },
{
"productClass": "Engineering",
"accYearPrev": 7000,
"accYearCurr": 5500
},
{ "productClass": "Marine", "accYearPrev": 7000, "accYearCurr": 5500 }
]
}