A slight improvement in the accepted answer: writing a read-only file and changing it is not very elegant yet. Moreover, I had the feeling that overwriting xls files with saveWorkbook
could lead to “damaged” files (ie Excel would have to restore the file when it was opened).
To avoid this, you can proceed as follows:
df <- data.frame(matrix(rnorm(100), nc=10)) library(xlsx) wb <- createWorkbook(type = "xlsx") sheet <- createSheet(wb, sheetName = "rnormdata") addDataFrame(df, sheet, row.names = FALSE) setColumnWidth(sheet, colIndex = 1:3, colWidth = 20) autoSizeColumn(sheet, colIndex = 4:ncol(df)) saveWorkbook(wb, "Final.xlsx")
source share