您需要使用 xlsx 的低级 API。CellStyle
请参阅函数中的示例。
wb <- createWorkbook()
sheet <- createSheet(wb, "Sheet1")
rows <- createRow(sheet, rowIndex=1)
cell.1 <- createCell(rows, colIndex=1)[[1,1]]
setCellValue(cell.1, "Hello R!")
cellStyle1 <- CellStyle(wb) +
Fill(backgroundColor="orange", foregroundColor="orange",
pattern="SOLID_FOREGROUND")
setCellStyle(cell.1, cellStyle1)
# Then save the workbook
saveWorkbook(wb, "filename.xlsx")
另请参阅 addDataFrame 函数以添加数据帧,然后使用低级 API 操作单元方面:
wb <- createWorkbook()
sheet <- createSheet(wb, sheetName="your nice sheet name")
addDataFrame(cars, sheet, row.names=FALSE)
# add manipulations here
saveWorkbook(wb, "filename.xlsx")