简体   繁体   English

如何使用 R 将目录中的所有 xlsx 文件组合成单个 xlsx 文件?

[英]How to combine all xlsx files in a directory into a single xlsx file using R?

I have a directory with three xlsx files:我有一个包含三个 xlsx 文件的目录:

my_path <- c('Path/To/My/Stuff')

And each of the files has a name like this: my-file_1.xlsx , my-file_2.xlsx , my-file_3.xlsx每个文件都有这样的名称: my-file_1.xlsxmy-file_2.xlsxmy-file_3.xlsx

I want to combine these three files into a new xlsx workbook where each sheet contains all the data for one of these standalone files, and is also named after the standalone file (but only the file_N ending piece.我想将这三个文件合并到一个新的 xlsx 工作簿中,其中每个工作表都包含这些独立文件之一的所有数据,并且也以独立文件命名(但只有file_N结尾部分。

For example, the master workbook would be called my-complete-workbook.xlsx and it would have three tabs: file_1 , file_2 , file_3 .例如,主工作簿将被称为my-complete-workbook.xlsx并且它将具有三个选项卡: file_1file_2file_3

What's the best way to achieve this in R, ideally using the openxlsx library?在 R 中实现此目的的最佳方法是什么,最好使用openxlsx库?

You can try this code -你可以试试这个代码 -

library(openxlsx)

# Get list of files with .xlsx extension in current directory
filenames <- list.files(my_path, pattern = '\\.xlsx$', full.names = TRUE)

# Read all files
list_data <- lapply(filenames, read.xlsx)

# Assign correct names to list
names(list_data) <- tools::file_path_sans_ext(basename(filenames))

# Write sheets
write.xlsx(list_data, 'file.xlsx')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 R 将多个 csv 文件编译成单个 xlsx 文件? - How to compile multiple csv files into a single xlsx file using R? 如何使用 R 的 xlsx package 对齐 XLSX 文件的单元格? - How to align the cells of an XLSX file using R's xlsx package? 如何使用 R 将多个 csv 文件编译成单个 xlsx 文件,并在 csv 文件名之后命名每个选项卡? - How to compile multiple csv files into a single xlsx file using R and name each tab after csv file name? 如何使用 R 将 JSON 转换为 xlsx 文件 - How to convert JSON into xlsx file using R 如何在R中使用XLConnect读取.xlsx文件 - How to read .xlsx file using XLConnect in R 读取单个 xlsx 文件,执行条件格式化并导出为 R 中的多个 xlsx 文件 - Read in single xlsx file, perform conditional formatting and export as as multiple xlsx files in R 在 R 中读取单个 *.xlsx 文件,而不使用文件名但使用 *.xlsx - read a single *.xlsx file in R without the use of filename but utilizing the *.xlsx R如何将一个文件夹中的all.xlsx文件合并为一个文件 - How do I merge all .xlsx files in a folder into one file in R 如何将多个xlsx文件读入R,然后将它们存储为标有xlsx文件名的单独列表? - How can I read multiple xlsx files into R and then store them as seperate lists labeled with the xlsx file name? 在单张xlsx文件中附加r输出 - Appending r output in a single sheet of xlsx file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM