Created
May 22, 2009 17:51
-
-
Save gstark/116265 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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