Write an object to worksheet with optional styling.
writeData(
wb,
sheet,
x,
startCol = 1,
startRow = 1,
array = FALSE,
xy = NULL,
colNames = TRUE,
rowNames = FALSE,
headerStyle = openxlsx_getOp("headerStyle"),
borders = openxlsx_getOp("borders", "none"),
borderColour = openxlsx_getOp("borderColour", "black"),
borderStyle = openxlsx_getOp("borderStyle", "thin"),
withFilter = openxlsx_getOp("withFilter", FALSE),
keepNA = openxlsx_getOp("keepNA", FALSE),
na.string = openxlsx_getOp("na.string"),
name = NULL,
sep = ", ",
col.names,
row.names
)
A Workbook object containing a worksheet.
The worksheet to write to. Can be the worksheet index or name.
Object to be written. For classes supported look at the examples.
A vector specifying the starting column to write to.
A vector specifying the starting row to write to.
A bool if the function written is of type array
An alternative to specifying startCol
and
startRow
individually. A vector of the form
c(startCol, startRow)
.
If TRUE
, column names of x are written.
If TRUE
, data.frame row names of x are written.
Custom style to apply to column names.
Either "none
" (default), "surrounding
",
"columns
", "rows
" or respective abbreviations. If
"surrounding
", a border is drawn around the data. If "rows
",
a surrounding border is drawn with 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.
Colour of cell border. A valid colour (belonging to colours()
or a hex colour code, eg see here).
Border line style
no border
thin border
medium border
dashed border
dotted border
thick border
double line border
hairline border
medium weight dashed border
dash-dot border
medium weight dash-dot border
dash-dot-dot border
medium weight dash-dot-dot border
slanted dash-dot border
If TRUE
or NA
, add filters to the column name row. NOTE can only have one filter per worksheet.
If TRUE
, NA values are converted to #N/A (or na.string
, if not NULL) in Excel, else NA cells will be empty.
If not NULL, and if keepNA
is TRUE
, NA values are converted to this string in Excel.
If not NULL, a named region is defined.
Only applies to list columns. The separator used to collapse list columns to a character vector e.g. sapply(x$list_column, paste, collapse = sep).
Deprecated, please use rowNames
, colNames
instead
invisible(0)
Formulae written using writeFormula to a Workbook object will not get picked up by read.xlsx(). This is because only the formula is written and left to Excel to evaluate the formula when the file is opened in Excel.
## See formatting vignette for further examples.
## Options for default styling (These are the defaults)
options("openxlsx.borderColour" = "black")
options("openxlsx.borderStyle" = "thin")
options("openxlsx.dateFormat" = "mm/dd/yyyy")
options("openxlsx.datetimeFormat" = "yyyy-mm-dd hh:mm:ss")
options("openxlsx.numFmt" = NULL)
## Change the default border colour to #4F81BD
options("openxlsx.borderColour" = "#4F81BD")
#####################################################################################
## Create Workbook object and add worksheets
wb <- createWorkbook()
## Add worksheets
addWorksheet(wb, "Cars")
addWorksheet(wb, "Formula")
x <- mtcars[1:6, ]
writeData(wb, "Cars", x, startCol = 2, startRow = 3, rowNames = TRUE)
#####################################################################################
## Bordering
writeData(wb, "Cars", x,
rowNames = TRUE, startCol = "O", startRow = 3,
borders = "surrounding", borderColour = "black"
) ## black border
writeData(wb, "Cars", x,
rowNames = TRUE,
startCol = 2, startRow = 12, borders = "columns"
)
writeData(wb, "Cars", x,
rowNames = TRUE,
startCol = "O", startRow = 12, borders = "rows"
)
#####################################################################################
## Header Styles
hs1 <- createStyle(
fgFill = "#DCE6F1", halign = "CENTER", textDecoration = "italic",
border = "Bottom"
)
writeData(wb, "Cars", x,
colNames = TRUE, rowNames = TRUE, startCol = "B",
startRow = 23, borders = "rows", headerStyle = hs1, borderStyle = "dashed"
)
hs2 <- createStyle(
fontColour = "#ffffff", fgFill = "#4F80BD",
halign = "center", valign = "center", textDecoration = "bold",
border = "TopBottomLeftRight"
)
writeData(wb, "Cars", x,
colNames = TRUE, rowNames = TRUE,
startCol = "O", startRow = 23, borders = "columns", headerStyle = hs2
)
#####################################################################################
## Hyperlinks
## - vectors/columns with class 'hyperlink' are written as hyperlinks'
v <- rep("https://CRAN.R-project.org/", 4)
names(v) <- paste0("Hyperlink", 1:4) # Optional: names will be used as display text
class(v) <- "hyperlink"
writeData(wb, "Cars", x = v, xy = c("B", 32))
#####################################################################################
## Formulas
## - vectors/columns with class 'formula' are written as formulas'
df <- data.frame(
x = 1:3, y = 1:3,
z = paste0(paste0("A", 1:3 + 1L), paste0("B", 1:3 + 1L), sep = " + "),
stringsAsFactors = FALSE
)
class(df$z) <- c(class(df$z), "formula")
writeData(wb, sheet = "Formula", x = df)
#####################################################################################
## Save workbook
## Open in excel without saving file: openXL(wb)
if (FALSE) { # \dontrun{
saveWorkbook(wb, "writeDataExample.xlsx", overwrite = TRUE)
} # }