If I have an excel workbook in R called "wb" and in that workbook I have two sheets one called sheet A and one called sheet C . If I want to create a new sheet that will go between A and B called C how would I do that ?
Asked
Active
Viewed 82 times
0
-
1I know the function `worksheetorder` can be used to swap sheet place, and `writeData()` has and argument to specify the index where the sheet you write should be. But I don't know if it works with names instead of index. Anyway, you can do `writeData(wb = data, sheet = 2)` to write a new sheet between 2 sheets that alrdeay exist – MonJeanJean Jul 23 '21 at 07:13
-
1Does [this](https://stackoverflow.com/questions/34731382/add-sheet-to-excel-file) or [this](https://stackoverflow.com/questions/49181980/r-append-a-worksheet-to-an-excel-workbook-without-reading-the-entire-workbook) or [this](https://www.r-bloggers.com/2019/08/creating-excel-workbooks-with-multiple-sheets-in-r/) help? – Naresh Jul 23 '21 at 07:19
1 Answers
0
You can consider the following approach. This approach only works on Windows.
library(RDCOMClient)
path_Excel_File_Output <- "D:\\output.xlsx"
path_Excel_File1 <- "D:\\test_File_2.xlsx"
path_Excel_File2 <- "D:\\test_File_1.xlsx"
xlApp <- COMCreate("Excel.Application")
xlWbk1 <- xlApp$Workbooks()$Open(path_Excel_File1)
xlWbk2 <- xlApp$Workbooks()$Open(path_Excel_File2)
xlWbk1$Worksheets(1)$Copy(after = xlWbk2$Worksheets(1))
xlWbk2$SaveAs(path_Excel_File_Output)
xlWbk1$Close()
xlWbk2$Close()
xlApp$Quit()

Emmanuel Hamel
- 1,769
- 7
- 19