Railscasts Episode #362 - Exporting to Excel: How to avoid the warning message given by Excel 2010 when opening the file?

我与影子孤独终老i 提交于 2019-12-01 17:17:16

The xls file that is generated by the Railscasts application is actually an XML file in the old Excel 2003 XML format.

Recent versions of Excel have a feature called Extension Hardening that generate this warning when the file format doesn't match the file extension:

The alert is a new security feature in Excel 2007 called Extension Hardening, which ensures that the file content being opened matches the extension type specified in the shell command that is attempting to open the file. Because the MIME types listed above are associated with the .XLS extension, the file must be in XLS (BIFF8) file format to open without this warning prompt.

In order to avoid this warning you will have to generate an Excel file in a format that matches the file extension. Editing the registry as suggested as a workaround in the above link probably isn't workable in practice. Changing the extension to xml might also work.

As alternatives writeexcel for xls, write_xlsx for xlsx and AXLSX (that you mention above) are good options.

In fact, the code generated:

<?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">Release Date</Data></Cell>
        <Cell><Data ss:Type="String">Price</Data></Cell>
      </Row>
    <% @products.each do |product| %>
      <Row>
        <Cell><Data ss:Type="Number"><%= product.id %></Data></Cell>
        <Cell><Data ss:Type="String"><%= product.name %></Data></Cell>
        <Cell><Data ss:Type="String"><%= product.released_on %></Data></Cell>
        <Cell><Data ss:Type="Number"><%= product.price %></Data></Cell>
      </Row>
    <% end %>
    </Table>
  </Worksheet>
</Workbook>

is XML (XLS is a binary format, XLST a zipped format)

Renaming the file to .xml should work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!