0

i have table like the following enter image description here

now, i want to export this to excel, so i can open it in ms excel

3 Answers 3

3

You can use FasterCSV gem.

You can either use to_csv method.

def index @records = .... respond_to do |format| format.html # index.html.erb format.xml { render :xml => @records } format.csv { @records.to_csv } end end 

or customize the output and use send_data method in the controller.

format.csv do csv_string = FasterCSV.generate do |csv| # header row csv << ["id", "Column1", "Column1"] # data rows @records.each do |r| csv << [r.id, r.column1, r.column2] end # send it to the browser send_data csv_string, :type => 'text/csv; charset=iso-8859-1; header=present', :disposition => "attachment; filename=records.csv" end 
Sign up to request clarification or add additional context in comments.

Comments

1

I would advice to use Spreadsheet which is mature. I'm using it with Rails 3 with no problems.

The overall process would be:

book = Spreadsheet::Workbook.new sheet = book.create_worksheet :name => 'Customers' sheet.row(0).concat %w{Name Country Acknowlegement} book.write '/path/to/output/excel-file.xls' 

2 Comments

Mature with a 0.x version number. hmmmm
Spreadsheet works great for me, too, until the size of the worksheet gets too big. It still works, but is really slow creating XLS file after db queries have finished.
0

Ruby 1.9 has a built in CSV library with very similar API as the FasterCSV gem (actually the gem got integrated into Ruby!).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.