To read an Excel file in R, use the readxl package: install it once with install.packages("readxl"), load it with library(readxl), then call read_excel("path/to/file.xlsx"). That single function returns a data frame you can work with immediately, and it handles both .xls and .xlsx files without needing Java or Excel installed.
What You'll Need
- R installed and working (any recent version from CRAN).
- RStudio or another IDE is helpful but not required.
- The
readxlpackage, which you install from CRAN. - The Excel file saved somewhere you can point to, ideally in your working directory.
- Internet access for the one-time package installation.
Step-by-Step
- Install the readxl package. In the R console, run
install.packages("readxl"). You only need to do this once per R installation. If you are prompted to choose a CRAN mirror, pick any one near you. - Load the package. Run
library(readxl)at the start of every session where you need it. R does not remember loaded packages between sessions. - Set your working directory (optional but recommended). Use
setwd("C:/Users/YourName/Documents/data")on Windows orsetwd("/Users/YourName/Documents/data")on macOS. On Windows, use forward slashes or double backslashes in paths. You can check the current directory withgetwd(). - Read the file. Call
my_data <- read_excel("sales.xlsx"). If the file is not in your working directory, give the full path, for exampleread_excel("C:/data/sales.xlsx"). The result is a tibble (a modern data frame) assigned tomy_data. - Inspect the result. Run
head(my_data)to see the first rows,str(my_data)to check column types, anddim(my_data)for row and column counts. Confirm the numbers and dates look right before analysis. - Read a specific sheet. If the workbook has multiple sheets, list them with
excel_sheets("sales.xlsx"), then read one by name or position:read_excel("sales.xlsx", sheet = "Q1")orread_excel("sales.xlsx", sheet = 2). By default, the first sheet is read. - Skip rows or limit columns if needed. Use
skip = nto ignore the first n rows (useful when a report has title rows above the header) andrange = "A1:D50"to read only a specific block. Column types can be forced withcol_types, for examplecol_types = c("text", "numeric", "date"). - Save your script. Put the
library()call andread_excel()line in an R script so the import is reproducible next time.
Common Mistakes
- Forgetting
library(readxl). Installing a package does not load it; you get "could not find function read_excel" until you load it. - Wrong path separators on Windows. Backslashes in R strings are escape characters. Use forward slashes or double backslashes.
- Assuming the file is in the working directory. Check with
getwd()or use an absolute path. - Ignoring the sheet argument. Reading a multi-sheet workbook without specifying a sheet silently gives you the first one, which may not be the data you want.
- Expecting dates to import perfectly. Excel stores dates as numbers with formatting; readxl usually converts them, but odd formats can arrive as text or numbers. Check with
str(). - Using
read.csv()on an .xlsx file. That will fail or produce garbage; use a package designed for Excel files.
Tips
- Keep Excel files clean: one header row, no merged cells, no blank rows at the top. This prevents most import problems.
- Use
read_excel()rather than the olderread.xlsx()from theopenxlsxpackage unless you specifically need writing features; readxl has no external dependencies. - For very large workbooks, read only the columns and range you need with
rangeandcol_typesto save memory. - Store the file path in a variable at the top of your script so it is easy to change.
- If you work with many files, consider
list.files(pattern = "\\.xlsx$")and loop over them.
Troubleshooting
- "Error: `path` does not exist" — the file is not where R is looking. Run
file.exists("yourfile.xlsx"); if FALSE, fix the path or move the file. - "could not find function" — the package is not loaded. Run
library(readxl). - "package 'readxl' is not available" — check your internet connection and that you are using a recent R version; try a different CRAN mirror with
chooseCRANmirror(). - Permission denied — the file may be open in Excel or in a protected folder. Close Excel and copy the file to your Documents folder.
- Columns import as text when they should be numbers — the column likely contains stray characters, spaces, or a text-formatted cell. Clean the source file or set
col_typesexplicitly. - Only part of the data appears — check for blank rows or a
rangeargument that is too narrow.
Frequently Asked Questions
Can I read .xls files as well as .xlsx?
Yes. read_excel() handles both legacy .xls and modern .xlsx files.
Do I need Excel installed on my computer?
No. readxl reads the file format directly and does not require Microsoft Excel or Java.
How do I read all sheets at once?
Use excel_sheets() to get the names, then lapply() over them with read_excel(). For example: sheets <- excel_sheets("file.xlsx"); data <- lapply(sheets, function(s) read_excel("file.xlsx", sheet = s)).
Why are my dates showing as numbers?
Excel stores dates as serial numbers. readxl usually detects and converts them, but if not, wrap the column with as.Date() or specify the column type. Check the source cell formatting first.
Can I read a password-protected Excel file?
readxl cannot open encrypted workbooks. Remove the password in Excel first, or use a package that supports decryption if one is available for your setup.
What is the difference between read_excel and read_xlsx?
read_excel() guesses the format from the file extension and works for both .xls and .xlsx. read_xlsx() is specific to .xlsx files. For most work, read_excel() is the simpler choice.