Skip to content

Instantly share code, notes, and snippets.

@twinge
Created April 24, 2010 17:47
Show Gist options
  • Select an option

  • Save twinge/377804 to your computer and use it in GitHub Desktop.

Select an option

Save twinge/377804 to your computer and use it in GitHub Desktop.
## Controller
respond_to do |format|
format.xls do
filename = @search_for.gsub(';',' -') + ".xls"
#this is required if you want this to work with IE
if request.env['HTTP_USER_AGENT'] =~ /msie/i
headers['Pragma'] = 'public'
headers["Content-type"] = "text/plain"
headers['Cache-Control'] = 'no-cache, must-revalidate, post-check=0, pre-check=0'
headers['Content-Disposition'] = "attachment; filename=\"#{filename}\""
headers['Expires'] = "0"
else
headers["Content-Type"] ||= 'text/xml'
headers["Content-Disposition"] = "attachment; filename=\"#{filename}\""
end
render :action => 'excel', :layout => false
end
end
## view
xml.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8"
xml.Workbook({
'xmlns' => "urn:schemas-microsoft-com:office:spreadsheet",
'xmlns:o' => "urn:schemas-microsoft-com:office:office",
'xmlns:x' => "urn:schemas-microsoft-com:office:excel",
'xmlns:html' => "http://www.w3.org/TR/REC-html40",
'xmlns:ss' => "urn:schemas-microsoft-com:office:spreadsheet"
}) do
xml.Worksheet 'ss:Name' => 'Directory Listing' do
xml.Table do
# Header
xml.Row do
@view.columns.each do |column|
xml.Cell {xml.Data column.title, 'ss:Type' => 'String'}
end
end
# Rows
for person in @people
xml.Row do
@view.columns.each do |column|
value = case column.column_type
when 'date'
format_date(person[column.safe_name])
when 'gender'
@person.human_gender(person[column.safe_name])
else
person[column.safe_name]
end
xml.Cell {xml.Data value, 'ss:Type' => 'String'}
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment