rguides

How to Write Data Frames to Excel Files in R

Write data frames to Excel files in R with writexl for simple exports or openxlsx for multi-sheet workbooks with cell styling. writexl has no Java dependency and a single function call, while openxlsx gives you formatting control and formula support. Both handle dates automatically, converting R Date and POSIXct columns to Excel date format.

library(writexl)
write_xlsx(df, "output.xlsx")

# Multiple sheets with a named list
write_xlsx(list(Students = df, Scores = df * 1.1), "combined.xlsx")

For polished output with multiple sheets and formatting, use openxlsx:

library(openxlsx)
wb <- createWorkbook()
addWorksheet(wb, "Sheet1")
writeData(wb, "Sheet1", df)
addWorksheet(wb, "Sheet2")
writeData(wb, "Sheet2", df * 1.1)
saveWorkbook(wb, "multi_sheet.xlsx", overwrite = TRUE)

The older xlsx package requires Java and rJava, making it harder to install. Prefer writexl for straightforward exports and openxlsx when you need formatting control or multiple sheets. With openxlsx, you can add filters, freeze panes, and set column widths: setColWidths(wb, "Sheet1", cols = 1:3, widths = "auto") after writing the data. To style header rows, use addStyle() with createStyle(textDecoration = "bold"). Both packages write the active data frame without modifying the original object.

See also