The package openxlsx can do some formatting. See the package documentation for all of the options.
library(openxlsx)
df <- data.frame(Label = LETTERS[1:10], Number = rnorm(10), Date = seq(42000, 42009))
wb <- createWorkbook()
addWorksheet(wb, sheetName = "Report")
writeData(wb = wb, sheet = "Report",x = df,
startCol = 1, startRow = 1, colNames = TRUE)
LabelStyle <- createStyle(halign = "center",
border = c("bottom", "right"),
borderStyle = "thin",
textDecoration = "bold",
fgFill = "#2020FF",
fontColour = "white")
NumStyle <- createStyle(halign = "right", numFmt = "0.00")
TextStyle <- createStyle(halign = "center",
border = "bottom",
borderStyle = "thin")
DateStyle <- createStyle(halign = "center", numFmt = "mm/dd/yyyy")
BkGrdStyle <- createStyle(fgFill = "#DDDDDD")
addStyle(wb, sheet = "Report", style = LabelStyle, rows = 1, cols = 1:3,
gridExpand = FALSE, stack = FALSE)
addStyle(wb, sheet = "Report", style = NumStyle, rows = 2:11, cols = 2,
gridExpand = FALSE, stack = FALSE)
addStyle(wb, sheet = "Report", style = TextStyle, rows = 2:11, cols = 1,
gridExpand = FALSE, stack = FALSE)
addStyle(wb, sheet = "Report", style = DateStyle, rows = 2:11, cols = 3,
gridExpand = FALSE, stack = FALSE)
#Append style information to a multiple rows and columns without overwriting
#the current style
addStyle(wb, sheet = "Report", style = BkGrdStyle, rows = 2:11, cols = 1:3,
gridExpand = TRUE, stack = TRUE)
saveWorkbook(wb, "Example.xlsx", overwrite = TRUE)