使用Ruby Spreadsheet gem,我可以将单元格格式化为货币吗?

时间:2012-12-07 10:04:34

标签: ruby-on-rails ruby export spreadsheet xls

行动中的相关代码如下:

# XLS
book = Spreadsheet::Workbook.new
sheet1 = book.create_worksheet
currency_format = Spreadsheet::Format.new :number_format => '0.00'

sheet1.row(0).concat 'Example header', 'Example header 2', 'Example header 3'



    @transactions.each_with_index do |tx, index|

      row = sheet1.row(index + 1)
      row.push tx.attr1, tx.attr2, tx.attr3      
      row.set_format(1, currency_format)
      row.set_format(2, currency_format)

    end


data = StringIO.new '';
book.write data


respond_to do |format|
  format.xls { send_data data.string, :disposition => "attachment; filename=transactions.xls" }
end

它导出正常,我可以将单元格格式化为数字,但Excel(在Mac上)不允许我总计数字格式列中的值。我可以选择货币格式吗?

由于

1 个答案:

答案 0 :(得分:3)

使用'$#,###.##'作为数字格式。有关详细信息,请参阅currency number format using spreadsheet gem