Skip to content

Instantly share code, notes, and snippets.

@DATAUNIRIO
Created July 12, 2025 16:57
Show Gist options
  • Select an option

  • Save DATAUNIRIO/ff03ea24989e60706aef7ac792ed0ddc to your computer and use it in GitHub Desktop.

Select an option

Save DATAUNIRIO/ff03ea24989e60706aef7ac792ed0ddc to your computer and use it in GitHub Desktop.
snippet meu_save_no_excel_formatado
library(openxlsx)
#options("openxlsx.borderColour" = "#003747")
#options("openxlsx.borderStyle" = "thin")
#options("openxlsx.dateFormat" = "%d %b %Y")
estilo_cabecalho <- createStyle(fgFill="#003747",fontColour="#ffffff",halign="center", border = "TopBottomLeftRight",fontSize=16,textDecoration = "Bold")
wb <- createWorkbook()
modifyBaseFont(wb, fontSize = 13, fontName = "Arial Narrow")
#Add Sheets
addWorksheet(wb, sheetName = "Motor Trend Car Road Tests", gridLines = TRUE)
#, startCol = 2, startRow = 2, borders="rows", headerStyle = headSty)
setColWidths(wb,sheet = "Motor Trend Car Road Tests", cols = c(1, 4, 6, 7, 9), widths = c(16, 15, 12, 18, 33))
setRowHeights(wb, sheet = "Motor Trend Car Road Tests", rows = c(1), heights = c(30))
#setColWidths(wb, sheet = 1, cols = "A", widths = 18)
data("mtcars")
writeDataTable(wb, sheet = "Motor Trend Car Road Tests",
x = mtcars,
colNames = TRUE, rowNames = TRUE,
headerStyle = estilo_cabecalho)
#negStyle <- createStyle(fontColour = "#9C0006", bgFill = "#FFC7CE")
#posStyle <- createStyle(fontColour = "#006100", bgFill = "#C6EFCE")
novo_ptres <- createStyle(fontColour = "#9C0006", bgFill = "#FFC7CE")
igual_zero <- createStyle(fontColour = "#006100", bgFill = "#C6EFCE")
conditionalFormatting(wb, "Motor Trend Car Road Tests", cols=4, rows=2:nrow(mtcars)+1, rule="==360", style = novo_ptres)
conditionalFormatting(wb, "Motor Trend Car Road Tests", cols=9, rows=2:nrow(mtcars)+1, rule="==0", style = igual_zero)
saveWorkbook(wb, "stockPrice.xlsx", overwrite = TRUE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment