Free Access Week - Data Engineering + BI
Data Engineering and BI courses are free this week!
Free Access Week - Jun 2-8

openxlsx (version 3.0.0)

write.xlsx: write directly to an xlsx file

Description

write directly to an xlsx file

Usage

write.xlsx(x, file, asTable = FALSE, ...)

Arguments

x
object or a list of objects that can be handled by writeData to write to file
file
xlsx file name
asTable
write using writeDataTable as opposed to writeData
...
optional parameters to pass to functions:
  • createWorkbook
  • addWorksheet
  • writeData
  • saveWorkbook

see details.

Value

A workbook object

Details

Optional parameters are:
  • creator A string specifying the workbook author
  • sheetName Name of the worksheet
  • gridLines A logical. If FALSE, the worksheet grid lines will be hidden.
  • startCol A vector specifiying the starting column(s) to write df
  • startRow A vector specifiying the starting row(s) to write df
  • xy An alternative to specifying startCol and startRow individually. A vector of the form c(startCol, startRow)
  • colNames or col.names If TRUE, column names of x are written.
  • rowNames or row.names If TRUE, row names of x are written.
  • headerStyle Custom style to apply to column names.
  • borders Either "surrounding", "columns" or "rows" or NULL. If "surrounding", a border is drawn around the data. If "rows", a surrounding border is drawn a border around each row. If "columns", a surrounding border is drawn with a border between each column. If "all" all cell borders are drawn.
  • borderColour Colour of cell border
  • borderStyle Border line style.
  • overwrite Overwrite existing file (Defaults to TRUE as with write.table)

columns of x with class Date or POSIXt are automatically styled as dates and datetimes respectively.

See Also

addWorksheet

writeData

createStyle for style parameters

Examples

Run this code
## write to working directory
options("openxlsx.borderColour" = "#4F80BD") ## set default border colour
write.xlsx(iris, file = "writeXLSX1.xlsx", colNames = TRUE, borders = "columns")
write.xlsx(iris, file = "writeXLSX2.xlsx", colNames = TRUE, borders = "surrounding")


hs <- createStyle(textDecoration = "BOLD", fontColour = "#FFFFFF", fontSize=12,
                  fontName="Arial Narrow", fgFill = "#4F80BD")

write.xlsx(iris, file = "writeXLSX3.xlsx", colNames = TRUE, borders = "rows", headerStyle = hs)

## Lists elements are written to individual worksheets, using list names as sheet names if available
l <- list("IRIS" = iris, "MTCATS" = mtcars, matrix(runif(1000), ncol = 5))
write.xlsx(l, "writeList1.xlsx")

## different sheets can be given different parameters
write.xlsx(l, "writeList2.xlsx", startCol = c(1,2,3), startRow = 2,
           asTable = c(TRUE, TRUE, FALSE), withFilter = c(TRUE, FALSE, FALSE))

Run the code above in your browser using DataLab