Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

openxlsx currency style - Is there a rounding option?

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")
like image 382
IceCreamToucan Avatar asked May 18 '18 14:05

IceCreamToucan


1 Answers

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

like image 177
eipi10 Avatar answered Sep 27 '22 22:09

eipi10