我正在写一个R函数来保存预格式化的数据帧。格式模板的一部分要求更改列标题字体的颜色。单元格样式返回正确的对齐方式和粗体,但文本仍为黑色。
下面是一个裸露的版本,用来演示我的字体着色问题(只需将file_path变量更改为存在的位置)。
library(xlsx)
file_path <- "C:/Users/.../Desktop/tst.xlsx"
wb <- createWorkbook()
headerStyle <- CellStyle(wb,
font = Font(wb, isBold=TRUE, color = "#ffffff"),
fill = Fill(foregroundColor = "#2db6e8",
pattern = "SOLID_FOREGROUND"),
alignment = Alignment(wrapText = TRUE,
horizontal = "ALIGN_CENTER",
vertical = "VERTICAL_CENTER")
)
x <- mtcars
sheet <- createSheet(wb, "test")
cellBlock <- CellBlock(sheet,
startRow = 1,
startCol = 1,
noRows = nrow(x) + 1,
noColumns = ncol(x) + 1,
create = TRUE)
CB.setRowData(cellBlock = cellBlock,
x = colnames(x),
rowIndex = 1,
colOffset = 1,
rowStyle = headerStyle +
Border(pen = "BORDER_MEDIUM", color = "black",
position = "BOTTOM"))
saveWorkbook(wb, file_path)
我能够使用INDEXED_COLORS_常量的颜色索引(白色为9)获得白色文本。对于您的示例代码,它将显示为:
headerStyle <- CellStyle(wb,
font = Font(wb, isBold=TRUE, color = "9"),
fill = Fill(foregroundColor = "#2db6e8",
pattern = "SOLID_FOREGROUND"),
alignment = Alignment(wrapText = TRUE,
horizontal = "ALIGN_CENTER",
vertical = "VERTICAL_CENTER")
)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句