5

The createStyle function in the openxlsx package has an argument numFmt, which allows you to create an excel format to be applied to specific cells in a .xlsx file. You can round values by specifying numFmt = '0', and you can apply a currency format by specifying numFmt = "CURRENCY".

Is there a way to specify a rounded currency format? I've tried the following:

  • Rounding the values in the data frame doesn't work, because the excel cells still display the cents e.g. $103.00.
  • numfmt = 'CURRENCY0' does not work

If not, is there another package which allows you to specify the formats of excel cells, and allows rounding of currency-formatted cells?

Edit:

This gives me what I wanted (Currency format, with commas, no cents)

createStyle(numFmt="$0,0")
4

2 回答 2

8

You can set numFmt="$0" to get rounded currency values. For example:

library(openxlsx)

mtcars$hp = 100 * mtcars$hp

wb = createWorkbook()
sht = addWorksheet(wb, "test")
writeData(wb, sht, mtcars)

sty1 = createStyle(numFmt="$0")
sty2 = createStyle(numFmt="$0.00")
sty3 = createStyle(numFmt="$0,0.00")

addStyle(wb, sht, sty1, rows=2:(nrow(mtcars)+1), cols=1)
addStyle(wb, sht, sty2, rows=2:(nrow(mtcars)+1), cols=5)
addStyle(wb, sht, sty3, rows=2:(nrow(mtcars)+1), cols=4)

saveWorkbook(wb, "test.xlsx")

Here's what the resulting Excel file looks like:

enter image description here

于 2018-05-18T14:21:50.217 回答
0

just incase you want to format in other currencies, you could try this

style = createStyle(numFmt="[$CURRENCY SYMBOL]#,##0")

E.g

style = createStyle(numFmt="[$UGX]#,##0")

于 2020-04-09T10:14:32.923 回答