导出Excel时如何在不包含任何宝石的情况下包括背景颜色?

时间:2018-11-08 06:21:20

标签: ruby-on-rails excel

我看到了这篇文章,其中提供了一个简单的示例,说明如何在不使用Rails的任何gem的情况下将数据导出到Excel工作表:

https://makandracards.com/makandra/31567-exporting-to-excel-from-rails-without-a-gem

我相信上面的文章基于同一主题的RailsCast教程:

http://railscasts.com/episodes/362-exporting-csv-and-excel?view=asciicast

我想知道如何包括背景色和行边框(以及边框颜色)。请帮忙!

1 个答案:

答案 0 :(得分:2)

如果您使用的是“手动” XML方法,那么正式的Microsoft XML Spreadsheet Reference就是一个很好的开始。

有很多可用的选项,例如带有边框的文档可能看起来像这样。

<?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">
  <Styles> 
      <Style ss:ID="h1" ss:Name="First">
        <Borders>
          <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>
          <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>
          <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/>
          <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>
        </Borders> 
      </Style>   
  </Styles> 

  <Worksheet ss:Name="Sheet1">
    <Table>
      <Row>
        <Cell ss:StyleID="h1"><Data ss:Type="String">ID</Data></Cell>
        <Cell ss:StyleID="h1"><Data ss:Type="String">Name</Data></Cell>
        <Cell ss:StyleID="h1"><Data ss:Type="String">Release Date</Data>    </Cell>
        <Cell ss:StyleID="h1"><Data ss:Type="String">Price</Data></Cell>
      </Row>
    </Table>
  </Worksheet>
</Workbook>

您可以在工作簿部分的开头定义不同的样式,然后将它们应用于不同的单元格。