如何使用axlsx(ruby)格式化excel的整列

时间:2015-06-11 00:14:27

标签: ruby axlsx

我对ruby很新,并尝试将生成的所有列格式化为excel文件的文本。 我有一个功能如下

def writeXlsx(columnNames, templateName)
        @xlsx = Axlsx::Package.new              # create xlsx doc
        @xlsx.use_autowidth = true
        @xlsxSheet = @xlsx.workbook.add_worksheet(:name => templateName)
        @unlocked = @xlsx.workbook.styles.add_style(:locked => false)       
        @xlsxSheet.col_style(0,@unlocked)
        @xlsxSheet.add_row columnNames, :types => string


        @xlsx.serialize(File.join(@outputFolder, "#{templateName}.xlsx"))

    end

这个错误说出了col_style undefined方法。你能告诉我我做错了什么吗? -ela

1 个答案:

答案 0 :(得分:1)

Axlsx似乎没有此选项。我想通过write_xlsx gem来做到这一点。

def WriteXlsxNew(columnNames, templateName,outputFolder)
    require 'write_xlsx'
    # Create a new Excel workbook
    filename = File.join(outputFolder, "#{templateName}.xlsx")
    workbook = WriteXLSX.new(filename)

    # Add a worksheet
    worksheet = workbook.add_worksheet(templateName)

    #  Add and define a format
    format1 = workbook.add_format(:num_format => '@')
    worksheet.set_column('A:BA',nil,format1)
    worksheet.write_row(0,0,columnNames)        
    workbook.close

end