如何在R中使用xlsx包为所有单元格提供边框?

时间:2018-02-14 05:14:48

标签: r

我是R编程的新手,所以我试图通过使用xlsx我的脚本为所有单元格添加边框

library(xlsx)
wb <- createWorkbook(type='xlsx')
sheet <- xlsx::createSheet(wb, sheetName = "addDataFrame1")
data <- data.frame(mon=month.abb[1:10], day=1:10, year=2000:2009,
               date=seq(as.Date("1999-01-01"), by="1 year", length.out=10),
               bool=c(TRUE, FALSE), log=log(1:10),
               rnorm=10000*rnorm(10),
               datetime=seq(as.POSIXct("2011-11-06 00:00:00", tz="GMT"), by="1 hour",
                            length.out=10))
   cs1 <- CellStyle(wb) + Font(wb, isItalic=TRUE)+Fill(foregroundColor = "lightblue",backgroundColor = "lightblue", pattern = "SOLID_FOREGROUND")
  cs2<-Border(color="black", position=c("BOTTOM", "LEFT","TOP","RIGHT"), pen=c("BORDER_THIN", "BORDER_THIN","BORDER_THIN","BORDER_THIN"))
addDataFrame(data,sheet,col.names = TRUE,row.names = FALSE,startRow = 2,startColumn = 1,colnamesStyle = cs1,
         rownamesStyle = cs2)

  xlsx::saveWorkbook(wb, file="E:/ff.xlsx")

1 个答案:

答案 0 :(得分:2)

对于期望的结果,

  1. cs2已修改,2。已添加列表all_Colstyle以传递至colStyle函数中的addDataFrame1参数
  2. 在下面这个结果中我想要大胆的最后一行怎么办?

    以下是修改后的代码:

    library(xlsx)
    wb <- createWorkbook(type='xlsx')
    sheet <- xlsx::createSheet(wb, sheetName = "addDataFrame1")
    data <- data.frame(mon=month.abb[1:10], day=1:10, year=2000:2009,
                   date=seq(as.Date("1999-01-01"), by="1 year", length.out=10),
                   bool=c(TRUE, FALSE), log=log(1:10),
                   rnorm=10000*rnorm(10),
                   datetime=seq(as.POSIXct("2011-11-06 00:00:00", tz="GMT"), by="1 hour",
                                length.out=10))
    cs1 <- CellStyle(wb) + Font(wb, isItalic=TRUE)+Fill(foregroundColor = "lightblue",backgroundColor = "lightblue", pattern = "SOLID_FOREGROUND")
    cs2 <- CellStyle(wb) + Border(color="black", position=c("BOTTOM", "LEFT","TOP","RIGHT"), pen=c("BORDER_THIN", "BORDER_THIN","BORDER_THIN","BORDER_THIN"))
    
    all_Colstyle<- rep(list(cs2), dim(data)[2]) 
    names(all_Colstyle) <- seq(1, dim(data)[2], by = 1)
    
    addDataFrame(data, sheet,col.names = TRUE, row.names = FALSE, startRow = 2,startColumn = 1,colnamesStyle = cs1, rownamesStyle = cs2, colStyle=cs2)
    
    saveWorkbook(wb, file="E:/ff.xlsx")
    

    <强>结果:

    snip1