将Excel电子表格保存为.csv与R?

将具有多个工作表的大型Excel电子表格转换为R中的.CSV文件的最简单方法是什么?

请注意,我testing了XLConnect和XLSX,发现我的Excel表导致它崩溃。 所以我特别寻找一个不使用XLConnect或XLSX包的解决scheme。

http://rwiki.sciviews.org/doku.php?id=tips:data-io:ms_windows

编辑:解决read.xlsx选项:

如果你运行Perl,你需要一个当前版本的gdata

require(gdata) installXLSXsupport() #now the example from help(read.xls) # load the third worksheet, skipping the first two non-data lines... if( 'XLSX' %in% xlsFormats() ) # if XLSX is supported.. data <- read.xls(exampleFile2007, sheet="Sheet with initial text", skip=2) data #----------------------- X X.1 D E. FG Factor 1 NA FirstRow 1 NA NA NA Red 2 NA SecondRow 2 1 NA NA Green 3 NA ThirdRow 3 2 1 NA Red 4 NA FourthRow 4 3 2 1 Black #------------------------ write.csv(data) 

这是在Mac上完成的,直到这个问题我一直在installXLSXsupport()阶段偶然发现,因为我总是遇到一个错误。 这次我从terminal命令行启动了Perl,并在首次设置我的个人configuration后,在我的大陆上定义了CPAN镜像,并且让我的Perl运行。

这是一个循环来写出所有表:

 require(gdata) ## install support for xlsx files installXLSXsupport() excelFile <- ("/full/path/to/excelFile.xlsx") ## note that the perl scripts that gdata uses do not cope well will tilde expansion ## on *nix machines. So use the full path. numSheets <- sheetCount(excelFile, verbose=TRUE) for ( i in 1:numSheets) { mySheet <- read.xls(excelFile, sheet=i) write.csv(mySheet, file=paste(i, "csv", sep="."), row.names=FALSE) } 

基于readxl包更新了答案。

 library("readxl") #function to read all sheets of a workbook read_excel_allsheets <- function(filename) { sheets <- readxl::excel_sheets(filename) x <- lapply(sheets, function(X) readxl::read_excel(filename, sheet = X)) names(x) <- sheets x } sheetnames <- read_excel_allsheets("excelFile.xlsx") names(sheetnames)