2015-08-24 26 views
5

GÜNCELLEME: ÇÖZÜLDÜ - Kod şimdi XLSX kullanarak yeniyim ve benim tüm ikinci sıra mavi yapmaya çalışıyorumR paket XLSX: Biçimlendirme Tek Hücre

mavi benim tek hücreyi biçimlendirmek için güncellenir (dahil çıktı sütunumdaki ilk sütun). Ama ben onu mavi yapmak için o hücreye başvurmakta zorluk çekiyorum.

Güncelleme 3: "Hücreyi A2 mavi yapmak için #try" yorumunun altında, (3. güncelleme itibarıyla) hücreye başvuru yapabilirim; ancak, daha önce girdiğim verilerle ilgili bir sorun yaratan yeni bir hücre oluşturarak yalnızca referans verebilirim. Önce yeni hücreyi oluşturursam, verileri veri çerçevesinden eklediğimde bunun üzerine yazılır. Zaten oluşturulduğunda bir hücreye başvurmanın bir yolu var mı?

Benim Kod aşağıdaki gibidir:

library(xlsx) 
# create a new workbook for outputs 
wb<-createWorkbook(type="xlsx") 

# Define some cell styles 
TITLE_STYLE <- CellStyle(wb)+ Font(wb, heightInPoints=10, 
            isBold=TRUE, name="Arial") + Alignment(horizontal="ALIGN_CENTER") 

# Styles for the data table row/column names 
TABLE_ROWNAMES_STYLE <- CellStyle(wb) + Font(wb, heightInPoints=10, name="Arial") 
TABLE_COLNAMES_STYLE <- CellStyle(wb) + Font(wb, heightInPoints=10, isBold=TRUE, color ="9", name="Arial") + Fill(foregroundColor="#0069AA") + 
    Alignment(wrapText=TRUE, horizontal="ALIGN_CENTER") 
TABLE_STYLE <- CellStyle(wb) + Font(wb, heightInPoints=10, name="Arial") 


# Create a new sheet in the workbook 
#++++++++++++++++++++++++++++++++++++ 
sheet <- createSheet(wb, sheetName = "US State Facts") 

#++++++++++++++++++++++++ 
# Helper function to add titles 
#++++++++++++++++++++++++ 
# - sheet : sheet object to contain the title 
# - rowIndex : numeric value indicating the row to 
#contain the title 
# - title : the text to use as title 
# - titleStyle : style object to use for title 
xlsx.addTitle<-function(sheet, rowIndex, title, titleStyle){ 
    rows <-createRow(sheet,rowIndex=rowIndex) 
    sheetTitle <-createCell(rows, colIndex=1) 
    setCellValue(sheetTitle[[1,1]], title) 
    setCellStyle(sheetTitle[[1,1]], titleStyle) 
} 

# Add title 
xlsx.addTitle(sheet, rowIndex=1, title="US State Facts", 
       titleStyle = TITLE_STYLE) 


#Add a table into a worksheet 

cell.format <- rep(list(TABLE_STYLE), (dim(state.x77)[2])) # style for remaining columns 
names(cell.format) <- seq(1, dim(state.x77)[2], by = 1) # assign names to list elements 
addDataFrame(state.x77, sheet, startRow=2, startColumn=1, 
      colStyle = cell.format, 
      colnamesStyle = TABLE_COLNAMES_STYLE, 
      rownamesStyle = TABLE_ROWNAMES_STYLE 
) 

# Change column width to auto 
autoSizeColumn(sheet, colIndex=c(1:ncol(state.x77))) 

#try to make cell A2 blue (as it's not included in the col name style) 
#rows <- createRow(sheet,rowIndex=2) #update 3 
#extracell <- createCell(rows, colIndex=1) #update 3 
#setCellStyle(extracell[[1,1]], TABLE_COLNAMES_STYLE) #update 3 

#######SOLUTION####### 
rows <- getRows(sheet) 
cells <- getCells(rows) 
setCellStyle(cells[[2]], TABLE_COLNAMES_STYLE) 

#merge header 
addMergedRegion(sheet, 1, 1, 1, 9) 


# Save the workbook to a file... 
saveWorkbook(wb, "h:/r-xlsx-report-example.xlsx") 

GÜNCELLEME 2: Ben sadece rownames Yeni bir değişken hale ve sonra rownames göstermiyor bu sorunu var. Ama eğer birisi tek bir hücreye nasıl referans gösterileceğini açıklayabilirse, bu harika olurdu!

+1

Belki de somut sorunun ne olduğunu açıklığa kavuşturabilirsiniz. Kodu çağırdığınızda bir hata var mı? İlk sütunla ilgili bir şeyden bahsetmiş olduğunuzdan - POI'deki endeksler 0 tabanlıdır. Mesele bu olabilir miydi? – bdecaf

+1

Endeksler hakkında Nevermind. Paket bununla ilgilenir. ama 'cell.A2 <- createCell (rowIndex = 1, colIndex = 1) ', makinemde bir hata veriyor. – bdecaf

+0

Bu, bir hata oluşturur: – Kath05

cevap

2

Benim daha spesifik bir örnek olarak yukarıdaki kodu güncelleriz bu

rows <- getRows(sheet) 
cells <- getCells(rows) 

#Then apply a style to a cell by referencing it's number as a java? object: 
setCellStyle(cells[[37]], TABLE_LASTROW_STYLE) 

#You could apply it to a row like this: 
lapply(c(37:43), function(i) setCellStyle(cells[[i]], TABLE_LASTROW_STYLE)) 

#You could apply it more generically to your last row like this: 
lapply(c((dim(df)[1]*dim(df)[2] + 2):(dim(df)[1]*dim(df)[2] + 2 + dim(df)[2] -1)), function(i) setCellStyle(cells[[i]], TABLE_LASTROW_STYLE)) 

gibi tek bir hücre başvurabilir.

İlgili konular