-
-
Save straydogstudio/7013844 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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