简体   繁体   中英

Joining multiple Excel workbooks and worksheets using R

I have a series of Excel workbooks and worksheets that I would like to join in R, but looking at packages like XLConnect or xlsx I only see append and cell merge functions (there are no merge worksheet or even merge workbook functions, for instance). Does anyone have a suggestion as to which package I should use and which function thereafter (both for worksheets and workbooks). Thanks!

You can merge two Excel files with the following function:

library(RDCOMClient)
merge_Two_Excel_Files <- function(path_Excel_File1,
                                  path_Excel_File2,
                                  path_Excel_File_Output)
{
  xlApp <- COMCreate("Excel.Application")
  xlWbk1 <- xlApp$Workbooks()$Open(path_Excel_File1)
  xlWbk2 <- xlApp$Workbooks()$Open(path_Excel_File2)
  nb_Sheets1 <- xlWbk1$Sheets()$Count()
  nb_Sheets2 <- xlWbk2$Sheets()$Count()

  for(l in 1 : nb_Sheets1)
  {
    wbSheet <- xlWbk1$Worksheets(l)$Copy(after = xlWbk2$Worksheets(l))
  }

  xlWbk2$SaveAs(path_Excel_File_Output)
  xlWbk1$Close()
  xlWbk2$Close()
  xlApp$Quit()

  rm(list = ls())
  gc()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM