在R中创建不同的XLSX表标头颜色
我想为我创建的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:
This is what I'd like to achieve with multiple header colors for easier differentiation of the columns:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道 xlsx 包。以下是如何使用 OpenXLSX 。
I don't know the xlsx package. Below is how to do with openxlsx.