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 toopenxlsx, 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 towritexl, 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 namedwrite.xlsxand depends on Java. If you follow an older tutorial and then loadopenxlsxas well, the two functions share a name, so writeopenxlsx::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
- Load the package:
library(openxlsx) - Write the data frame:
write.xlsx(iris, "iris.xlsx") - Open
iris.xlsxin 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
- Load the package:
library(writexl) - 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)
- openxlsx reference, write.xlsx: https://ycphs.github.io/openxlsx/reference/write.xlsx.html (checked 2026-10-29)
- openxlsx manual on CRAN: https://cran.r-project.org/web/packages/openxlsx/refman/openxlsx.html (checked 2026-10-29)
- writexl documentation (rOpenSci): https://docs.ropensci.org/writexl/ (checked 2026-10-29)
- xlsx package on CRAN, listing its Java requirement: https://cran.r-project.org/web/packages/xlsx/index.html (checked 2026-10-29)







