Skip to main content
v2026.11,986 entries · CC-BY 4.0

Write XLSX in R: write.xlsx and write_xlsx Explained

Write an xlsx file in R with openxlsx write.xlsx or writexl write_xlsx: install, multi-sheet output, asTable, and fixes for mixed-up functions.

Written and maintained by CASRAI Editorial Board

Published · Last updated

On this page

To write an xlsx file in R, install the openxlsx package and call write.xlsx(x, file), or install writexl and call write_xlsx(x, path). Both take a data frame, both write a real Excel file without Excel installed, and both write several sheets from a named list. The names look almost identical (a dot versus an underscore) but they belong to different packages with different arguments, which is the source of most errors people hit with this task.

What it is used for in research

Researchers write xlsx files from R to hand results to collaborators who work in Excel: summary tables, cleaned analysis datasets, supplementary tables for a manuscript or a data appendix. When the recipient only needs plain rows and columns, a text format is often simpler; see R Write CSV: write.csv, write_csv and fwrite compared and CSV vs XLSX for research data for how to decide. Choose xlsx when you need several sheets in one file or a workbook your collaborators open directly in Excel.

write.xlsx vs write_xlsx: which function is which

write.xlsx (dot) and write_xlsx (underscore) are different functions in different packages.

  • openxlsx::write.xlsx() belongs to openxlsx, which the CRAN manual describes as simplifying “the process of writing and styling Excel xlsx files from R” and removing “the dependency on Java”.
  • writexl::write_xlsx() belongs to writexl, described in its documentation as a “portable, light-weight data frame to xlsx exporter based on libxlsxwriter”, needing no Java and no Excel installation.
  • An older package, xlsx, also exports a function named write.xlsx and depends on Java. If you follow an older tutorial and then load openxlsx as well, the two functions share a name, so write openxlsx::write.xlsx() to say which one you mean.

Install or access (official source only)

Both packages are distributed through CRAN, R’s package repository. Install one of them from the R console:

install.packages("openxlsx")
# or
install.packages("writexl")

The openxlsx reference is at ycphs.github.io/openxlsx and its manual is on CRAN. The writexl documentation is at docs.ropensci.org/writexl.

Step-by-step: write a data frame to an xlsx file

With write.xlsx from openxlsx

  1. Load the package: library(openxlsx)
  2. Write the data frame: write.xlsx(iris, "iris.xlsx")
  3. Open iris.xlsx in Excel or read it back into R to check it.

The signature in the reference is write.xlsx(x, file, asTable = FALSE, overwrite = TRUE, ...). x is a data frame or a (named) list of objects, and file is the path to save the xlsx file. overwrite defaults to TRUE, “as with write.table”, so running the line again replaces an existing file without a warning; set overwrite = FALSE if you do not want that.

With write_xlsx from writexl

  1. Load the package: library(writexl)
  2. Write the data frame: write_xlsx(iris, "iris.xlsx")

Called with only the data, write_xlsx(x) writes to a temporary file and returns its path.

Write several sheets from a named list

Both functions turn a named list of data frames into one workbook with one sheet per element.

writexl::write_xlsx(list(Flowers = iris, Cars = mtcars), "two-sheets.xlsx")
openxlsx::write.xlsx(list("IRIS" = iris, "MTCATS" = mtcars), "two-sheets-openxlsx.xlsx")

The names of the list become the sheet names.

Write a data frame as an Excel table (asTable)

In openxlsx, asTable = TRUE makes write.xlsx use writeDataTable() instead of writeData(), so the range is written as an Excel table rather than plain cells. The default is FALSE. The reference’s own multi-sheet example passes vectors to this and related arguments, one value per sheet, for example asTable = c(TRUE, TRUE, FALSE) for three list elements.

Output and how to read it

The result is an ordinary .xlsx workbook: one sheet for a data frame, one sheet per list element for a named list. Check it by opening it in Excel, or read it back into R and compare it with the original; How to load data into R covers reading Excel files. For openxlsx, the sheet name, column names and row names can be controlled through the arguments passed on to the underlying writing functions (the reference lists sheetName, colNames and rowNames); check ?write.xlsx in your installed version before relying on a default.

Troubleshooting

“could not find function” for write_xlsx or write.xlsx

Either the package is not installed or not loaded, or you typed the wrong separator. write_xlsx (underscore) needs writexl; write.xlsx (dot) needs openxlsx (or the Java-based xlsx). Use the package::function form to remove the ambiguity.

An argument is reported as unused

Arguments such as asTable belong to openxlsx’s write.xlsx; they are not part of writexl::write_xlsx. If you copied a call from a tutorial for the other package, check which package’s documentation it matches.

An existing file was replaced

openxlsx’s overwrite argument defaults to TRUE. Pass overwrite = FALSE or write to a new file name.

Large data frames use a lot of memory

writexl’s documentation describes a constant_memory = TRUE option. In the package author’s own benchmark on a 336,776 by 19 data frame (R 4.6.1, writexl 2.0.0, openxlsx2 1.28), it reduced peak memory from 894 MB to 208 MB. That figure is the author’s measurement on their setup; yours will differ.

Alternative packages for writing xlsx files in R

Function Package Java needed Several sheets Notes from the documentation
write.xlsx() openxlsx No Named list of data frames asTable option; overwrite defaults to TRUE
write_xlsx() writexl No Named list of data frames Based on libxlsxwriter; constant_memory option
write.xlsx() xlsx (older) Yes Check the package documentation Same function name as openxlsx; use package:: to avoid mix-ups

The writexl documentation also compares it with the openxlsx2 package in the benchmark above (12.3 seconds against 19.6 seconds for the same file, with output files of about 27.8 MB and 27.9 MB). Treat that as the package author’s measurement, not a general ranking.

FAQ

How do I write an xlsx file in R?

Install openxlsx and run write.xlsx(my_data, "my_data.xlsx"), or install writexl and run write_xlsx(my_data, "my_data.xlsx").

What is write.xlsx in R?

It is a function that saves a data frame, or a named list of data frames, as an .xlsx file. The common version is in the openxlsx package; the older xlsx package has one with the same name.

Do I need Java or Excel to write xlsx in R?

Not with openxlsx or writexl. The openxlsx CRAN manual describes removing the dependency on Java, and the writexl documentation says “No Java or Excel required”.

How do I write more than one sheet?

Pass a named list of data frames as the first argument. Each element becomes a sheet, named after the list element.

Should I save CSV or xlsx?

Use xlsx when you need multiple sheets or collaborators who work in Excel; otherwise see CSV vs XLSX for research data and what a TSV file is for plain-text options.

Sources (official docs)

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about Write XLSX in R: write.xlsx and write_xlsx Explained

2 free questions a day, no account.

An AI assistant for research: funder rules, compliance, methods and publishing. It links the sources behind its answers and labels answers that come from a web search.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays open to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →