38

I have loaded a workbook into R and read in the worksheets using xlConnect, but I was wondering if there was a way of extracting the names of the sheets perhaps in a vector?

So far my code is:

dataIn<-loadWorkbook(file.path(filenames[1],sep=""))
lst = readWorksheet(dataIn, sheet = getSheets(dataIn), startRow=1, startCol=1, header=TRUE)

...and I want to extract the sheet names of the sheets in lst.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
userk
  • 901
  • 5
  • 11
  • 19

4 Answers4

73

Another really nice package developed by the folks at RStudio is readxl. It's easy to get the excel sheet names with the excel_sheets() function.

library(readxl)
path <- "path/to/your/file.xlsx"
excel_sheets(path = path)
Matt Dancho
  • 6,840
  • 3
  • 35
  • 26
27

You are looking for getSheets

Returns all worksheet names in a workbook.
agstudy
  • 119,832
  • 17
  • 199
  • 261
6

In the "openxlsx" package it would be a command "getSheetNames":

library(openxlsx)
path <- "path/to/your/file.xlsx"
getSheetNames(path)
Luker
  • 141
  • 2
  • 4
0

Here is another approach that can be considered :

library(RDCOMClient)
xlApp <- COMCreate("Excel.Application")
xlApp[["DisplayAlerts"]] <- FALSE
xlApp[["Visible"]] <- TRUE

path_To_Excel_File <- "D:/excel_File.xlsx"
xlWbk <- xlApp$Workbooks()$Open(path_To_Excel_File)
nb_Sheets <- xlWbk$Sheets()$Count()
sheets_Names <- character(nb_Sheets)

for(i in 1 : nb_Sheets)
{
  sheets_Names[i] <- xlWbk$Sheets(i)$Name()
}
Emmanuel Hamel
  • 1,769
  • 7
  • 19