Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A small script which generates a file that does not show any columns when opened with Microsoft Excel 2010 unless you highlight all and select the unhide option.
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.blank?
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.blank? }.map { |column| column.gsub('-', ' ').titleize }
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