简体   繁体   中英

Combining multiple worksheets from multiple excel workbooks into a single workbook via. R

Lets say I have two excel workbooks (eg A.xlsx and B.xlsx) that each contain multiple worksheets (eg "a1" and "a2" in A.xlsx; "b1", "b2", and "b3" in B.xlsx). Note that these worksheets all have their own conditional formatting, filtering, zoom, and other viewing options.

Given this setup, is it possible to generate a combined workbook via. R (eg C.xlsx) featuring the 5 worksheets from A.xlsx and B.xlsx (eg "a1", "a2", "b1", "b2", and "b3" in C.xlsx) that furthermore retains all of the original conditional formatting, filtering, zoom, and other viewing options? Thank you for your help!

We could read both the datasets together in map2 . Get the sheet names from 'A.xlsx' and 'B.xlsx' data. Create a named vector of filepath with sheet names, loop over the vector with imap , read the sheets with read_excel , bind those together after creating a column for identification of sheetname, and combine the list output from imap to a single list with imap

library(purrr)
library(dplyr)
library(readxl)
library(openxlsx)

# // replace the path/to/your - actual path
fileA <- 'path/to/your/A.xlsx'
fileB <- 'path/to/your/B.xlsx'
nmA <- excel_sheets(path = fileA)
nmB <- excel_sheets(path = fileB)
nm1A <- setNames(rep(fileA, length(nmA)), nmA)
nm1B <- setNames(rep(fileB, length(nmB)), nmB)
lstC <- imap(c(nm1A, nm1B), ~ 
              read_excel(.x, sheet = .y) %>%
                  mutate(sheetname = .y))
names(lstC) <- c(nmA, nmB)
write.xlsx(lstC, '/path/to/your/C.xlsx')

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