英文:
Copy a worksheet from one file to another using R
问题
我想复制一个Excel文件中的整个工作表到另一个Excel文件。我发现openxlsx
中的cloneWorksheet()
可以将工作表克隆到同一个Excel文件。我想知道是否有办法将其粘贴到不同的现有文件中?
我还发现saveWorkbook()
会将工作表保存为工作簿,但我只想将其保存为工作簿中的一个工作表,而不会擦除工作簿中的其他工作表。
英文:
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
得分: 1
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
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论