复制一个工作表从一个文件到另一个使用R。

huangapple go评论67阅读模式
英文:

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 &lt;- read_excel(&quot;Name.xlsx&quot;, sheet = &quot;sheet_you_want_cloning&quot;)

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

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

# writes sheet_1 as a sheet called &quot;nameofnewsheet&quot; in the excel file called Excel_File_you_want_to_add_the_sheet_to.xlsx   

huangapple
  • 本文由 发表于 2023年6月26日 05:00:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76552388.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定