Am使用axlsx_rails gem和activeadmin导出格式化的Excel报告 这种格式在Ubuntu LibreOffice中工作正常,但是样式在Windows OS中会中断。
这是Ubuntu中正确的对齐格式。
但是此图像下面是Windows中的标头边框和间距 对齐不正确
我的代码在这里。
宝石文件
gem 'axlsx', '~> 2.0.1'
gem 'axlsx_rails'
gem 'rubyzip', '~> 1.0.0'
config / initializers / mime_types.rb
Mime :: Type.register“ application / vnd.ms-excel”,:xls
app / admin / job.rb
member_action :export_lease_abstract, method: :get do
@job = Job.find(params[:id])
render xlsx: 'export_lease_abstract_new_format', filename: 'lease_abstract_new_format', disposition: 'inline'
end
示例视图文件-export_lease_abstract_new_format.xlsx.axlsx
wb = xlsx_package.workbook
header_style = wb.styles.add_style :sz => 14, :name => 'Times New Roman', :b => true, :alignment => {:horizontal => :center, :vertical => :top, :wrap_text => true}, :bg_color => "C0C0C0", :fg_color => "000000"
property_style = wb.styles.add_style :b => true, :sz => 12, :name => 'Times New Roman', :border => { :color => 'FF000000', :style => :thin }
sub_header_style = wb.styles.add_style :b => true, :sz => 10, :name => 'Times New Roman', :border => { :color => 'FF000000', :style => :thin }
content_title_style = wb.styles.add_style :b => true, :sz => 10, :name => 'Times New Roman'
content_value_style = wb.styles.add_style :sz => 10, :name => 'Times New Roman', :alignment => {:horizontal => :left}
content_title_top = wb.styles.add_style :b => true, :sz => 10, :name => 'Times New Roman', :alignment => {:horizontal => :left, :vertical => :top}
content_wrap_text = wb.styles.add_style :sz => 10, :name => 'Times New Roman', :alignment => {:horizontal => :left, :vertical => :top, :wrap_text => true}
content_title_center = wb.styles.add_style :b => true, :sz => 10, :name => 'Times New Roman', :alignment => {:horizontal => :center}
content_title_right = wb.styles.add_style :b => true, :sz => 10, :name => 'Times New Roman', :alignment => {:horizontal => :right}
rent_value_center_style = wb.styles.add_style :sz => 8, :name => 'Times New Roman', :alignment => {:horizontal => :center}
rent_value_right_style = wb.styles.add_style :sz => 10, :name => 'Times New Roman', :alignment => {:horizontal => :right}
border = wb.styles.add_style :border => { :color => 'FF000000', :style => :thin }
gray_cell = wb.styles.add_style :bg_color => "C0C0C0", :fg_color => "FF"
wb.add_worksheet(:name => "Lease Abstract") do |sheet|
sheet.sheet_view.show_grid_lines = false
sheet.column_widths 5.2, 32.5, 12, 14, nil, 15, nil, nil, 5.2
sheet.add_row [nil, nil, nil, nil, nil, nil, nil, nil, nil], :style => gray_cell
sheet.add_row [nil, @job.la_project.name, nil, nil, nil, nil, nil, nil, nil], :style => [gray_cell, header_style, nil, nil, nil, nil, nil, nil, gray_cell], :height => 20
sheet.merge_last_row columns:["B","H"]
# img = File.expand_path('../../../assets/images/hds-logo.png', File.dirname(__FILE__))
# sheet.add_image(:image_src => img, :noSelect => true, :noMove => true) do |image|
# image.width=100
# image.height=50
# image.start_at 0, 0
# end
sheet.add_row [nil, nil, nil, nil, nil, nil, nil, nil, nil], :style => [gray_cell, nil, nil, nil, nil, nil, nil, nil, gray_cell], :height => 12
sheet.add_row [nil, lease_information.try(:tenant_name), nil, nil, nil, nil, nil, 'Lease Abstract', nil], :height => 17, :style => [gray_cell, property_style, nil, nil, nil, nil, nil, property_style, gray_cell], width: 10
sheet.merge_last_row columns:["B","G"]
sheet.add_row [nil, "PARTIES:", nil, nil, nil, nil, nil, nil, nil], :height => 12, :style => [gray_cell, sub_header_style, nil, nil, nil, nil, nil, nil, gray_cell]
sheet.merge_last_row columns:["B","H"]
# sheet.column_info.first.width = 30
sheet.add_row [nil, nil, nil, nil, nil, nil, nil, nil, nil], :style => [gray_cell, nil, nil, nil, nil, nil, nil, nil, gray_cell], :height => 8
sheet.add_row [nil, "Tenant Name:", lease_information.try(:tenant_name), nil, nil, "DBA:", lease_information.try(:dba), nil, nil], :style => [gray_cell, content_title_style, content_value_style, nil, nil, content_title_style, content_value_style, nil, gray_cell]
sheet.merge_last_row columns:["C","E"]
sheet.merge_last_row columns:["G","H"]
# sheet.column_info.first.width = 30
sheet.add_row [nil, nil, nil, nil, nil, nil, nil, nil, nil], :style => [gray_cell, nil, nil, nil, nil, nil, nil, nil, gray_cell], :height => 8
end
如何将这种格式修复为在所有操作系统中都一样。