2

我对 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 未定义的方法。你能告诉我我做错了什么吗?-埃拉

4

1 回答 1

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
于 2015-06-18T09:23:22.607 回答