How To Repair WordPress Admin Panel

Reading data from Excel files (XLSX) is a common requirement in many
applications. Ruby, being a versatile and powerful programming language,
offers several libraries to work with Excel files. In this tutorial,
we’ll explore how to read XLSX files in Ruby using the popular
roo and creek gems.

Using the Roo Gem

Roo is a widely-used Ruby gem that provides a simple
interface to work with various spreadsheet formats, including XLSX,
XLS, OpenOffice, and Google Drive files. Let’s see how to read an XLSX
file using Roo.

1. Install the Roo gem:

To get started, you’ll need to install the Roo gem. You can do this by
simply running the following command:

gem install roo

2. Read the XLSX file:

Once the gem is installed, you can use it to read the contents of an
XLSX file. Here’s an example that demonstrates how to read an XLSX file
using Roo:

require ‘roo’

xlsx = Roo::Spreadsheet.open(‘path/to/your/xlsx/file.xlsx’)

xlsx.sheet(0).each_row_streaming do |row|
# Process your row data here
puts row.inspect
end

In the example above, we first require the ‘roo’ gem, and then open the
XLSX file using the Roo::Spreadsheet.open method. We
then choose the first sheet (sheet 0) and iterate through each row.

Using the Creek Gem

Another option to read XLSX files in Ruby is the creek
gem. Creek is a lightweight, fast, and memory-efficient option
specifically designed for reading large XLSX files.

1. Install the Creek gem:

To install the Creek gem, you can run the following command:

gem install creek

2. Read the XLSX file:

After installing the Creek gem, you can read the contents of an XLSX
file as follows:

require ‘creek’

creek = Creek::Book.new(‘path/to/your/xlsx/file.xlsx’)

creek.sheets[0].rows.each do |row|
# Process your row data here
puts row.inspect
end

In this example, we require the ‘creek’ gem and open the XLSX file using
the Creek::Book.new method. We then choose the first
sheet (sheet 0) and iterate through each row.

Conclusion

In this tutorial, we explored two popular Ruby gems, Roo and Creek, to
read XLSX files. Both gems provide a simple interface to work with XLSX
files, and you can choose the one that best suits your requirements and
performance needs.