Skip to content

Instantly share code, notes, and snippets.

@straydogstudio
Forked from zaid/xlsx_test.rb
Created October 16, 2013 20:01
Show Gist options
  • Save straydogstudio/7013844 to your computer and use it in GitHub Desktop.
Save straydogstudio/7013844 to your computer and use it in GitHub Desktop.
require 'axlsx'
title = "Some report"
selected_columns = %w(one two three four five)
Axlsx::Package.new do |package|
package.workbook do |workbook|
# Disabling this will improve performance for very large documents
workbook.use_autowidth = false
workbook.add_worksheet(:name => 'Data') do |sheet|
unless title =~ /^\s*$/
title_format = sheet.styles.add_style({:b => true, :sz => 18})
sheet.add_row([title], :style => title_format)
sheet.add_row
end
# => Create the headers from the selected columns
header_format = sheet.styles.add_style({:b => true, :alignment => {:horizontal => :center}, :bg_color => "FFDFDEDF"})
headers = selected_columns.select { |column| column !~ /^\s*$/ }.map { |column| column.gsub('-', ' ') }
sheet.add_row(headers, :style => header_format)
row_format = sheet.styles.add_style({:alignment => {:horizontal => :left}})
sheet.add_row([1, 2, 3, 4, 5], :style => row_format)
end
end
package.serialize("Test_file.xlsx")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment