List of xlsx names with R

Can I create a list of file names in an xlsx file? Or maybe I can check if the sheet name exists, and if not, go to a specific function?

+10
r xlsx
source share
4 answers

Using the xlsx library, you can get a list of sheets in an existing workbook using getSheets ():

wb <- loadWorkbook(your_xlsx_file) sheets <- getSheets(wb) 
+12
source share

Yes, I did this with the xlsx package, which (like XLConnect ) uses a Java backend with Apache POI code, so it is cross-platform.

+7
source share

You can also do this with the RODBC package:

 h <- odbcConnectExcel2007("file.xlsx") sqlTables(h) 
+2
source share

To get Excel or Workbook file sheet names using R xlsx

Download your workbook or Excel file, in my case, for example, the name of the Excel file is "input_4_r.xlsx"

 > wb<-loadWorkbook("input_4_r.xlsx") 

see the list of files, here it shows 2 sheets in my example in my example, I did not name the first sheet and kept the default value, but the 2nd sheet, I called "name city" and therefore the output below

 > getSheets(wb) $Sheet1 [1] "Java-Object{Name: /xl/worksheets/sheet1.xml - Content Type: application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml}" $'name city' [1] "Java-Object{Name: /xl/worksheets/sheet2.xml - Content Type: application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml}" 

You can see the sheet name names as below

 > names(getSheets(wb)) [1] "Sheet1" "name city" 

to get the name of a specific index of the sheet, for example, passing [2] in my case for the 2nd sheet

 > names(getSheets(wb))[2] [1] "name city" 

*** It is assumed that the xlsx package is installed and downloaded to R.

0
source share

All Articles