在 Rails 4 中导出 Excel,无法在 Microsoft Excel 中打开,但在 Libre Office 中有效

Exporting Excel in Rails 4, unable to open in Microsoft Excel but works in Libre Office

我在 Microsoft Office Excel 中打开 generated/exported excel 时遇到问题。

但它在 Libre Office 中运行良好。

我试着用下面的代码做了一个简单的测试。

控制器:

  respond_to do |format|
    format.html   
    format.xls do 
      headers['Content-Type'] ||= 'text/xls'
      headers['Content-Disposition'] = "attachment; filename='Report.xls'"
    end
  end

查看代码:

  <?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>
      <% 5.times do |product| %>
        <Row>
          <Cell><Data ss:Type="Number"><%= product %></Data></Cell>
          <Cell><Data ss:Type="String"><%= product %></Data></Cell>
          <Cell><Data ss:Type="String"><%= product %></Data></Cell>
          <Cell><Data ss:Type="Number"><%= product %></Data></Cell>
        </Row>
      <% end %>
      </Table>
    </Worksheet>
  </Workbook>

我是按照本教程学习的,railscasts/362-exporting-csv-and-excel

我尝试在视图中使用简单的 html,它起作用了。

因此,我认为使用 XML 作为模板存在问题。

请帮忙解决问题。

Here is a sample generated excel file.

XML spredsheet Microsoft Office Excel 2003 不支持新版本

首先,扩展名应该是 .xml,而不是 .xls.xls 是一种二进制格式:即使您的 .xls.xml 文件有正确的语法,Excel 也会在启动时报错。

也不应该是.xlsx。 xlsx 是基于 XML 的,但它是一个完整的压缩结构。你可以做到,但你需要更复杂的代码和 ruby​​zip。

我的建议是编写纯 .csv 文件。使用 Excel/OpenOffice.

易于编写、易于阅读和易于打开

如果你真的想写一个 Microsoft Office XML format : 我打开了您随 OpenOffice 提供的 xls 文件。我从 OpenOffice 中将其保存为 xlsx,用 Excel 打开它,将其保存为 xml(2003 版本),并删除了所有可选属性。在这里:

<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<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">
 <Styles>
  <Style ss:ID="Default" ss:Name="Normal">
   <Font ss:FontName="Arial" x:Family="Swiss"/>
  </Style>
  <Style ss:ID="titleStyle">
   <Alignment ss:Vertical="Center"/>
   <Borders>
    <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>
    <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>
    <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/>
    <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>
   </Borders>
   <Font ss:FontName="Arial" x:Family="Swiss" ss:Size="15" ss:Color="#FFFFFF"
    ss:Bold="1"/>
   <Interior ss:Color="#E22828" ss:Pattern="Solid"/>
  </Style>
  <Style ss:ID="dataStyle">
   <Alignment ss:Vertical="Center" ss:WrapText="1"/>
   <Borders>
    <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>
    <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>
    <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/>
    <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>
   </Borders>
   <Font ss:FontName="Arial" x:Family="Swiss" ss:Size="11" ss:Color="#000000"/>
  </Style>
 </Styles>
 <Worksheet ss:Name="Applicant Data">
  <Table>
   <Row>
    <Cell ss:StyleID="titleStyle"><Data ss:Type="String">Code No.</Data></Cell>
    <Cell ss:StyleID="titleStyle"><Data ss:Type="String">Country</Data></Cell>
    <Cell ss:StyleID="titleStyle"><Data ss:Type="String">Name</Data></Cell>
    <Cell ss:StyleID="titleStyle"><Data ss:Type="String">Email</Data></Cell>
    <Cell ss:StyleID="titleStyle"><Data ss:Type="String">Address</Data></Cell>
   </Row>
   <Row>
    <Cell ss:StyleID="dataStyle"><Data ss:Type="String">0</Data></Cell>
    <Cell ss:StyleID="dataStyle"><Data ss:Type="String">0</Data></Cell>
    <Cell ss:StyleID="dataStyle"><Data ss:Type="String">0</Data></Cell>
    <Cell ss:StyleID="dataStyle"><Data ss:Type="String">0</Data></Cell>
    <Cell ss:StyleID="dataStyle"><Data ss:Type="String">0</Data></Cell>
   </Row>
   <Row>
    <Cell ss:StyleID="dataStyle"><Data ss:Type="String">1</Data></Cell>
    <Cell ss:StyleID="dataStyle"><Data ss:Type="String">1</Data></Cell>
    <Cell ss:StyleID="dataStyle"><Data ss:Type="String">1</Data></Cell>
    <Cell ss:StyleID="dataStyle"><Data ss:Type="String">1</Data></Cell>
    <Cell ss:StyleID="dataStyle"><Data ss:Type="String">1</Data></Cell>
   </Row>
   <Row>
    <Cell ss:StyleID="dataStyle"><Data ss:Type="String">2</Data></Cell>
    <Cell ss:StyleID="dataStyle"><Data ss:Type="String">2</Data></Cell>
    <Cell ss:StyleID="dataStyle"><Data ss:Type="String">2</Data></Cell>
    <Cell ss:StyleID="dataStyle"><Data ss:Type="String">2</Data></Cell>
    <Cell ss:StyleID="dataStyle"><Data ss:Type="String">2</Data></Cell>
   </Row>
   <Row>
    <Cell ss:StyleID="dataStyle"><Data ss:Type="String">3</Data></Cell>
    <Cell ss:StyleID="dataStyle"><Data ss:Type="String">3</Data></Cell>
    <Cell ss:StyleID="dataStyle"><Data ss:Type="String">3</Data></Cell>
    <Cell ss:StyleID="dataStyle"><Data ss:Type="String">3</Data></Cell>
    <Cell ss:StyleID="dataStyle"><Data ss:Type="String">3</Data></Cell>
   </Row>
  </Table>
 </Worksheet>
</Workbook>