答案 0 :(得分:0)
使用此gem生成带有rails xlsx generation with charts, images, automated column width, customizable styles and full schema validation
的excel示例示例,sample example syntaxes 检查此链接
以下示例代码,用于生成上面指定的excel格式
def excel
@package = Axlsx::Package.new
@wb = @package.workbook
load_styles #----> to call load_styles function for styles
@wb.add_worksheet name: 'outline_level', fit_to_page: true do |sheet|
sheet.add_row [""],:height => 6
sheet.add_row ["","","","","","","",""],:height => 20, :b => true, :style => @align_center
sheet.merge_cells("A2:H2")
sheet.add_row [""],:height => 6
sheet.add_row ["", "","","", "", "","",""], :sz => 10, :style => @top,:height => 16,:style => @top_right
sheet.merge_cells("A4:D4")
sheet.merge_cells("F4:H4")
sheet.rows.last.cells[5].b = true
@exporter = @invoice.exporter_address.split("\n")
@importer = @invoice.plant_address.split("\n")
@consignee = @invoice.consignee_address.split("\n")
@notifyparty = @invoice.clearingagent_address.split("\n")
sheet.add_row ["", "","","", "", "","",""], :sz => 10,:height => 16,:style => @right
sheet.merge_cells("A5:D5")
sheet.merge_cells("F5:H5")
sheet.rows.last.cells[0].sz = 8
sheet.rows.last.cells[0].b = true
sheet.add_row ["", "","","", "", "","",""], :sz => 10,:height => 16,:style => @right
sheet.merge_cells("A6:D6")
sheet.merge_cells("E6:H6")
sheet.rows.last.cells[4].sz = 8
sheet.rows.last.cells[4].b = true
sheet.rows.last.cells[0].sz = 8
sheet.rows.last.cells[0].b = true
sheet.rows.last.cells[4].style = @top
sheet.add_row ["", "","","", "#{}", "","",""], :sz => 9,:height => 16,:style => @right
sheet.merge_cells("A7:D7")
sheet.merge_cells("E7:H7")
sheet.rows.last.cells[4].b = true
sheet.rows.last.cells[4].style = @align_center
sheet.rows.last.cells[0].sz = 8
sheet.rows.last.cells[0].b = true
sheet.add_row ["","","","","","","",""],:height => 10
sheet.merge_cells("A8:D8")
sheet.merge_cells("E8:H8")
sheet.rows.last.cells[3].style = @right
sheet.rows.last.cells[7].style = @right
sheet.add_row ["","","","", "", "","",""], :sz => 8,:height => 12,:style => @right
sheet.merge_cells("A9:D9")
sheet.merge_cells("E9:H9")
sheet.add_row ["", "","","", "", "","",""],:sz => 8,:height => 16,:style => @right
sheet.merge_cells("A10:D10")
sheet.merge_cells("E10:H10")
sheet.rows.last.cells[4].sz = 8
sheet.add_row ["", "","","", "", "","",""],:sz => 8,:height => 16,:style => @right
sheet.merge_cells("A11:D11")
sheet.merge_cells("E11:H11")
sheet.rows.last.cells[0].b = true
sheet.add_row ["", "","","", "", "","",""],:sz => 8,:height => 16,:style => @right
sheet.merge_cells("A12:D12")
sheet.merge_cells("E12:H12")
sheet.rows.last.cells[0].b = true
sheet.add_row ["", "","","", "", "","",""],:sz => 8,:height => 16,:style => @right
sheet.merge_cells("A13:D13")
sheet.merge_cells("E13:H13")
sheet.rows.last.cells[0].b = true
sheet.add_row ["","","","","","","",""],:height => 0,:style => @right
sheet.merge_cells("A15:D15")
sheet.merge_cells("E15:H15")
sheet.rows.last.cells[3].style = @right
sheet.rows.last.cells[7].style = @right
sheet.add_row ["", "","","", " ", "","",""],:sz => 8,:height => 16,:style => @top_right
sheet.merge_cells("A16:D16")
sheet.merge_cells("E16:H16")
sheet.add_row ["","","","","","","",""],:sz => 8,:height => 12,:style => @top_right
sheet.merge_cells("A23:B23")
sheet.merge_cells("C23:D23")
sheet.merge_cells("E23:H23")
sheet.add_row ["","","","","","","",""],:sz => 8,:height => 16,:style => @right
sheet.merge_cells("A24:B24")
sheet.merge_cells("C24:D24")
sheet.merge_cells("E24:H24")
sheet.rows.last.cells[0].b = true
sheet.rows.last.cells[2].b = true
sheet.rows.last.cells[4].b = true
sheet.rows.last.cells[4].style = @align_center
sheet.rows.last.cells[0].style = @bottom_center
sheet.rows.last.cells[2].style = @bottom_center
sheet.add_row [" ","","","","",""],:sz => 8,:height => 12,:style => @right
sheet.merge_cells("A25:B25")
sheet.merge_cells("C25:D25")
sheet.merge_cells("E25:H25")
sheet.add_row ["","","#{@invoice.delivery_type}, #{@invoice.final_destination}","","","","",""],:sz => 8,:height => 16, :style => @bottom,:style => @right
sheet.merge_cells("A26:B26")
sheet.merge_cells("C26:D26")
sheet.merge_cells("E26:H26")
sheet.rows.last.cells[0].b = true
sheet.rows.last.cells[2].b = true
sheet.rows.last.cells[0].style = @align_center
sheet.rows.last.cells[2].style = @align_center
sheet.add_row [""," "," ","","","","",""],:sz => 8,:height => 12,:style => @top
sheet.rows.last.cells[7].style = @center_right_top_border
sheet.rows.last.cells[5].style = @right_left_border_top
sheet.rows.last.cells[6].style = @right_left_border_top
sheet.merge_cells("C27:E27")
sheet.column_widths 12.5, 12.5, 12.5, 12.5, 12.5, 12.5, 12.5, 12.5
end
@package.serialize("#{Rails.root}/tmp/basic.xlsx")
send_file("#{Rails.root}/tmp/basic.xlsx", filename: "excel.xlsx", type: "application/xlsx")
end
def load_styles
@wb.styles do |s|
@top = s.add_style :font_name => 'Arial', :font_name => 'Arial',:border => { :style => :thin, :color =>"00000000", :edges => [:top] }
@right = s.add_style :font_name => 'Arial', :border => { :style => :thin, :color =>"00000000", :edges => [:right] }
@left = s.add_style :font_name => 'Arial', :border => { :style => :thin, :color =>"00000000", :edges => [:left] }
@bottom = s.add_style :font_name => 'Arial', :border => { :style => :thin, :color =>"00000000", :edges => [:bottom] }
@center = s.add_style :font_name => 'Arial', :alignment => { :horizontal => :center }
@top_bottom = s.add_style :font_name => 'Arial', :border => { :style => :thin, :color =>"00000000", :edges => [:top,:bottom] }
@bold = s.add_style :font_name => 'Arial', :b => true
@top_right = s.add_style :font_name => 'Arial', :border => { :style => :thin, :color =>"00000000", :edges => [:top,:right] }
end
end