0
<?xml version="1.0"?>
<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:ss="urn:schemas-microsoft-com:office:spreadsheet"
  xmlns:html="http://www.w3.org/TR/REC-html40">
  <Worksheet ss:Name="Sheet1">
    <Table>
      <Row>
        <Cell><Data ss:Type="String">ID</Data></Cell>
        <Cell><Data ss:Type="String">Name</Data></Cell>
        <Cell><Data ss:Type="String">Email</Data></Cell>
        <Cell><Data ss:Type="String">Registered On</Data></Cell>
      </Row>
    <% users.compact.each do |user| %>
      <Row>
        <Cell><Data ss:Type="Number"><%= user.id %></Data></Cell>
        <Cell><Data ss:Type="String"><%= user.name %></Data></Cell>
        <Cell><Data ss:Type="String"><%= user.email%></Data></Cell>
        <Cell><Data ss:Type="Date"><%= user.created_at.to_s(:long)%></Data></Cell>
      </Row>
    <% end %>
    </Table>
  </Worksheet>
</Workbook>

Controller:

def index
  respond_to do |format|
    format.html
    format.xls
  end
end

index.html.haml file links to some_path(format: "xls")

The above code is located in index.xls.erb. When I go download the xls file, I open a spreadsheet that just has the code above pasted into it. Any clue what I'm doing wrong?

davetakahashi
  • 494
  • 1
  • 4
  • 12
user1261445
  • 291
  • 1
  • 6
  • 15

1 Answers1

0

have you set a content type?

try adding

Mime::Type.register 'application/vnd.ms-excel', :xls

to config/initializers/mime_types.rb

Or maybe even try using .xlsx as the is the open document version of excel spreadsheet, as opposed the binary/biff version above. If you go this route, you will need to adjust the content type to be

Mime::Type.register 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', :xlsx 

and make your code use format.xlsx as opposed to format.xls

Doon
  • 19,719
  • 3
  • 40
  • 44