├── .gitignore
├── README.md
├── package-lock.json
├── package.json
├── public
├── favicon.ico
├── index.html
├── logo192.png
├── logo512.png
├── manifest.json
└── robots.txt
└── src
├── App.js
├── index.css
└── index.js
/.gitignore:
--------------------------------------------------------------------------------
1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2 |
3 | # dependencies
4 | /node_modules
5 | /.pnp
6 | .pnp.js
7 |
8 | # testing
9 | /coverage
10 |
11 | # production
12 | /build
13 |
14 | # misc
15 | .DS_Store
16 | .env.local
17 | .env.development.local
18 | .env.test.local
19 | .env.production.local
20 |
21 | npm-debug.log*
22 | yarn-debug.log*
23 | yarn-error.log*
24 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # read-csv-file-react
2 | Read CSV file in React
3 |
4 | ## Documentation
5 |
6 | [https://www.cluemediator.com/read-csv-file-in-react](https://www.cluemediator.com/read-csv-file-in-react)
7 |
8 | ## Quick Start
9 |
10 | Follow the below steps to run the project.
11 |
12 | 1. Clone repository
13 | 2. Run `npm i` command to install dependencies
14 | 3. Execute `npm start` command to run the project
15 |
16 | ## Connect with us
17 |
18 | Website: [Clue Mediator](https://www.cluemediator.com)
19 | Like us on [Facebook](https://www.facebook.com/thecluemediator)
20 | Follow us on [Twitter](https://twitter.com/cluemediator)
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "read-csv-file-react",
3 | "version": "0.1.0",
4 | "private": true,
5 | "dependencies": {
6 | "react": "^16.13.1",
7 | "react-data-table-component": "^6.11.2",
8 | "react-dom": "^16.13.1",
9 | "react-scripts": "3.4.3",
10 | "styled-components": "^5.1.1",
11 | "xlsx": "^0.16.6"
12 | },
13 | "scripts": {
14 | "start": "react-scripts start",
15 | "build": "react-scripts build",
16 | "test": "react-scripts test",
17 | "eject": "react-scripts eject"
18 | },
19 | "eslintConfig": {
20 | "extends": "react-app"
21 | },
22 | "browserslist": {
23 | "production": [
24 | ">0.2%",
25 | "not dead",
26 | "not op_mini all"
27 | ],
28 | "development": [
29 | "last 1 chrome version",
30 | "last 1 firefox version",
31 | "last 1 safari version"
32 | ]
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/public/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/cluemediator/read-csv-file-react/370afcd9c5e4c44203541b9a81583397ec09e35d/public/favicon.ico
--------------------------------------------------------------------------------
/public/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
12 |
13 |
17 |
18 |
27 | React App
28 |
29 |
30 |
31 |
32 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/public/logo192.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/cluemediator/read-csv-file-react/370afcd9c5e4c44203541b9a81583397ec09e35d/public/logo192.png
--------------------------------------------------------------------------------
/public/logo512.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/cluemediator/read-csv-file-react/370afcd9c5e4c44203541b9a81583397ec09e35d/public/logo512.png
--------------------------------------------------------------------------------
/public/manifest.json:
--------------------------------------------------------------------------------
1 | {
2 | "short_name": "React App",
3 | "name": "Create React App Sample",
4 | "icons": [
5 | {
6 | "src": "favicon.ico",
7 | "sizes": "64x64 32x32 24x24 16x16",
8 | "type": "image/x-icon"
9 | },
10 | {
11 | "src": "logo192.png",
12 | "type": "image/png",
13 | "sizes": "192x192"
14 | },
15 | {
16 | "src": "logo512.png",
17 | "type": "image/png",
18 | "sizes": "512x512"
19 | }
20 | ],
21 | "start_url": ".",
22 | "display": "standalone",
23 | "theme_color": "#000000",
24 | "background_color": "#ffffff"
25 | }
26 |
--------------------------------------------------------------------------------
/public/robots.txt:
--------------------------------------------------------------------------------
1 | # https://www.robotstxt.org/robotstxt.html
2 | User-agent: *
3 | Disallow:
4 |
--------------------------------------------------------------------------------
/src/App.js:
--------------------------------------------------------------------------------
1 | import React, { useState } from 'react';
2 | import * as XLSX from 'xlsx';
3 | import DataTable from 'react-data-table-component';
4 |
5 | function App() {
6 |
7 | const [columns, setColumns] = useState([]);
8 | const [data, setData] = useState([]);
9 |
10 | // process CSV data
11 | const processData = dataString => {
12 | const dataStringLines = dataString.split(/\r\n|\n/);
13 | const headers = dataStringLines[0].split(/,(?![^"]*"(?:(?:[^"]*"){2})*[^"]*$)/);
14 |
15 | const list = [];
16 | for (let i = 1; i < dataStringLines.length; i++) {
17 | const row = dataStringLines[i].split(/,(?![^"]*"(?:(?:[^"]*"){2})*[^"]*$)/);
18 | if (headers && row.length == headers.length) {
19 | const obj = {};
20 | for (let j = 0; j < headers.length; j++) {
21 | let d = row[j];
22 | if (d.length > 0) {
23 | if (d[0] == '"')
24 | d = d.substring(1, d.length - 1);
25 | if (d[d.length - 1] == '"')
26 | d = d.substring(d.length - 2, 1);
27 | }
28 | if (headers[j]) {
29 | obj[headers[j]] = d;
30 | }
31 | }
32 |
33 | // remove the blank rows
34 | if (Object.values(obj).filter(x => x).length > 0) {
35 | list.push(obj);
36 | }
37 | }
38 | }
39 |
40 | // prepare columns list from headers
41 | const columns = headers.map(c => ({
42 | name: c,
43 | selector: c,
44 | }));
45 |
46 | setData(list);
47 | setColumns(columns);
48 | }
49 |
50 | // handle file upload
51 | const handleFileUpload = e => {
52 | const file = e.target.files[0];
53 | const reader = new FileReader();
54 | reader.onload = (evt) => {
55 | /* Parse data */
56 | const bstr = evt.target.result;
57 | const wb = XLSX.read(bstr, { type: 'binary' });
58 | /* Get first worksheet */
59 | const wsname = wb.SheetNames[0];
60 | const ws = wb.Sheets[wsname];
61 | /* Convert array of arrays */
62 | const data = XLSX.utils.sheet_to_csv(ws, { header: 1 });
63 | processData(data);
64 | };
65 | reader.readAsBinaryString(file);
66 | }
67 |
68 | return (
69 |
83 | );
84 | }
85 |
86 | export default App;
87 |
--------------------------------------------------------------------------------
/src/index.css:
--------------------------------------------------------------------------------
1 | body {
2 | margin: 20px;
3 | font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
4 | 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
5 | sans-serif;
6 | -webkit-font-smoothing: antialiased;
7 | -moz-osx-font-smoothing: grayscale;
8 | }
9 |
10 | code {
11 | font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
12 | monospace;
13 | }
14 |
--------------------------------------------------------------------------------
/src/index.js:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import ReactDOM from 'react-dom';
3 | import './index.css';
4 | import App from './App';
5 |
6 | ReactDOM.render(
7 |
8 |
9 | ,
10 | document.getElementById('root')
11 | );
12 |
--------------------------------------------------------------------------------