I need to output a data frame into an excel file. The problem is that I would like to merge-center the data only for the first column. This is an easy excel function but would like to know if this is possible directly from R using xlsx or anyother library
Any help will be appreciated.
Edit: I am new to posting questions on StackOverflow so I guess I do not have rights to upload pictures. I have added the two on my google+. The first picture is what the situation is now
https://lh5.googleusercontent.com/-ednAfy8SCb8/UdcM8RB6xgI/AAAAAAAAMPA/9FuO15_UP4M/w256-h194-no/1.jpg
The second image is how I would like it to be
https://lh4.googleusercontent.com/-rU8elOT4FN8/UdcM8UZDTLI/AAAAAAAAMPE/ImNJoe5uzwk/w256-h194-no/2.jpg
Right-click the merged cell B1:D1, select "paste special -> formulas" You should see the merged cell being 0. Type Col1 in the merged cell. You should now see all B2, C2, D2 to be Col1, i.e. now you can reference the merged cell as you expect it to be.
Select the cell with the formatting you want to copy. Select Home > Format Painter. Drag to select the cell or range you want to apply the formatting to. Release the mouse button and the formatting should now be applied.
This can be done in the xlsx package using:
addMergedRegion(sheet, startRow, endRow, startColumn, endColumn)
One possibility is to use library XLConnect
and function mergeCells()
- only for this function you have to provide reference in for of A2:B3
and so on.
library(XLConnect)
#Create file
wb <- loadWorkbook("file.xlsx", create = TRUE)
# Create a worksheet called 'cars'
createSheet(wb, name = "cars")
#write data cars to sheet
writeWorksheet(wb, cars, sheet = "cars")
# Merge the cells A2:A3 and A4:A5 on the worksheet created above
mergeCells(wb, sheet = "cars", reference = c("A2:A3","A4:A5"))
# Save workbook
saveWorkbook(wb)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With