To search within a spreadsheet in Excel, press Ctrl + F
(or Cmd + F
on Mac) to open the Find dialog. Enter the target text in the “Find what” box and click “Find Next” to locate instances of the text in the spreadsheet.
Key takeaways:
Create a React app that lets users search for a specific word in an Excel sheet and shows the rows where that word appears.
Set up the project using
npx create-react-app
and install thexlsx
library to read Excel files.Main parts:
App.js
: Contains the search input where users type their queries.
ExcelReader.js
: Handles uploading the Excel file and filtering the data based on the search input.
The app reads the Excel file using the
FileReader
API and converts the data into a format that can be searched.This app works well for small to medium-sized Excel files, but may slow down with larger files.
In this Answer, we will develop a React application that takes the input of an Excel file, a sheet name, and a target word. The objective is to search for the specified target word in the Excel sheet. If the target word is located within the sheet, we display the rows where it is present.
Before we begin, ensure you have the following:
Basic knowledge of ReactJS.
A React environment set up on your machine (use npx create-react-app
if you haven’t already).
A modern browser for testing.
We’ll use the xlsx
library to parse the Excel sheet, so let’s start by setting up the project.
First, navigate to your project folder and run the following command to create a new React app and install the xlsx
library:
npx create-react-app excel-appcd excel-appnpm install xlsx
This command sets up the React environment and installs xlsx
, a powerful library that enables us to parse Excel sheets into JSON format.
App component: The App.js
file will act as the main component where users can search data after reading it from the Excel file.
import React, { useState } from 'react';import ExcelReader from './components/ExcelReader';function App() {const [searchQuery, setSearchQuery] = useState('');return (<div className="App"><h1>Excel Sheet Reader and Search</h1><inputtype="text"placeholder="Search..."value={searchQuery}onChange={(e) => setSearchQuery(e.target.value)}className="search-box"/><ExcelReader searchQuery={searchQuery} /></div>);}export default App;
This component renders the search input and passes the search query to the ExcelReader
component for filtering.
ExcelReader component: The ExcelReader.js
file is responsible for handling file uploads, reading the content of the Excel file, and displaying it in a searchable table.
import React, { useState } from 'react';import * as XLSX from 'xlsx';function ExcelReader({ searchQuery }) {const [excelData, setExcelData] = useState([]);// Function to handle file upload and parsingconst handleFileUpload = (event) => {const file = event.target.files[0];const reader = new FileReader();reader.onload = (e) => {const data = new Uint8Array(e.target.result);const workbook = XLSX.read(data, { type: 'array' });const firstSheet = workbook.Sheets[workbook.SheetNames[0]];const jsonData = XLSX.utils.sheet_to_json(firstSheet);setExcelData(jsonData);};reader.readAsArrayBuffer(file);};// Filter data based on the search queryconst filteredData = excelData.filter((row) => {return Object.values(row).some((value) =>value.toString().toLowerCase().includes(searchQuery.toLowerCase()));});return (<div className="excel-reader"><input type="file" onChange={handleFileUpload} className="file-input" /><table><thead><tr>{excelData.length > 0 &&Object.keys(excelData[0]).map((key) => <th key={key}>{key}</th>)}</tr></thead><tbody>{filteredData.map((row, index) => (<tr key={index}>{Object.values(row).map((value, i) => (<td key={i}>{value}</td>))}</tr>))}</tbody></table></div>);}export default ExcelReader;
This component:
Reads and parses the Excel file using the xlsx
library.
Converts the sheet data into JSON format.
Filters the data based on the search query entered by the user.
To style the application, create a styles/App.css
file. Here’s an example of the CSS you can use to style the components:
This adds basic styling for the app layout, table formatting, and input fields.
body {font-family: Arial, sans-serif;margin: 0;padding: 0;background-color: #f4f4f4;}.App {text-align: center;padding: 20px;}h1 {color: #333;}.search-box {width: 200px;padding: 8px;margin-bottom: 20px;border: 1px solid #ccc;border-radius: 4px;}.excel-reader {margin-top: 20px;}.file-input {padding: 10px;margin-bottom: 20px;}table {width: 100%;border-collapse: collapse;margin-top: 20px;}table, th, td {border: 1px solid #ccc;}th, td {padding: 12px;text-align: left;}th {background-color: #f2f2f2;}
When you upload an Excel file, the handleFileUpload
function reads the file using the FileReader
API, converts it into an array buffer, and parses it using the XLSX.read
function.
The XLSX.utils.sheet_to_json
function converts the sheet into a JSON object, which is then saved in the state (excelData
).
As you type in the search box, the application filters the data. The filteredData
array only includes rows where any value matches the search query.
The filtered data is displayed in a table format, with each column representing a key from the Excel sheet.
Below is the complete working code to search in the Excel sheet.
We have provided the project structure along with CSS files to style our forms and tables in the React app.
AAABAAQAEBAAAAEAIADjAQAARgAAABgYAAABACAADAMAACkCAAAgIAAAAQAgADkDAAA1BQAAQEAAAAEAIACwBgAAbggAAIlQTkcNChoKAAAADUlIRFIAAAAQAAAAEAgDA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5aSHoDs6qWc9166DAz1/A7z9p94j9K/IPisRBEXwW+XuRmLQ/v4FfRW4BKS4BSstEpDyACqmsolpcVHOpUioIKBcJr6kFSoqdAoV1EFgvGg00ShM5mqE1t0RFtTRrxzSHJmmlVjTaAqGu0BYIr4Z2c2Y64GcsnaLoQkO77iS2CLrN+e6Bh+GWQBH0iklfPwM8HRSRIQxeisjg073H9uT1QpElEEzgd7EYBkZEMYrBqChGgB6x+B5ImCUwxgOxGb/F2IQoojCIEsXEGBcnxeYBl04uMHWEQO1Zh1AEV+xJ/GFO4ksMmg+ZxD/w07WMPY5lnHYvY5exjNMwYxYw9zLK6zqIMDZSLXXSxK1cUcy25OS0vLU2UiMNxkaaszbS8bdy1YGt7HWY5o1t4vDCf56HSfFpfgEglsX8oMOPc1D+IrEAC/OfvA1laXnlo8tQmuDjyvKSp6Gc3dIsU11VprpWhRuq1pSprlqm6m/r6xu3eHrjmeLGU25trHvaundh2YTPHoXFu7Td9ShtZy+u/uW9wL+8+wWMW6cIGBlis7UXcbbFJtWvB7mQJBbxesiKF4sESD5hzEtUMS/m+DFPgokwvi5/aAfNh1n+QdP2owot6j7bi7qroljVou5936jrDNvROwfD9k60f9h2xf2Pzrj/MQCN69fET0Amdi/E/l+X9lz0Dsdc7S4Pmb0rMJQDOxwUASEGZsoMEKa00yXCIEpZt0+Uwo4nL8VdX4o73xDAJyImTqpuCWERUZBeABmUY3imQeHSAAAAAElFTkSuQmCC
We have successfully parsed an input Excel file, integrated search functionality through filtering mechanisms, state management using React hooks, and presented the results in a styled table.
This Answer demonstrated how to read and search for specific content in an Excel sheet using ReactJS. By utilizing modular components, such as Form.js
for user input and ExcelData.js
for processing, we enhanced maintainability. The implementation leverages the xlsx
npm package, along with useState
and useEffect
hooks, to manage state and perform case-insensitive searches.
While effective for small to medium Excel files, performance may be impacted with larger datasets. Overall, this solution provides a solid foundation for Excel data handling in React applications.
Haven’t found what you were looking for? Contact Us
Free Resources