Read.xls - how to read in the sheet title

Given several files .xlswith a different number of sheets, I read them in Rusing read.xlsthe package gdata. I have two related problems (solving the second problem should solve the first):

  • It is not yet known how many sheets each .xlsfile will have , and in fact this value will differ from one file to another.
  • I need to write the sheet name, which is the relevant data

Right now, to resolve (1), I use try()and repeat sheet numbers until I find an error.

How can I get a list of sheet names so that I can iterate over them?

+5
source share
2 answers

. sheetCount sheetNames ( ) gdata. xls <- "a.xls", , , , , :

sapply(sheetNames(xls), read.xls, xls = xls, simplify = FALSE)

, . simplify = FALSE.

+8

XLConnect. , .

#Read your workbook 
wb<-loadWorkbook("Your_workbook.xls")

#Save each sheet name as a vector
lp<-getSheets(wb)

#Now read each sheet as separate list element
dat<-lapply(seq_along(lp),function(i) readWorksheet(wb,sheet=lp[i]))

UPDATE

@Martin Studer, XLConnect , lapply(), getSheets() readWorksheet().

dat <- readWorksheet(wb, sheet = getSheets(wb))
+8

All Articles