Sumit.Daksh
Sumit.Daksh

Reputation: 23

export excel 2003(xls) file in ruby with date column(cell formatting = date)

I want to export/create excel sheet 2003 (xls) with date column (cell formating = Date). Please suggest any gem and logic for that.

Upvotes: 1

Views: 617

Answers (1)

ChesterLaborde
ChesterLaborde

Reputation: 458

I had this same problem. Axlsx_rails helped me. It has various examples.

To format a cell to a Date cell, you have to do the next,

wb = xlsx_package.workbook
wb.styles do |s|
  date = s.add_style(:format_code => "dd/mm/yyyy")
end

And then you pass the style you want to the cell, i.e:

wb = xlsx_package.workbook
wb.styles do |s|
  date = s.add_style(:format_code => "dd/mm/yyyy")
  wb.add_worksheet(name: "Example") do |sheet|
     sheet.add_row['Date Column']
     sheet.add_row[Date.today], :style => [date]
  end
end

As for the format, you could change the name on save, like so:

    respond_to do |format|
      format.html
      format.xlsx do
        response.headers['Content-Disposition'] = 'attachment; filename="excel_sheet.xls"'
      end
    end

Upvotes: 1

Related Questions