Thursday, April 04, 2019

To get excel or workbook file sheet names using R xlsx package

Load your workbook or excel file, in my case e.g. name of 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 case in my example, I have not named first sheet and kept the default but 2nd sheet, I named as "name city" and hence 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 names of sheetnames as below
> names(getSheets(wb))
[1] "Sheet1"    "name city"
to get the name of specific index of sheet, e.g. passing [2] in my case for 2nd sheet
> names(getSheets(wb))[2]
[1] "name city"
*** Assumption for above is xlsx package is installed and loaded in R

0 Comments:

Post a Comment

<< Home