简体   繁体   English

读取同一目录中的所有 Excel 工作簿,每个工作簿都有多个工作表,并将每个工作表导出为 R 中的 a.csv

[英]Read all Excel workbooks in the same directory each with multiple sheets and export each sheet as a .csv in R

I am trying to read all sheets from multiple workbooks and then export each sheet as its own.csv.我正在尝试从多个工作簿中读取所有工作表,然后将每个工作表导出为自己的.csv。 I ran this code:我运行了这段代码:

files.to.read <- list.files(path = "/Users/filelocation", pattern="xlsx",
                            full.names = TRUE)

I then applied this line of code to turn all xlsx in directory to csv, however it doesn't account for the individual sheets within each xlsx file.然后我应用这行代码将目录中的所有 xlsx 转换为 csv,但是它不考虑每个 xlsx 文件中的各个工作表。

lapply(files.to.read, function(x) {
  df = read_excel(x, sheet=1)
  write.csv(df, gsub("xlsx", "csv", x), row.names=FALSE)
})

What's the best way to account for the sheets as well?考虑工作表的最佳方法是什么? I tried using excel_sheets() and map_df() in the function but it keeps returning我尝试在 function 中使用 excel_sheets() 和 map_df() 但它一直返回

Error: `path` must be a string

Please help and thank you commmunity!请帮助并感谢社区!

You need to first read all the workbooks then from each workbook read all the sheets in it.您需要先阅读所有工作簿,然后从每个工作簿中阅读其中的所有工作表。

Using lapply you can try:使用lapply您可以尝试:

library(readxl)

files.to.read <- list.files(path = "/Users/filelocation", 
                            pattern="xlsx", full.names = TRUE)

lapply(files.to.read, function(x) {
  sheets <- excel_sheets(x)
  lapply(sheets, function(y) {
    df = read_excel(x, sheet=y)
    write.csv(df, paste0(tools::file_path_sans_ext(basename(x)), y, '.csv'), 
              row.names=FALSE)  
  })
})

It hard to say which cause the errors without knowing the underline data and what cause the errors so I would modify your code which ensure that it read each sheet and when an error happened it output that error.在不知道下划线数据的情况下很难说导致错误的原因以及导致错误的原因,因此我将修改您的代码,以确保它读取每张纸,并且在发生错误时会出现 output 那个错误。 Please try it and see what is the cause of the error:请尝试一下,看看错误的原因是什么:

library(readxl)

files.to.read <- list.files(path = "/Users/filelocation", pattern="xlsx",
  full.names = TRUE)

read_excel_to_csv <- function(x) {
  sheets <- excel_sheets(x)
  lapply(sheets, function(y) {
    tryCatch({
      df = read_excel(x, sheet=y)
      write.csv(df,
        path = paste0(tools::file_path_sans_ext(basename(x)), "_", y, '.csv'),
        row.names=FALSE)  
    }, error = function(e) {
      print(paste0("Error at file: ", x, " --- sheet: ", y))
    })    
  })
}

lapply(files.to.read, read_excel_to_csv)

@Modify to use part of code for @Ronak's answer @Modify 将部分代码用于@Ronak 的答案

暂无
暂无

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

相关问题 导入多个 excel 工作簿,每个工作簿包含 R 中的多个工作表 - Import multiple excel workbooks each containing multiple sheets in R 循环将 r 中的多个数据框导出到 Excel 并命名每个工作表 - Loop to export multiple dataframes in r to Excel and Name Each Sheet R Shiny将两个数据框导出到同一Excel工作表中的两个工作表中 - R Shiny export two dataframes into two sheets in same excel sheet 有没有办法读取 R 中的整个文件文件夹,然后导出为 1 个 excel 工作簿,每个文件输出为一张表? - Is there a way to read an entire folder of files in R then export as 1 excel workbook with each files output as a sheet? 我在尝试将多个 XLS 表导出到它自己的 R 文件中时遇到问题 - I'm having a problem trying to export multiple XLS Sheets each into it's own csv file in R R:从 dataframe 导出多个 Excel 工作簿 - R: Export Multiple Excel Workbooks from dataframe R - 如何阅读每个excel文件的最后一页? - R - How to read last sheet of each excel file? 如何创建多个工作簿,每个工作簿中有多个工作表 - how to create multiple workbooks that have multiple sheets in each 如何从多个目录中读取数据集并将R中每个目录中的所有数据集合并? - How to read in datasets from multiple directories and merge all datasets in each directory in R? 将数据框导出到 R 中的多个 excel 工作簿,条件格式完好无损 - Export dataframe to multiple excel workbooks in R with conditional formatting intact
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM