tags:

views:

371

answers:

3

I need to create an excel file with multiple tabs. I need to do this in Ruby on Rails. I have checked out Apache POI but I'm not sure if it provides that functionality. Does anyone know if it does or if there are other alternatives that can do this? Thanks.

+2  A: 

If I understand you correctly - by multiple tabs you mean multiple 'worksheets' in a workbook. Apache POI does provide this functionality (check the links below). Please note that I am not a Ruby person (atleast not yet) and these links are for the usages in Java but I am pretty sure the bridges like YAJB will help you in getting it done:

Creating a New WorkBook
Creating a New WorkSheet

Vinnie
+2  A: 

I figured it out, you can call createSheet() on an instance of Workbook if you're using Apache POI.

A: 

I use the ruby gem "spreadsheet-excel" for this kind of functionality

#!/usr/bin/env ruby
RAILS_ENV = 'production'

require File.dirname(__FILE__) + '/../config/environment'
require "spreadsheet/excel" 

file = "name_of_your_excel_file.xls" 
workbook = Spreadsheet::Excel.new("#{RAILS_ROOT}/#{file}")

# First Sheet
worksheet = workbook.add_worksheet("Sheet No. 1")
worksheet.write(0, 0, "Timestamp")
worksheet.write(0, 1, "Type")
worksheet.write(0, 2, "Text")
# ...and whatever you want to do here
# Second Sheet
worksheet_2 = workbook.add_worksheet("Sheet No. 2")
#... and so on

This works great both in Ruby and Ruby-on-Rails.

To install spreadsheet/excel just type

ruby gem install "spreadsheet-excel"

Hope this can help you

Jonas Söderström