以CSV格式更改显示标题的字体颜色

时间:2014-12-29 14:09:42

标签: ruby-on-rails csv haml

我正在从rails导出csv数据。我在这里遵循教程:http://nithinbekal.com/posts/rails-csv-export/

在控制器中:

def export
  @team_status_updates = @team.team_status_updates.team_updates
  respond_to do |format|
    format.html
    format.csv do
      headers['Content-Disposition'] = "attachment; filename=\"Team-report-#{DateTime.now.strftime("%Y-%m-%d-%H-%M-%S-%L")}.csv\""
      headers['Content-Type'] ||= 'text/csv'
    end
  end
end

在视图中输出export.csv.haml

- headers = ['DATE', 'TYPE', 'TEAM MEMBER', 'STATUS', 'COMMITMENT', 'QUANTITY', 'HOUR-DAY-WEEK?', 'EFFORT']
= CSV.generate_line headers, :col_sep => "\\"
- @team_status_updates.each do |team_updates|
  - if team_updates[1].length > 0
    - team_updates[1].group_by { |d| d.created_at }.each do |status_updates|
    - member_wise = status_updates[1].group_by { |d| d.member }
    - member_wise.each_with_index do |status_update, index|
      - standup_questions = status_update[1].sort!
        - standup_questions.each do |update|
          = CSV.generate_line([update.created_at.strftime("%a %b %d %Y"), update.kind, update.member.full_name_titlecase, update.body, commitments_url(update.commitment), update.commitment.time_to_complete_time,  update.commitment.time_to_complete_units, update.commitment.time_to_complete], :col_sep => "\\")

这里的问题是,我需要显示标题“red”font-color。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

查看来自rails cast的exporting-csv-and-excel视频。像@matt说CSV是计划文本XLS可以包含格式。

主要说明: 在 config / initializers / mime_types.rb 文件中,请务必添加Mime::Type.register "application/xls", :xls

并在此处创建export.xls.haml,您可以在haml中添加一个表格,如

<?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>

现在位于 products_controller.rb

def export
  @team_status_updates = @team.team_status_updates.team_updates
  respond_to do |format|
    format.html
    format.csv do
      headers['Content-Disposition'] = "attachment; filename=\"Team-report-#{DateTime.now.strftime("%Y-%m-%d-%H-%M-%S-%L")}.csv\""
      headers['Content-Type'] ||= 'text/csv'
    end
    format.xls 
  end
end

我希望这能够提供帮助。