[Write dataframe to Excel file] Write a simple xlsx spreadsheet of data #r #excel
#-- wrapper function to create a worksheet with particular formatting
writeXlsx <- function(d, fn, sheet_name = ''){
require(openxlsx)
# creates xlsx workbook containing data
# header styling
hs1 <- createStyle(fgFill = "#4F81BD", halign = "LEFT", textDecoration = "Bold", border = "Bottom", fontColour = "white")
cs1 <- createStyle(wrapText = TRUE, halign = 'LEFT', valign = 'top')
wb <- createWorkbook()
addWorksheet(wb, sheetName=sheet_name)
setColWidths(wb, 1, 1:ncol(d), 'auto')
freezePane(wb, 1, firstRow = TRUE)
writeData(wb, 1, d, headerStyle = hs1)
addStyle(wb, 1, style = cs1, rows = 2:(nrow(d) + 1), cols = 1:ncol(d), gridExpand = TRUE)
saveWorkbook(wb, fn, overwrite = TRUE)
}