Jay Mehta
Jay Mehta

Reputation: 1

writeWorkSheet function in R not pasting values into Excel

I am trying to copy some data from an R data frame (Shipments) to an excel file using writeWorkBook function in the XLConnect package. However, it is not copying anything to the excel file. The execution doesn't result in any error/warning appearing in the console. It just doesn't copy.

I have loaded the library XLConnect and made sure I am not loading the library xlsx. The column to be copied has been type-casted to dataframe as I thought that might be an issue.

wbnames is an additional thing. I directly wrote the sheet name in the writeWorkBook and it should have worked fine. Even with wbnames there hasn't been any change in the result.

I originally intended to copy the content to a macro file and then run the macro file from R itself but it wasn't working. So I thought it may be because of macro file but the function is not working on .xlsx itself.

So, not sure what is the issue. Would be grateful if I can get some help here. Am I missing something?

library(XLConnect) 
library(RDCOMClient) 

xlApp   <- COMCreate("Excel.Application")
xlWbk   <- xlApp$Workbooks()$Open(FILEPATH+FILENAME.XLSX)
xlWb    <- loadWorkbook(FILEPATH+FILENAME.XLSX)
wbnames <- as.vector(getSheets(xlWb))

# Copy a column from the existing data frame and paste it to the first
# sheet of the FILENAME.XLSX, starting at Row#6, no headers and no rownames:
writeWorksheet(xlWb, as.data.frame(Shipments$SHIPMENT_ID),
    sheet = wbnames[1], startRow = 6, header = F, rownames = NULL)

Upvotes: 0

Views: 1161

Answers (1)

KenHBS
KenHBS

Reputation: 7174

xlWb is the R-object that contains the workbook. It looks like the data has been written to the workbook, which is good. In order to view in Excel format, however, you still need to save the workbook to Excel. Add this line after your code and you should see a document called your_file_name.xlsx with your data in your working directory:

XLConnect::saveWorkbook(xlWb, "your_file_name.xlsx")

Upvotes: 0

Related Questions