1

I am new for R Programming so what would i tried to add border for all cells by using xlsx my script is below

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")
IRTFM
  • 258,963
  • 21
  • 364
  • 487

1 Answers1

4

For the desired result,

  1. cs2 is edited, 2. added list all_Colstyle to pass to colStyle argument in addDataFrame1 function

In This Below result I want bold last row how should i do?

following is the modified code :

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")

Result:

snip1

parth
  • 1,571
  • 15
  • 24