Excel file with multiple tabs

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

0


a source to share


3 answers


If you understood correctly - by multiple tabs, you mean multiple "worksheets" in a book. Apache POI provides this functionality (check links below). Please note that I'm not a Ruby person (at least not yet) and these links are for Java use, but I'm sure YAJB-like bridges can help you with that:



Create a new workbook
Create a new worksheet

+2


a source


I figured you can call createSheet () on the Workbook instance if you are using Apache POI.



+2


a source


I am using ruby ​​gem "spreadsheet-excel" for this kind of function

#!/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 in both Ruby and Ruby-on-Rails.

To install a spreadsheet or Excel just type

ruby gem install "spreadsheet-excel"

      

Hope this helps you.

0


a source







All Articles