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:
$103.00
.numfmt = 'CURRENCY0'
does not workIf 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")
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With