According to this post , spreadsheet is an opportunity. It seems to be a very popular stone. Check this. Example:
book = Spreadsheet::Workbook.new sheet1 = book.create_worksheet header_format = Spreadsheet::Format.new( :weight => :bold, :horizontal_align => :center, :bottom => true, :locked => true ) sheet1.row(0).default_format = header_format FasterCSV.open(input_path, 'r') do |csv| csv.each_with_index do |row, i| sheet1.row(i).replace(row) end end book.write(output_path)
According to this post write_xlsx is a feature.
I used Apache POI library with JRuby to export xls files. Here is a brief example.
require 'java' require 'poi.jar'
Some useful methods for formatting POI tables:
sheet.createFreezePane(0,1,0,1)wb.setRepeatingRowsAndColumns(0, -1, -1, 0, 1)sheet.setColumnWidth(i, 100 *256)sheet.autoSizeColumn(i) , but be careful if you are working in headless mode, you need to call java.lang.System.setProperty("java.awt.headless", "true")
You can also use Win32ole on Windows if you have Excel installed
require 'win32ole' require 'rubygems' require 'fastercsv' xl = WIN32OLE.new('Excel.Application') xl.Visible = 0 wb = xl.Workbooks.Add ws = wb.Worksheets(1) FasterCSV.open(ARGV.first) do |csv| csv.each_with_index do |csv_row, line_no| csv_row.each_with_index do |value, col| ws.Cells(line_no + 1, col + 1).Value = value end end end wb.SaveAs("workbook.xls", 56)
Some useful methods for formatting in Excel:
xl.Rows(1).Font.Bold = truews.Cells.EntireColumn.AutoFit
Another option is to write directly in the Microsoft XML Spreadsheet format , as Ryan Bates on Railscasts.com makes at the end of his CSV and Excel export episodes .
<?xml version="1.0"?> <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40"> <Worksheet ss:Name="Sheet1"> <Table> <Row> <Cell><Data ss:Type="String">ID</Data></Cell> <Cell><Data ss:Type="String">Name</Data></Cell> <Cell><Data ss:Type="String">Release Date</Data></Cell> <Cell><Data ss:Type="String">Price</Data></Cell> </Row> <% @products.each do |product| %> <Row> <Cell><Data ss:Type="Number"><%= product.id %></Data></Cell> <Cell><Data ss:Type="String"><%= product.name %></Data></Cell> <Cell><Data ss:Type="String"><%= product.released_on %></Data></Cell> <Cell><Data ss:Type="Number"><%= product.price %></Data></Cell> </Row> <% end %> </Table> </Worksheet> </Workbook>
This stone looks promising, too .
John douthat
source share