2016-08-19 10 views
0

CSVが完全に正しく生成されています。私は大胆なCSVヘッダーを印刷したい。太字で印刷する方法はありますか?RailsでCSVヘッダーを太字で表示

シンプルなCSVを印刷するコードです。

<%= CSV.generate do |csv| 

    csv << ["REF", "Customer Name", "Property Address", "Moving Date", "Mobile Phone", "Operator", "Status"] 
    @results.each do |result| 
     csv << [result.id, result.customer_name, result.address_label, result.moving_date.strftime("%b %d, %Y"), "", result.operator.try(:name), result.status ] 

end.html_safe 
%> 

ありがとうございます!

+0

VALUE」という値をラップしてみましたか?例えば「REF」の代わりに「REF」となります。 –

+0

@ TamerShlashそれは奇妙に解析されるか、またはパースされますが、平文で取得されます。 これはできないと私はかなり確信しています。 CSVは書式設定をサポートしていません(もちろん、それを大文字にして印刷する以外に) - 単純なデータです。書式設定を行う場合、CSVはファイルタイプではありません。そして、ああ、ここ - あなたの状況をよりよく説明します。 [link](http://stackoverflow.com/questions/3749020/how-to-bold-csv-data-in-excel) – jemonsanto

+1

私はあなたの質問を考えていました。多くの人がCSVをXLSと混同しています.Explore openほとんどのCSVファイル、CSVには実際に何をしたいのですか? – MZaragoza

答えて

1

私が手伝って、そのビューへの私のxlsxビュー

#app/views/products/index.xlsx.axlsx 
wb = xlsx_package.workbook 
wb.styles do |style| 
    header_cell = style.add_style(b: true) 

    wb.add_worksheet(name: "Products") do |sheet| 
    sheet.add_row ["Title", "Price"], :style=>[header_cell, header_cell] 
    @products.each do |product| 
    sheet.add_row [product.title, product.price] 
    end 
    end 
end 

リンクの私のコントローラ

class ProductsController < ApplicationController 
    def index 
    @products = Product.order('created_at DESC') 
    respond_to do |format| 
     format.html 
     format.xlsx { 
     response.headers['Content-Disposition'] = 'attachment; filename="all_products.xlsx"' 
     } 
    end 
    end 
end 

例のその本当に簡単な

例には「axlsx_rails」と呼ばれる宝石を使用

<%= link_to 'Download as .xlsx', products_path(format: :xlsx) %> 

アクションのサンプルコードは、私はあなたがすべてを見ることができるのgitリポジトリを作成した見たい場合は、私これを行うことができない