我正在编写一个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)
答案 0 :(得分:3)
我能够使用 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")
)
答案 1 :(得分:1)
它似乎与白色文本有关。尝试使用不同的颜色:
headerStyle <- CellStyle(wb, font = Font(wb, isBold=TRUE, color = "grey"), fill = Fill(foregroundColor = "#2db6e8", pattern = "SOLID_FOREGROUND"), alignment = Alignment(wrapText = TRUE, horizontal = "ALIGN_CENTER", vertical = "VERTICAL_CENTER")
)
它适用于橙色,灰色,蓝色,但不是白色。如果背景是默认的白色,这可能是为了防止文本不可见,但我不能肯定地说。也许包装创建者可以发表评论。
答案 2 :(得分:1)
xlsx包使用的常量在这些组中定义:
HALIGN_STYLES_
VALIGN_STYLES_
BORDER_STYLES_
FILL_STYLES_
CELL_STYLES_
INDEXED_COLORS_
所以,只需在控制台中显示INDEX_COLORS_
即可
BLACK WHITE RED BRIGHT_GREEN BLUE YELLOW
8 9 10 11 12 13
PINK TURQUOISE DARK_RED GREEN DARK_BLUE DARK_YELLOW
14 15 16 17 18 19
VIOLET TEAL GREY_25_PERCENT GREY_50_PERCENT CORNFLOWER_BLUE MAROON
20 21 22 23 24 25
LEMON_CHIFFON ORCHID CORAL ROYAL_BLUE LIGHT_CORNFLOWER_BLUE SKY_BLUE
26 28 29 30 31 40
LIGHT_TURQUOISE LIGHT_GREEN LIGHT_YELLOW PALE_BLUE ROSE LAVENDER
41 42 43 44 45 46
TAN LIGHT_BLUE AQUA LIME GOLD LIGHT_ORANGE
47 48 49 50 51 52
ORANGE BLUE_GREY GREY_40_PERCENT DARK_TEAL SEA_GREEN DARK_GREEN
53 54 55 56 57 58
OLIVE_GREEN BROWN PLUM INDIGO GREY_80_PERCENT AUTOMATIC
59 60 61 62 63 64
您可以使用数字或别名样式:
cs2 <- CellStyle(wb) +
Font(
wb,
heightInPoints = 12,
isBold = F,
isItalic = F,
name = "Arial",
color="ORANGE"
)
cs2 <- CellStyle(wb) +
Font(
wb,
heightInPoints = 12,
isBold = F,
isItalic = F,
name = "Arial",
color=59
)