How to write array to Excel with differing styles per column using Axlsx

a 夏天 提交于 2019-12-11 02:22:39

问题


I would like to be able to use the Axlsx gem to write out an Excel spreadsheet with different styles applied to different columns.

I am writing an Array of Hashes an entire row at a time and, as a result, I cannot seem to format the columns differently as needed.

I don't know how to say, "for columns A-D use default styling, however for column E and F, use horizontal center alignment".

require 'axlsx'

p = Axlsx::Package.new
wb = p.workbook

# Default Style for all cells
standard_text = wb.styles.add_style( :alignment => {:vertical=>:center, :wrap_text=>true} )

# Custom styling to be applied to cells in rows E and F only
custom_text = wb.styles.add_style( :alignment => {:horizontal=>:center} )

assessment_technology_hashes.each do |rows|
  sheet.add_row(rows.values, :height => 35, :style => standard_text)
end

Here is the structure of the Array of hashes for rows that is being written with add_row:

{:vendor_name=>"vendor", :link=>"Link\n", :importance=>"Low Priority", :score=>"5", :overall_score=>"4.5", :match=>"Yes", :access=>"Anywhere", :title=>"Full Title"}
{:vendor_name=>"vendor2", :link=>"Link2\n", :importance=>"Medium Priority", :score=>"7", :overall_score=>"8.5", :match=>"Yes", :access=>"Typical", :title=>"Full Title"}
...
...

In this scenario, is the correct approach to write all of the data with the primary formatting standard_text that is desired, and then apply the custom formatting AFTER all the data has been written?

I thought this might work in the loop to only write out rows when the condition is true but it always returns false:

sheet.add_row(rows.values, :height => 35, :style => custom_text) if rows.key?(:overall_score) || rows.key?(:match)

Can someone point me in the correct direction to do this?


回答1:


Here is how I do it.

First, I generate my styles like this:

p  = Axlsx::Package.new
wb = p.workbook
  # styles
  style_1 = wb.styles.add_style bg_color: '3492ff', font_name: 'Calibri'
  style_2 = wb.styles.add_style bg_color: 'ff8800', font_name: 'Arial'

After my styles are read, I create a worksheet:

wb.add_worksheet(name: 'Example') do |sheet|

After worksheet has been created I am able to add data by calling add_row. And as a last argument, I pass array of styles. Example:

sheet.add_row ['Column 1', 'Column2'], style: [style_1, style_2]

In this way I am able to style individual column's cell. In case you don't want to apply style_2, just type [style_1, nil].

Here is the concatenated code

p  = Axlsx::Package.new
wb = p.workbook
  # styles
  style_1 = wb.styles.add_style bg_color: '3492ff', font_name: 'Calibri'
  style_2 = wb.styles.add_style bg_color: 'ff8800', font_name: 'Arial'
  # worksheet
  wb.add_worksheet(name: 'Example') do |sheet|
    sheet.add_row ['Column 1', 'Column2'], style: [style_1, style_2]
  end
end


来源:https://stackoverflow.com/questions/36583704/how-to-write-array-to-excel-with-differing-styles-per-column-using-axlsx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!