Welcome folks today in this blog post we will be exporting html5 table
to excel file and download it as attachment
in browser using javascript. All the full source code
of the application is shown below.
Get Started
In order to get started you need to make a new react.js
app using the below command as shown below
npx create-react-app sampleapp
cd sampleapp
And after that you need to install the below library
using the below command as shown below
npm i react-html-table-to-excel
And after that you will see the below directory
structure of the react.js app as shown below
Export HTML5 Table to Excel File
Now we can modify the App.js
file and copy paste the following code
App.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
import ReactHTMLTableToExcel from "react-html-table-to-excel"; export default function App() { return ( <div> <ReactHTMLTableToExcel table="table-to-xls" filename="tablexls" sheet="tablexls" buttonText="Download as XLS" /> <table id="table-to-xls"> <tr> <th>Firstname</th> <th>Lastname</th> <th>Age</th> </tr> <tr> <td>Jill</td> <td>Smith</td> <td>50</td> </tr> <tr> <td>Eve</td> <td>Jackson</td> <td>94</td> </tr> </table> </div> ); } |
As you can see we are importing the react-html-table-to-excel
library at the top and then we are rendering the widget
and then we are passing the id
of the table to export to excel
and also we are passing the filename
of the excel and then we are passing the text
of the button as shown below.