How to write a data.frame data to current existing excel file (and the sheet name existing also---refer to the image)?
I tried xlsx
or openxlsx
, but all of them failed:
setwd('C:\\Users\\Administrator\\Desktop\\MFOLDER)
mydata <- data.frame(category=LETTERS,
amount=1:26)
# METHOD 1, show error as below
library(xlsx)
xlsx::write.xlsx(mydata,file='existingfile.xlsx',
sheetName = 'Sheet1',
append = TRUE)
# Error in .jcall(wb, "Lorg/apache/poi/ss/usermodel/Sheet;", "createSheet", :
# java.lang.IllegalArgumentException: The workbook already contains a sheet of this name
# METHOD 2, this code no error,but it remove current excel file and create new one
library(openxlsx)
openxlsx::write.xlsx(mydata,file='existingfile.xlsx',
sheetName = 'Sheet1',
append = TRUE)