library(openxlsx)

my.wb <- createWorkbook() ## Create a new workbook
addWorksheet(wb=my.wb,    ## Add a worksheet
             sheetName = "Expenditure",
             gridLines = FALSE)

h.style <- createStyle( # create a style for column headers
  fontSize = 16, fontColour = "white", halign = "right",
  fgFill = "blue", border = "TopBottom", borderColour = "black"
)
tab.style <- createStyle( ## style for the body of the table
  border = "TopBottom", borderColour = "red")
tab.style50 <- createStyle( ## style for the column "1950"
  border = "TopBottom", borderColour = "red",
  fontColour = "darkred", textDecoration = "bold",
  numFmt = "0.00")

## write data to worksheet 1
writeData(wb=my.wb, sheet = 1, x = USPersonalExpenditure, 
          colNames = TRUE, rowNames = TRUE,
          startRow = 4, startCol = 3, 
          headerStyle = h.style)

addStyle(wb=my.wb, sheet = 1, style = tab.style,
         rows = 5:(4+nrow(USPersonalExpenditure)),
         cols = 3:(3+ncol(USPersonalExpenditure)), 
         gridExpand = TRUE)
addStyle(wb=my.wb, sheet = 1, style = tab.style50,
         rows = 5:(4+nrow(USPersonalExpenditure)),
         cols = 6, gridExpand = TRUE)

setColWidths(wb = my.wb, sheet = 1, ## set column width 
             cols = 3, widths = "auto") 
saveWorkbook(wb=my.wb, file = "addStyleExample2.xlsx", 
             overwrite = TRUE)
## End(Not run)
