filmov
tv
How to convert excel file into JSON object by using JavaScript
Показать описание
This video tells how to convert an excel file into a JSON object by using JavaScript. How to convert Excel File (xlsx, xls) to JSON with Javascript using XLSX (inbuilt functionality of sheetJS) library.
Initially, when I started working on this requirement, people suggested using any one of the backend libraries to read and convert the excel data into JSON objects. But it is difficult to send requests to the server every time. On the other hand, it was really easy to convert the excel file on the client into a JSON object by using pure JavaScript.
There is JavaScript plugin XSLX that can be used to read excel files as binary strings and to convert it into a JSON object.
Installation
Include the CDN link in the head tag of an HTML file like this:
Now, in the HTML body tag, we need to write an input tag with the type as a file and need to restrict the user to select only files with the extension of .xls or .xlsx. Paste the below code into your HTML body tag.
Now we need to add event listeners for both input and button elements for handling change in input and button click events.
In JavaScript we can add the event listeners to elements like this:
We can read the data in the excel file by using file reader as a binary string in JavaScript. Then we use XLSX which has a builtin facility of SheetJS to convert our binary string into a JSON object.
Now select the excel file and click on the convert button to display the JSON object in the browser. After that, we can use the JSON object for any manipulations and integrate the data into different UI layouts.
Initially, when I started working on this requirement, people suggested using any one of the backend libraries to read and convert the excel data into JSON objects. But it is difficult to send requests to the server every time. On the other hand, it was really easy to convert the excel file on the client into a JSON object by using pure JavaScript.
There is JavaScript plugin XSLX that can be used to read excel files as binary strings and to convert it into a JSON object.
Installation
Include the CDN link in the head tag of an HTML file like this:
Now, in the HTML body tag, we need to write an input tag with the type as a file and need to restrict the user to select only files with the extension of .xls or .xlsx. Paste the below code into your HTML body tag.
Now we need to add event listeners for both input and button elements for handling change in input and button click events.
In JavaScript we can add the event listeners to elements like this:
We can read the data in the excel file by using file reader as a binary string in JavaScript. Then we use XLSX which has a builtin facility of SheetJS to convert our binary string into a JSON object.
Now select the excel file and click on the convert button to display the JSON object in the browser. After that, we can use the JSON object for any manipulations and integrate the data into different UI layouts.
Комментарии