如何使用axlsx gem将Excel单元格格式化为货币-Ruby on Rails

时间:2019-03-06 05:31:42

标签: ruby-on-rails axlsx

我了解到您可以像这样在Excel中设置单元格的类型:

:types => [nil, :integer, :string]

但是,我查看了所有可能类型的列表,但仅找到integerfloat,但没有看到currency选项。

如何将单元格设置为货币格式,以便自动包含$前缀和千位分隔符?

请帮助!

2 个答案:

答案 0 :(得分:1)

您可以根据需要定义自定义样式。

# an example of applying specific styles to specific cells
require "rubygems" # if that is your preferred way to manage gems!
require "axlsx"

p = Axlsx::Package.new
ws = p.workbook.add_worksheet

# define your styles    
currency = ws.styles.add_style(format_code: "$#,##0;[Red]$-#,##0",
                              border: Axlsx::STYLE_THIN_BORDER).
ws.add_row ["Q1", 4000, 40], style: [currency]

Additional styling documentation

答案 1 :(得分:0)

以下样式会将货币添加2个小数位,例如:$ 4200.32

currency = ws.styles.add_style(format_code: "$#,##0.00;[Red]$-#,##0.00",
                              border: Axlsx::STYLE_THIN_BORDER)