Skip to content

Instantly share code, notes, and snippets.

@gstark
Created May 22, 2009 17:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gstark/116265 to your computer and use it in GitHub Desktop.
Save gstark/116265 to your computer and use it in GitHub Desktop.
controller:
def task_summary
# Find all the tasks
tasks = Task.find(:all)
# Create an IO buffer for the generated excel data
target_string = StringIO.new
# Ask for excel data for these tasks to be put in the target_string
excel_summary( tasks, target_string )
# Send back just the excel data string and suggest a filename and a MIME type
send_data( target_string.string, :filename => "my_excel_file.xls", :type => "application/excel" )
end
helper / model:
def excel_summary( tasks, target_io )
workbook = Excel.new( target_io )
format = Format.new
worksheet = workbook.create_worksheet
worksheet.write(0, 0, "ID")
worksheet.write(0, 1, "Name")
# Loop over all the tasks and create rows for each
tasks.each_with_index do |task,row|
worksheet.write(row, 0, task.id)
worksheet.write(row, 1, task.Task)
end
workbook.close
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment