How to export data to an XLSX spreadsheet on Rails?

How to export data to an XLSX spreadsheet on Rails?

27 Sep 2022
0 Comments
How to export data to an XLSX spreadsheet on Rails?

XLSX is a file extension for a Microsoft Excel Spreadsheet. Exporting a file with the .xlsx extension creates an editable and viewable spreadsheet in Excel. This helps to easily re-group, combine, and re-format the data.

A model and controller are used to display data from the database in a table format. To convert that data into an XLSX spreadsheet, follow the steps mentioned below:
 

Step 1: Add gem in the gem file.


Step 2: Run bundle install


Step 3: Add a link to the view


Step 4: Add the support for XLSX responses to the controller


Step 5: Define the spreadsheet template

Create a file in views index.xlsx.axlsx and write the below-mentioned code for spreadsheet format.

In this format, add the column headings in the add_row section.

And to retrieve DB data in the excel sheet, add Sheet.add_row section database field names.


Step 6: After running the project, the index page will be shown, as shown below.


Step 7: Click on the “Export Excel Sheet” button to generate an Excel sheet. The sheet is automatically stored in Downloads and looks like the following.


Step 8: Using these three gems, the user can export Excel Sheet in the Rails application.




Leave a comment: