Search code examples
rexcelxlconnect

Thousand seperator - format excel cell using XLConnect and R


I export tables with large values to .xlsx using XLConnect.

Is there a way for the results to be written into an excel-cell with activated thousand separators?

library(XLConnect)
#example for a large value
a <- 10000000000

wb <- loadWorkbook("sof_q.xlsx"), create = TRUE)
    cs <- createCellStyle(wb)
    setDataFormat(cs, format = "0.00")
createSheet(wb, name = "a")
writeWorksheet(wb,a,"a",startRow = 1, startCol = 1, header = TRUE)
    rc = expand.grid(row = 1:2, col = 1:2)
    setCellStyle(wb, sheet = "a", row = rc$row, col = rc$col, cellstyle = cs)
    setColumnWidth(wb, sheet = "a", column = 1:5, width = -1)
saveWorkbook(wb)

In Excel, a should look like this

10.000.000.000

Using

setDataFormat(cs, format = "0,000,000.00")

might work but in the case of shorter values, I have values like

0,032,666.29

Solution

  • Use # for a digit placeholder:

    setDataFormat(cs, format = "###,###.00")
    

    or

    setDataFormat(cs, format = "###,##0")