
IN A NUTSHELL
-
Despite other data formats, clients consistently demand .xlsx reports. fast_excel remains my top choice in 2025 for its unmatched performance and clean syntax, vital for modern Rails applications handling growing data volumes.
-
The gem’s minimalist API, backed by libxlsxwriter, allows developers to quickly implement complex Excel features—like styling and formulas—without bloated code, directly addressing the ongoing need for lean, performant solutions in Rails development.
-
As applications scale, slow or complex reporting becomes a bottleneck. This guide demonstrates how fast_excel provides a simple, high-performance foundation, ensuring your Rails app’s reporting capabilities remain robust and maintainable for years to come.
Almost all applications have reporting features that are translated into documents or files. More often than not, if the file format is not a big factor, I like to use CSV file formats for reports since it’s pretty straightforward to generate, and Rails already has a built-in class for CSV. However, often, there are specific requirements that necessitate creating a report in an Excel format (.xlsx).
There are many available Ruby on Rails gems for writing an Excel file. My go-to gem, however, would be fast_excel, mainly for its simplicity (syntax-wise) and high performance (based on its own benchmarks: https://github.com/Paxa/fast_excel#benchmarks). Also, it’s worth noting that it uses the libxlsxwriter C library
Installing the gem is straightforward. Check here for reference: https://github.com/Paxa/fast_excel?tab=readme-ov-file#install
Basic Usage
# Create a new workbook
workbook = FastExcel.open("sample.xlsx")
# Add a worksheet
worksheet = workbook.add_worksheet("First Sheet")
# Write Data
worksheet.write_row(0, ["Name", "Email", "Phone", "Total Purchase"]) # Header
worksheet.write_row(1, ["John Doe", "test@test.com", "09204259099", 500])
worksheet.write_row(2, ["Jane Smith", "test2@test.com", "09294258483", 1500])
# Also take note, You can use append_row as well specially if you're writing data sequentially
# worksheet.append_row(["Name", "Email", "Phone", "Total Purchase"]) #
# worksheet.append_row(["John Doe", "test@test.com", "09204259099", 500])
# worksheet.append_row(["Jane Smith", "test2@test.com", "09294258483", 1500])
# Save the file
workbook.close
Adding Styles and Format
You can also add style formats either to a whole row, or to a specific cell
workbook = FastExcel.open("sample2.xlsx")
# Create custom formats
header_format = workbook.bold_format
header_format.set(
bold: true,
bg_color: "#FFD700",
font_size: 12,
align: :center
)
# Create a worksheet
worksheet = workbook.add_worksheet
# Write headers with format
worksheet.write_row(0, ["Product", "Price", "Quantity"], header_format)
# Write data
worksheet.write_row(1, ["Apple", 10.99, 100])
worksheet.write_row(2, ["Banana", 15.99, 50])
# Format a Single Cell
worksheet.write_value(3, 1, "A Single Formatted Cell", header_format)
workbook.close
[
Setting Column Widths
Before you append or write on a row, You can set the column width
workbook = FastExcel.open("sample2.xlsx")
...
worksheet.set_column_width(0, 20) # Column A width to 20
worksheet.set_column_width(1, 40) # Column B width to 40
worksheet.set_column_width(2, 30) # Column C width to 30
worksheet.write_row(0, ["Product", "Price", "Quantity"], header_format)
...
[
Adding Formulas
workbook = FastExcel.open("formulas_example.xlsx")
worksheet = workbook.add_worksheet
worksheet.write_row(0, ["Quantity", "Price", "Total"])
worksheet.write_row(1, [5, 10])
worksheet.write_row(2, [3, 15])
# Add formulas
worksheet.write_value(1, 2, FastExcel::Formula.new("A2*B2")) # Calculates total for row 1. Writes on C2
worksheet.write_value(2, 2, FastExcel::Formula.new("A3*B3")) # Calculates total for row 2. Writes on C3
worksheet.write_value(3, 2, FastExcel::Formula.new("SUM(C2:C3)")) # Calculates grand total. Writes on C4
workbook.close
Conclusion
The fast_excel gem provides a minimalist, but powerful and efficient way to generate Excel files in Ruby applications. This is just a overview of basic use cases for the fast_excel gem. You can also check out other examples from their github repository https://github.com/Paxa/fast_excel/tree/master/examples
Ps. if you have any questions
Ask here