在R中创建不同的XLSX表标头颜色

发布于 2025-01-25 07:43:09 字数 1328 浏览 1 评论 0原文

我想为我创建的XLSX表具有不同的标题颜色。但是,我只能使用以下代码使用XLSX软件包为标头创建单个颜色。有没有办法使用此软件包进行多种颜色?谢谢你!

## create workbook with styles
wb <- createWorkbook(type="xlsx")
TABLE_COLNAMES_STYLE <- CellStyle(wb) + Font(wb, isBold = TRUE) + Fill(foregroundColor="light blue") + Alignment(wrapText=T, horizontal = "ALIGN_CENTER")
TABLE_STYLE <- CellStyle(wb) + Alignment(wrapText=T, h="ALIGN_RIGHT", v = "VERTICAL_TOP") 

## load worksheet 1
sheet <- xlsx::createSheet(wb, sheetName="Sheet")

### load data to worksheet with styles
cell.format <- rep(list(TABLE_STYLE), (dim(iris)[2]))
xlsx::addDataFrame(data.frame(iris), sheet, row.names=FALSE, colStyle=cell.format, colnamesStyle = TABLE_COLNAMES_STYLE)
setColumnWidth(sheet, colIndex = c(1), colWidth = 15)
setColumnWidth(sheet, colIndex = c(2), colWidth = 50)
setColumnWidth(sheet, colIndex = c(3:(ncol(iris)-1)), colWidth = 15)
xlsx::createFreezePane(sheet, 2,2,2,3)

这就是我从上面的代码中得到的:

这是我想用多种标头颜色实现的目标,以便于列的差异化:

I would like to have different header colors for the xlsx sheet I'm creating. However, I'm only able to create a single color for the header with the below code using xlsx package. Is there a way to do multiple colors with this package? Thank you!

## create workbook with styles
wb <- createWorkbook(type="xlsx")
TABLE_COLNAMES_STYLE <- CellStyle(wb) + Font(wb, isBold = TRUE) + Fill(foregroundColor="light blue") + Alignment(wrapText=T, horizontal = "ALIGN_CENTER")
TABLE_STYLE <- CellStyle(wb) + Alignment(wrapText=T, h="ALIGN_RIGHT", v = "VERTICAL_TOP") 

## load worksheet 1
sheet <- xlsx::createSheet(wb, sheetName="Sheet")

### load data to worksheet with styles
cell.format <- rep(list(TABLE_STYLE), (dim(iris)[2]))
xlsx::addDataFrame(data.frame(iris), sheet, row.names=FALSE, colStyle=cell.format, colnamesStyle = TABLE_COLNAMES_STYLE)
setColumnWidth(sheet, colIndex = c(1), colWidth = 15)
setColumnWidth(sheet, colIndex = c(2), colWidth = 50)
setColumnWidth(sheet, colIndex = c(3:(ncol(iris)-1)), colWidth = 15)
xlsx::createFreezePane(sheet, 2,2,2,3)

This is what I'm getting from the above code:
enter image description here

This is what I'd like to achieve with multiple header colors for easier differentiation of the columns:
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

朦胧时间 2025-02-01 07:43:09

我不知道 xlsx 包。以下是如何使用 OpenXLSX

library(openxlsx)

## Create a new workbook
wb <- createWorkbook("John Doe")

## Add a worksheets
addWorksheet(wb, "Iris")

## write data to worksheet 1
writeData(wb, sheet = 1, iris)

## create and add a style to the column headers
headerStyle1 <- createStyle(
  fontSize = 14, fontColour = "#FFFFFF",
  fgFill = "#4F81BD", halign = "center"
)
addStyle(wb, sheet = 1, headerStyle1, rows = 1, cols = 1:4, gridExpand = TRUE)
headerStyle2 <- createStyle(
  fontSize = 14, fontColour = "yellow",
  fgFill = "purple", halign = "center"
)
addStyle(wb, sheet = 1, headerStyle2, rows = 1, cols = 5)

## save
saveWorkbook(wb, "Iris.xlsx", overwrite = TRUE)

I don't know the xlsx package. Below is how to do with openxlsx.

library(openxlsx)

## Create a new workbook
wb <- createWorkbook("John Doe")

## Add a worksheets
addWorksheet(wb, "Iris")

## write data to worksheet 1
writeData(wb, sheet = 1, iris)

## create and add a style to the column headers
headerStyle1 <- createStyle(
  fontSize = 14, fontColour = "#FFFFFF",
  fgFill = "#4F81BD", halign = "center"
)
addStyle(wb, sheet = 1, headerStyle1, rows = 1, cols = 1:4, gridExpand = TRUE)
headerStyle2 <- createStyle(
  fontSize = 14, fontColour = "yellow",
  fgFill = "purple", halign = "center"
)
addStyle(wb, sheet = 1, headerStyle2, rows = 1, cols = 5)

## save
saveWorkbook(wb, "Iris.xlsx", overwrite = TRUE)

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文