write.xlsx: write data to an xlsx file

Description Usage Arguments Value Optional Parameters Author(s) See Also Examples

View source: R/writexlsx.R

Description

write a data.frame or list of data.frames to an xlsx file

Usage

1
write.xlsx(x, file, asTable = FALSE, overwrite = TRUE, ...)

Arguments

x

A data.frame or a (named) list of objects that can be handled by writeData() or writeDataTable() to write to file

file

A file path to save the xlsx file

asTable

If TRUE will use writeDataTable() rather than writeData() to write x to the file (default: FALSE)

overwrite

Overwrite existing file (Defaults to TRUE as with write.table)

...

Additional arguments passed to buildWorkbook(); see details

Value

A workbook object

Optional Parameters

createWorkbook Parameters

addWorksheet Parameters

writeData/writeDataTable Parameters

freezePane Parameters

colWidths Parameters

Author(s)

Alexander Walker, Jordan Mark Barbone

See Also

addWorksheet()

writeData()

createStyle() for style parameters

buildWorkbook()

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
## write to working directory
options("openxlsx.borderColour" = "#4F80BD") ## set default border colour
## Not run: 
write.xlsx(iris, file = "writeXLSX1.xlsx", colNames = TRUE, borders = "columns")
write.xlsx(iris, file = "writeXLSX2.xlsx", colNames = TRUE, borders = "surrounding")

## End(Not run)


hs <- createStyle(
  textDecoration = "BOLD", fontColour = "#FFFFFF", fontSize = 12,
  fontName = "Arial Narrow", fgFill = "#4F80BD"
)
## Not run: 
write.xlsx(iris,
  file = "writeXLSX3.xlsx",
  colNames = TRUE, borders = "rows", headerStyle = hs
)

## End(Not run)

## 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))
## Not run: 
write.xlsx(l, "writeList1.xlsx", colWidths = c(NA, "auto", "auto"))

## End(Not run)

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

## End(Not run)

# specify column widths for multiple sheets
## Not run: 
write.xlsx(l, "writeList2.xlsx", colWidths = 20)
write.xlsx(l, "writeList2.xlsx", colWidths = list(100, 200, 300))
write.xlsx(l, "writeList2.xlsx", colWidths = list(rep(10, 5), rep(8, 11), rep(5, 5)))

## End(Not run)

openxlsx documentation built on Dec. 14, 2021, 5:06 p.m.