Open In App

How to add Downloadable Spreadsheet in Next.js ?

Last Updated : 05 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to learn how we can add a downloadable spreadsheet in NextJs. NextJS is a React-based framework. It has the power to Develop beautiful Web applications for different platforms like Windows, Linux, and mac. The linking of dynamic paths helps in rendering your NextJS components conditionally.

Approach: To add our downloadable spreadsheet we are going to use the react-xls package. The react-xls package helps us to add a downloadable spreadsheet anywhere in our app. So first, we will install the react-xls package and then we will add a downloadable spreadsheet on our homepage.

Create NextJS Application: You can create a new NextJs project using the below command:

npx create-next-app gfg

Install the required package: Now we will install the react-xls package using the below command:

npm i react-xls

Project Structure: It will look like this.

Adding the Spreadsheet: We can easily add the downloadable spreadsheet in our app after installing the react-xls package. For this example, we are going to add the downloadable spreadsheet to our homepage.

Add the below content in the index.js file:

Javascript




import React from 'react';
import { useExcelDownloder } from 'react-xls';
  
function App() {
  const { ExcelDownloder, Type } = useExcelDownloder();
  
  const data = {
    Data1: [
      { name: 'gfg1', category: 'gfg4' },
      { name: 'gfg2', category: 'gfg5' },
      { name: 'gfg3', category: 'gfg6' },
    ],
    // Worksheet named pokemons
    Data2: [
      { name: 'gfg1', category: 'gfg1' },
      { name: 'gfg1', category: 'gfg1' },
      { name: 'gfg1', category: 'gfg1' },
    ],
  };
  
  return (
    <div>
      <h3>GeeksforGeeks - Downloadable Spreadsheet</h3>
      <ExcelDownloder
        data={data}
        filename={'book'}
        type={Type.Button} // or type={'button'}
      >
        Download the Spreadsheet
      </ExcelDownloder>
    </div>
  );
}
  
export default App;


Explanation: In the above example first, we are importing the useExcelDownloader from the installed package. After that, we are creating a new data variable in which we will store the data for our spreadsheet. Then we will add this data in a downloadable spreadsheet using the useExcelDownloader.

Steps to run the application: Run the below command in the terminal to run the app.

npm run dev

Output:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads