Rootstrap Get Started →

Insights

From Memory to Client: In-Memory Creation of XLSX Files and Zip Compression with Ruby on Rails

From Memory to Client: In-Memory Creation of XLSX Files and Zip Compression with Ruby on Rails

In the ever-evolving landscape of web applications, efficient data handling and delivery are paramount. Imagine a scenario where you need to dynamically generate spreadsheet data on the fly, without storing intermediate files on disk, and then seamlessly deliver them to your users through an API. How can you achieve this efficiently, all the while minimizing server load and latency?

In this document, we embark on a journey through the powerful capabilities of Ruby on Rails, uncovering a solution that bridges the gap between data generation and user delivery, all without touching the disk! We will explore the art of creating XLSX (Excel) files and compressing them into Zip archives, all in-memory, harnessing the agility of Ruby on Rails.

Let's get to work

Initially in order to generate a file with the Xlsx format, we need to add the caxlsx gem to our Ruby on Rails project's Gemfile. This gem allows us to export data correctly, taking into account the required format for these types of files.

gem 'caxlsx', '~> 3.4.1'

Building a XLSX file

We are going to create a class called XlsxBuilderService, whose purpose is to provide the generation of a file with the .xlsx extension, where the format can be defined according to the specific requirements we have within the columns we want to populate. It's important to be clear that the generated Xlsx file will not be saved to disk and will be stored in memory.

The parameter to be used by XlsxBuilderService will be a that will include the entire structure of headers and rows to be stored within the xlsx file, along with its respective name. For example:

← All insights