Write an object to a worksheet

Share:

Description

Write an object to worksheet with optional styling.

Usage

1
2
3
4
5
6
writeData(wb, sheet, x, startCol = 1, startRow = 1, xy = NULL,
  colNames = TRUE, rowNames = FALSE, headerStyle = NULL,
  borders = c("none", "surrounding", "rows", "columns", "all"),
  borderColour = getOption("openxlsx.borderColour", "black"),
  borderStyle = getOption("openxlsx.borderStyle", "thin"),
  withFilter = FALSE, keepNA = FALSE)

Arguments

wb

A Workbook object containing a worksheet.

sheet

The worksheet to write to. Can be the worksheet index or name.

x

Object to be written. For classes supported look at the examples.

startCol

A vector specifiying the starting column to write to.

startRow

A vector specifiying the starting row to write to.

xy

An alternative to specifying startCol and startRow individually. A vector of the form c(startCol, startRow).

colNames

If TRUE, column names of x are written.

rowNames

If TRUE, data.frame row names of x are written.

headerStyle

Custom style to apply to column names.

borders

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.

borderColour

Colour of cell border. A valid colour (belonging to colours() or a hex colour code, eg see here).

borderStyle

Border line style

  • none no border

  • thin thin border

  • medium medium border

  • dashed dashed border

  • dotted dotted border

  • thick thick border

  • double double line border

  • hair hairline border

  • mediumDashed medium weight dashed border

  • dashDot dash-dot border

  • mediumDashDot medium weight dash-dot border

  • dashDotDot dash-dot-dot border

  • mediumDashDotDot medium weight dash-dot-dot border

  • slantDashDot slanted dash-dot border

withFilter

If TRUE, add filters to the column name row. NOTE can only have one filter per worksheet.

keepNA

If TRUE, NA values are converted to #N/A in Excel else NA cells will be empty.

Details

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.

Author(s)

Alexander Walker

See Also

writeDataTable

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
45
46
47
48
49
50
51
52
53
54
55
56
## See formatting vignette for further examples.

wb <- createWorkbook()
options("openxlsx.borderStyle" = "thin")
options("openxlsx.borderColour" = "#4F81BD")
## Add worksheets
addWorksheet(wb, "Cars")


x <- mtcars[1:6,]
writeData(wb, "Cars", x, startCol = 2, startRow = 3, rowNames = TRUE)
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)

## Write a hyperlink vector
v <- rep("http://cran.r-project.org/", 4)
names(v) <- paste("Hyperlink", 1:4) # Names will be used as display text
class(v) <- 'hyperlink'
writeData(wb, "Cars", x = v, xy = c("B", 32))


addWorksheet(wb, "Formula")

## create column of class "formula"
df <- data.frame(x=1:3, y = 1:3,
                 z = paste(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
saveWorkbook(wb, "writeDataExample.xlsx", overwrite = TRUE)

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.