0

Is there a way to export a data frame to an existing excel sheet? For instance I am trying to export a data frame called aus to an existing sheetname called "AUS + USA".

My approach:

write.xlsx2(aus, "Mission Performance Graphs.xlsx", sheetName="AUS + USA", 
            col.names=TRUE, row.names=FALSE, append=TRUE)

Thank you!

mischva11
  • 2,535
  • 3
  • 15
  • 31
  • 1
    Possible duplicate of [Export data to Excel](https://stackoverflow.com/questions/19414605/export-data-to-excel) – mischva11 Aug 26 '18 at 10:03
  • What is the problem with your approach? – John Coleman Aug 26 '18 at 10:58
  • My Excel sheet already exists. I want to replace the sheet with the updated data. I don't want to create a new excel sheet, but rather use the same one and just replace it. – Eliel Epelbaum Aug 26 '18 at 11:24
  • Why not just use `removeSheet()` to first remove the sheet and then `write.xlsx2` to write the new sheet? – John Coleman Aug 26 '18 at 12:29
  • 1
    I tried. I get the following error: Error in envRefInferField(x, what, getClass(class(x)), selfEnv) : ‘getSheetIndex’ is not a valid field or method name for reference class “Workbook” – Eliel Epelbaum Aug 26 '18 at 13:30

1 Answers1

0

try this kind of code

# R format to Excel sheet
library("xlsx")
write.xlsx(x=aus[[1]], "Mission Performance Graphs.xlsx", row.names = FALSE, sheetName = "AUS")
writte.xlsx(x=aus[[2]], "Mission Performance Graphs.xlsx", row.names = FALSE, sheetName = "USA", append = TRUE)

#Use getwd() to see where the file is located
getwd()