繁体   English   中英

在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中打开生成/导出的Excel时遇到问题。

但它在自由办公室工作得很好。

我试着做一个简单的测试,如下面的代码。

控制器:

  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-exported-csv-and-excel

我试图在视图中使用简单的html,并且它有效。

所以,我认为使用XML作为模板存在问题。

请帮助解决问题。

这是一个生成excel文件的示例。

XML Spredsheet受Microsoft Office Excel 2003支持,而不是新版本支持

首先,扩展名应为.xml ,而不是.xls .xls是二进制格式:即使.xls文件具有正确的.xml文件语法,Excel也会在启动时抱怨。

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

我的建议是写简单的.csv文件。 易于编写,易于阅读,易于使用Excel / OpenOffice打开。

如果您真的想编写Microsoft Office XML格式 :我打开了您使用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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM