caxlsx / caxlsx_rails

A Rails plugin to provide templates for the axlsx gem
MIT License
744 stars 84 forks source link

render html page in xlsx instead of requested data #117

Closed Kuassari closed 5 years ago

Kuassari commented 5 years ago

I tried everything I saw here, guides and stackoverflow and I don't know what to do. Everytime I try to export data it takes the html page text and insert it to xlsx file instead of what I wanted. See this picture:

image

Gemfile:

gem 'rails', '4.2.11'

# import/export xlsx gems
gem 'write_xlsx'
gem 'rubyzip', '>= 1.2.1'
gem 'axlsx', git: 'https://github.com/randym/axlsx.git', ref: 'c8ac844'
gem 'axlsx_rails'

Html file:

= link_to "<i class='icon icon-file-down'></i> Export</a></li>".html_safe, server_report_system_reports_path(format: :xlsx)

Controller file:

def server_report
    organization_id = params[:organization_id]
    @accounts = Admin::Account.where("organization_id = ?", organization_id) unless organization_id.blank?
    @accounts_paginated = @accounts.paginate(per_page: 10, page: params[:page] || 1 )

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

xlsx.axlsx file:

wb = xlsx_package.workbook
wb.add_worksheet(name: "Accounts") do |sheet|

  sheet.add_row ["Account id", "Member id", "Member name", "Member email"]
  @accounts_paginated.each do |account|
    account.members.each do |member|
      sheet.add_row [account.id, member.id, member.name, member.email]
    end
  end
end
straydogstudio commented 5 years ago

@Kuassari Just to clarify, the spreadsheet is inserted into a web page, or the web page is inserted into a spreadsheet? It sounds like you might be getting a layout.

Also, this link would probably create a problem: link_to " Export".html_safe, server_report_system_reports_path(format: :xlsx)

The link_to helper will create another tag on the right, and you are probably getting another tag as well. This could be the source of your problems.

Kuassari commented 5 years ago

the layout ruined it so after I wrote layout: false it worked. Now I have other problems but it's not connected to this problem