Search code examples
rxlconnect

R XLConnect write DF to existing workbook without column names


I'm trying to write data to the existing Excel template. I want to preserve cell formatting, so I use XLConnect package. Say I have nine cells, from G9 to I11, that I want to fill with numbers.

I create a DF and next, following the instructions from this post, I use XLConnect package:

install.packages("XLConnect")
library(XLConnect)

df <- data.frame(x = c(1,2,3),
                 y = c(10, 20, 30),
                 z = c(100, 200, 300))

wb <- XLConnect::loadWorkbook("C:/...../test.xlsx") # Here is a path to my file

XLConnect::setStyleAction(wb,XLC$"STYLE_ACTION.NONE")

XLConnect::writeWorksheet(wb, df, sheet = "my_sheet", startRow = 9, startCol = 7)

saveWorkbook(wb)

The problem is that the whole DF, together with column names, is written to Excel, while I want only numbers. I have nine cells to fill and nine values in the DF (1,2,3,10,20,30,100,200,300), except for the headers. Any ideas how to do it?


Solution

  • XLConnect::writeWorksheet(wb, df, sheet = "my_sheet", startRow = 9, startCol = 7, header = FALSE)
    

    Does this work?