2

axlsx_rails gem で xlsx ファイルを生成しています。

ユーザー入力を収集した後、データを投稿しています/basic_report_post.xlsx

コントローラーのアクションは次のようになります

def basic_report_post
    @config = params[:config]
    @data = params[:data]
    @filename = "#{Rails.root}/public/test.xlsx"

    respond_to do |format|
      format.xlsx {
        render xlsx: 'basic_report_post'
      }
    end
  end

このアクションのファイルを表示 basic_report_post.xlsx.axlsx

wb = xlsx_package.workbook    
wb.add_worksheet(name: 'Data1') do |s|    
# Drawing columns    
end
xlsx_package.serialize @filename

私の問題は、生の .xlsx ファイルである応答データ (成功後のアクション) を取得していることです。しかし、後でダウンロードするには、何らかの形で応答する必要があり@filenameます(json/htmlの形式)。

4

2 に答える 2

3

axlsx_railsテンプレート レンダラーを使用して文字列を作成し、ファイルに保存することができます。

def basic_report_post
  @config = params[:config]
  @data = params[:data]
  @filename = "#{Rails.root}/public/test.xlsx"
  File.open(@filename, 'w') do |f|
    f.write render_to_string(handlers: [:axlsx], formats: [:xlsx], template: 'path/to/template')
  end
  render json: {name: @filename}
end

次に、必要に応じて、テンプレートを使用してファイルを直接提供できます。

于 2014-06-10T16:27:46.303 に答える