1

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?

Chris
  • 251
  • 1
  • 7
  • Use the openxlsx package. You can then write data to an arbitrary range (see help for writeData function) and turn of the column names. – deschen Oct 17 '22 at 22:48

1 Answers1

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

Does this work?

Isaiah
  • 2,091
  • 3
  • 19
  • 28