Importing Excel Files with readxl
Read .xls and .xlsx sheets with sheet selection and cell range options.
Why readxl for Excel Files?
Excel files (.xlsx and .xls) are ubiquitous in business settings. The readxl package reads them without needing Excel installed, with no Java dependencies. It returns a tibble and handles multiple sheets, named ranges, and cell type detection.
library(readxl)
# readxl can read:
# .xlsx - modern Excel format (XML-based)
# .xls - older Excel format (binary)
# .xlsm - Excel with macros (reads data, ignores macros)
# Core functions:
# read_excel() - auto-detects .xls vs .xlsx
# read_xlsx() - always reads as .xlsx
# read_xls() - always reads as .xls
# excel_sheets() - lists all sheet names
cat('readxl requires no Java, no Excel installation!')excel_sheets() — List All Sheets
excel_sheets('file.xlsx') returns a character vector of all sheet names. Use this before reading to inspect the workbook structure, then pass the sheet name to read_excel().
library(readxl)
# Using readxl's built-in example file
path <- readxl_example('datasets.xlsx')
# List all sheets in the workbook
sheets <- excel_sheets(path)
print(sheets)
cat('\nNumber of sheets:', length(sheets))All lessons in this course
- Reading CSV Files with read_csv()
- Parsing TSV and Fixed-Width Files
- Importing Excel Files with readxl
- Writing Data to Multiple Formats