openxlsx (version 2.0.1)

write.xlsx: write directly to an xlsx file

Description

write directly to an xlsx file

Usage

write.xlsx(x, file, ...)

Arguments

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

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 columns(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)}

See Also

addWorksheet writeData createStyle for style parameters

Examples

Run this code
## write to working directory
write.xlsx(iris, file = "writeXLSX1.xlsx", colNames = TRUE, borders = "rows")
write.xlsx(iris, file = "writeXLSX2.xlsx", colNames = TRUE, borders = "columns",
 borderStyle = "dashed")

options("openxlsx.borderColour" = "#4F80BD") ## set default border colour
write.xlsx(iris, file = "writeXLSX3.xlsx", colNames = TRUE, borders = "rows",
 sheetName = "Iris data", gridLines = FALSE)

options("openxlsx.borderStyle" = "dashDot")
write.xlsx(iris, file = "writeXLSX4.xlsx", colNames = TRUE, borders = "rows",
 gridLines = FALSE)

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

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

## Lists elements are written to individual worksheets
l <- list(iris, mtcars, matrix(runif(1000), ncol = 5))
write.xlsx(l, "writeList.xlsx")
names(l) <- c("IRIS", "MTCARS", "RUNIF")
write.xlsx(l, "writeList2.xlsx")

Run the code above in your browser using DataLab