To format excel through R

I am trying to format excel file using R, but format is not working.

I tried r2excel package but unable to install it. Is their any packages that can help to format excel, format in the sense is to Increase font , bold, Italic, color etc..,

Let me know for any packages or sample code to format.

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)

Thank you FJCC.. I will try this and let you know the result. Thank you very much for your help.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.