Copy a worksheet from one file to another using R

344 Views Asked by At

I want to copy an entire worksheet from an Excel file to another Excel file. I found cloneWorksheet() from openxlsx which clones a worksheet to the same excel file. I wonder if there is a way to paste it to a different, existing file?

I also found saveWorkbook() saves the sheet as a workbook, but I just want to save it as a sheet to the workbook without erasing other sheets in the workbook.

1

There are 1 best solutions below

0
U Bhalraam On

easily done.

library(xlsx)

sheet_1 <- read_excel("Name.xlsx", sheet = "sheet_you_want_cloning")

# imports the sheet you want to clone from Name.xlsx

write.xlsx(sheet_1, file = "Excel_File_you_want_to_add_the_sheet_to.xlsx", sheetName=nameofnewsheet, append=TRUE, row.names = FALSE)

# writes sheet_1 as a sheet called "nameofnewsheet" in the excel file called Excel_File_you_want_to_add_the_sheet_to.xlsx