How to export data to an XLSX spreadsheet on Rails?

How to export data to an XLSX spreadsheet on Rails?

01 Sep 2021
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 in XLSX spreadsheet, follow the steps mentioned below:

 

Step 1: Add gem in the gem file.

XLSX-spreadsheet

 

Step 2: Run bundle install

 

Step 3: Add a link to the view

spreadsheet-on-Rails

 

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

spreadsheet-on-Rails

 

Step 5: Define the spreadsheet template

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

spreadsheet-on-Rails

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

And to retrieve DB data in 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.

spreadsheet-on-Rails

 

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.

spreadsheet-on-Rails

 

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




Leave a comment: