What is the simplest way to convert a large Excel spreadsheet with multiple worksheets into .CSV files in R?
Note that I\'ve tested XLConnect and XLSX and found that m
http://rwiki.sciviews.org/doku.php?id=tips:data-io:ms_windows
EDIT: to address the read.xlsx option:
If you have Perl running, you need a current version of 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. F G 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)
This was done on a Mac and up until this question I had always stumbled at the installXLSXsupport() stage since I always got an error. This time I started up Perl from a Terminal command line, and got success after first setting up my personal configuration, defining CPAN mirrors on my continent, and I left perl running.
Here's a loop to write out all sheets:
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)
}
Updated answer based on readxl package.
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)