How to format whole column for an excel using axlsx (ruby)

1.4k views Asked by At

I am pretty new to ruby and trying to format all columns generated to text for the excel file. I have got a function as below

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

this errors out saying col_style undefined method. Can you please tell me what I am doing wrong? -ela

1

There are 1 answers

0
ela On

Axlsx does not seem to have this option. I figured out to do it using 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