I learnt that you could format the types of cells in an Excel like this:
:types => [nil, :integer, :string]
However I looked at the list of all types possible and I find only integer
and float
but I don't see a currency
option.
How can I format a cell as a currency so that the $ prefix and thousand separators are included automatically?
Please help!
You can define custom styles as you need them.
# 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]
Below style will add 2 decimal places to currency eg: $4200.32
currency = ws.styles.add_style(format_code: "$#,##0.00;[Red]$-#,##0.00",
border: Axlsx::STYLE_THIN_BORDER)
I know this is an old thread, but in case anyone stumbles across it like I did, this solution should also work, with the caxlsx gem ( https://rubygems.org/gems/caxlsx ):
# I discovered the 'num_fmt' usage in this source_code file:
# https://github.com/caxlsx/caxlsx/blob/946f287a37c7a5d2b4fe2ae8d109848e257241b1/lib/axlsx/stylesheet/num_fmt.rb
xlsx_package = Axlsx::Package.new
wb = xlsx_package.workbook
currency_style = wb.styles.add_style({num_fmt: 8})
The 'format_code' option wb.styles.add_style(format_code: "$#,##0.00;[Red]$-#,##0.00")
didn't work for my use-case. My spreadsheet reader (LibreOffice) would only recognize that option as a 'custom' formatted cell, not a 'currency' cell.
edit: They did use it in one of their examples. https://github.com/caxlsx/caxlsx/blob/946f287a37c7a5d2b4fe2ae8d109848e257241b1/examples/number_format_example.md
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.