The quickest way to save a table from R as an Excel file is to write it out with a package that understands the .xlsx format: writexl::write_xlsx(df, "table.xlsx") for a single sheet with no Java dependency, or openxlsx::write.xlsx(df, "table.xlsx") when you want styling, multiple sheets in one workbook, or control over the header row. A plain CSV (write.csv) is not an Excel file, and while Excel opens it fine, you lose data types, formatting and, on some systems, the correct delimiter. The three-step route is: install a package that writes .xlsx, confirm your data is in a data frame, then call the write function with a file path. Everything below explains how to do that reliably, including what to do when the file comes out wrong.
What You'll Need
- R installed and working, plus RStudio or another editor you are comfortable in. The steps below work the same from the console or a script.
- Your table loaded into R as a data frame (or a tibble — both work with the packages below). A matrix or a list needs converting first.
- The
writexloropenxlsxpackage. Both are on CRAN, soinstall.packages("writexl")from an official CRAN mirror is the only download involved. - A writeable destination folder. If you cannot write to your working directory, R will throw an error rather than silently failing.
- Java is not needed for either package above — that requirement belongs to older xlsx packages, and skipping it is one reason these two are recommended.
Step-by-Step
- Install the package once. Run
install.packages("writexl"). If you also want formatting and multi-sheet control, runinstall.packages("openxlsx"). You only need to install once per R installation, not once per session. - Load it in every new session. Add
library(writexl)(orlibrary(openxlsx)) at the top of your script. R does not remember loaded packages between sessions, and forgetting this is the most common reason a script that worked yesterday now fails. - Check that your table really is a data frame. Run
class(your_table)andstr(your_table). You want to see"data.frame"or"tbl_df"with columns of ordinary types (numeric, character, logical, Date). If you see"matrix", convert withas.data.frame(your_table). If you see"list"with nested objects, flatten it first — xlsx writers cannot store lists inside cells. - Decide where the file should go and give it a full path. Set the folder with
setwd("/path/to/folder"), or just include the whole path in the filename:write_xlsx(your_table, "/path/to/folder/table.xlsx"). On Windows, use forward slashes or doubled backslashes (C:/data/table.xlsx), because a single backslash is an escape character in R strings. - Write the file. For a single sheet:
write_xlsx(your_table, "table.xlsx"). For a workbook with a named sheet:write.xlsx(your_table, "table.xlsx", sheetName = "Results")using openxlsx. Both functions open a connection, write the file and close it — you do not need to close anything manually. - Write several tables to one workbook (optional). With openxlsx, pass a named list:
write.xlsx(list(Summary = summary_df, Detail = detail_df), "report.xlsx"). Each list name becomes a worksheet tab. With writexl you can pass the same named list towrite_xlsx(), which also creates one sheet per element. - Protect the header row if your data has odd column names. Both packages use your column names as the header by default. If those names are long, contain line breaks or are duplicated, set
colNames = TRUEand consider renaming the columns in R first withnames(your_table) <- c("ID", "Date", "Value"), which is simpler than fixing them in Excel afterwards. - Stop R overwriting an existing file by accident. These functions overwrite silently. If the target matters, write to a new filename each time, or check first with
file.exists("table.xlsx")before writing. - Verify the result. Run
file.exists("table.xlsx"). Then open the file in Excel or LibreOffice and confirm the row count matchesnrow(your_table)and the column count matchesncol(your_table). A quick check inside R is to read it back:readxl::read_excel("table.xlsx").
Menu labels in RStudio differ slightly by version and operating system, so if a menu path in your install does not match a tutorial you are following, use the console commands instead — they behave identically everywhere.
Common Mistakes
Writing a CSV and calling it an Excel file. write.csv(df, "table.xls") produces a comma-separated text file with an .xls extension. Excel will usually warn about the format mismatch or open it with everything in one column. If you need .xlsx, use a package that genuinely writes it.
Using a single backslash on Windows. "C:\data\table.xlsx" breaks because \d and \t are escape sequences. Use C:/data/table.xlsx or file.path("C:", "data", "table.xlsx").
Forgetting to load the package. The error could not find function "write_xlsx" almost always means the library() call is missing or was run in a different session.
Trying to write a list column or a nested table. Cells cannot hold data frames. Flatten or paste those columns into text, or split the nested parts into separate sheets.
Writing to a path you cannot write to. Saving into a system folder or a synced folder locked by another application produces a permission error. Write to your home or project folder first.
Assuming dates survive untouched. Excel stores dates as serial numbers with a format. A date column that looks right in R can appear as a five-digit number in Excel if the cell format is general — apply a date format in Excel, or write dates as text with format() if the file is for interchange rather than analysis.
Tips
Use writexl when you just need the data out and want the fewest dependencies; use openxlsx when the person receiving the file expects bold headers, column widths or multiple tabs. Mixing both in one script is fine.
Wrap the write call in a small function or save it in its own script so you are not retyping the path. Something like save_table <- function(df, name) openxlsx::write.xlsx(df, paste0(name, ".xlsx")) keeps filenames consistent across a project.
Keep the working directory explicit at the top of the script with setwd() or an RStudio project, so the file lands in the same place every run. Relative paths are resolved against the current working directory, which is not always what you expect.
If the data is large, write a summary sheet and a detail sheet separately rather than one enormous tab — Excel is slow to open very wide files, and readers usually only need the summary.
Before sending a file onward, reopen it once. Re-reading with readxl::read_excel() catches column type drift, such as numeric IDs that came back as text, far earlier than a recipient will.
Troubleshooting
"There is no package called 'writexl'." The install step did not complete or installed into a different library. Run install.packages("writexl") again and watch the console for errors, then library(writexl).
Permission denied or cannot open file. The folder is read-only, or the file is open in Excel. Close Excel, then write to a folder under your user directory. On Windows, an open workbook locks the file and prevents overwriting.
The file is created but empty or has one column. Check ncol() first. This usually means a matrix was passed, or a data frame was built with a single list column. Inspect with str() and rebuild the object.
Garbled characters or broken accents. Ensure your R session encoding matches the data. Write to CSV with a UTF-8 encoding argument only as a fallback; for xlsx, both packages handle Unicode, so garbling usually points to the source data being read in with the wrong encoding earlier in the pipeline.
Numbers arrive as text in Excel. Look for the green warning triangles in the cells. Convert the column in R with as.numeric() before writing, rather than coercing it in Excel.
Still stuck? Run sessionInfo() and check the package version, restart the R session cleanly, and try the smallest possible example — write_xlsx(data.frame(a = 1), "test.xlsx"). If that works, the problem is in your data, not the package.
Frequently Asked Questions
Can I save an R table as .xlsx without installing a package?
No. Base R writes CSV and tab-separated text but has no built-in .xlsx writer. You need a package such as writexl or openxlsx.
What is the difference between write.csv and write_xlsx?
write.csv produces plain text with no formatting or sheet structure. write_xlsx produces a real Excel workbook with typed cells and multiple sheets.
How do I put several tables in one Excel file?
Pass a named list to write_xlsx() or write.xlsx(). Each element becomes its own worksheet, named after the list element.
Why does my Excel file have a date column showing numbers?
Excel stores dates as serial numbers. The value is correct but the cell format is not set to a date. Format the column as a date in Excel, or write the dates as formatted text.
Does saving to xlsx change my data in R?
No. The write functions read your object and create a new file. Your data frame in the session is untouched unless you assign over it.
Can I overwrite an existing Excel file from R?
Yes, both packages overwrite without prompting. Check for the file with file.exists() first if you want to guard against accidental loss.