Learn R Programming

openxlsx (version 2.4.0)

read.xlsx: Read data from a worksheet or Workbook object into a data.frame

Description

Read data from a worksheet or Workbook object into a data.frame

Usage

read.xlsx(xlsxFile, sheet = 1, startRow = 1, colNames = TRUE,
  skipEmptyRows = TRUE, rowNames = FALSE, detectDates = FALSE,
  rows = NULL, cols = NULL)

Arguments

xlsxFile
An xlsx file or Workbook object
sheet
The name or index of the sheet to read data from.
startRow
first row to begin looking for data. Empty rows at the top of a file are always skipped, regardless of the value of startRow.
colNames
If TRUE, the first row of data will be used as column names.
skipEmptyRows
If TRUE, empty rows are skipped else empty rows after the first row containing data will return a row of NAs.
rowNames
If TRUE, first column of data will be used as row names.
detectDates
If TRUE, attempt to recognise dates and perform conversion.
rows
A numeric vector specifying which rows in the Excel file to read. If NULL, all rows are read.
cols
A numeric vector specifying which columns in the Excel file to read. If NULL, all columns are read.

Value

  • data.frame

Details

Creates a data.frame of all the data on a worksheet.

Examples

Run this code
xlsxFile <- system.file("readTest.xlsx", package = "openxlsx")
df1 <- read.xlsx(xlsxFile = xlsxFile, sheet = 1, startRow = 1, skipEmptyRows = FALSE)
sapply(df1, class)

df2 <- read.xlsx(xlsxFile = xlsxFile, sheet = 3, startRow = 1, skipEmptyRows = TRUE)
df2$Date <- convertToDate(df2$Date)
sapply(df2, class)
head(df2)

df2 <- read.xlsx(xlsxFile = xlsxFile, sheet = 3, startRow = 1, skipEmptyRows = TRUE,
                   detectDates = TRUE)
sapply(df2, class)
head(df2)

#wb <- loadWorkbook(system.file("readTest.xlsx", package = "openxlsx"))
#df3 <- read.xlsx(wb, sheet = 2, startRow = 1, skipEmptyRows = FALSE, colNames = TRUE)
#df4 <- read.xlsx(xlsxFile, sheet = 2, startRow = 1, skipEmptyRows = FALSE, colNames = TRUE)
#all.equal(df3, df4)

#wb <- loadWorkbook(system.file("readTest.xlsx", package = "openxlsx"))
#df3 <- read.xlsx(wb, sheet = 2, startRow = 1, skipEmptyRows = FALSE,
# cols = c(1, 4), rows = c(1, 3, 4))

Run the code above in your browser using DataLab