30

I am using "openxlsx" package to read and write excel files. I have a fixed file with a sheet called "Data" which is used by formulas in other sheets. I want to update this Data sheet without touching the other. I am trying the following code:

write.xlsx(x = Rev_4, file = "Revenue.xlsx", sheetName="Data")

But this erases the excel file and creates a new one with just the new data in the "Data" sheet while all else gets deleted. Any Advice?

3
  • I cannot test this in the computer I am using at the moment but have you tried using the argument append = TRUE?
    – leosz
    Dec 9, 2015 at 7:59
  • 4
    "append=T" option seems to be absent with the "openxlsx" package. I know it is present in the java based "xlsx" package, but the package is slow and quickly runs out of memory with heavier files. I have a more tedious way around it, but it will be great if i could have an "openxlsx" based solution. Dec 12, 2015 at 16:38
  • openxlsx is a pretty poorly implemented package. YOu have to do all sorts of extra things to add a worksheet to an existing workbook if you want the same code to work for the first worksheet you are creating.
    – jzadra
    Sep 7, 2018 at 18:28

2 Answers 2

37

Try this:

wb <- loadWorkbook("Revenue.xlsx")
writeData(wb, sheet = "Data", Rev_4, colNames = F)
saveWorkbook(wb,"Revenue.xlsx",overwrite = T)

You need to load the complete workbook, then modify its data and then save it to disk. With writeData you can also specify the starting row and column. And you could also modify other sections before saving to disk.

10
  • 1
    This corrupts my file. Apr 30, 2018 at 21:54
  • @BrashEquilibrium just tried it with Excel 2013 and the latest version of openxlsx and it still works for me. May 1, 2018 at 3:37
  • 2
    This doesn't work if the workbook doesn't exist for the first sheet.
    – jzadra
    Sep 7, 2018 at 18:27
  • 3
    wb <- createWorkbook() addWorksheet(wb, sheetName = "test1") writeData(wb, sheet = "test1", x = data1) addWorksheet(wb, sheetName = "test2") writeData(wb, sheet = "test2", x = data2) saveWorkbook(wb, "test.xlsx") Sep 10, 2018 at 19:52
  • 1
    This corrupts my file too. Upon further investigation it seems that openxlsx will corrupt a workbook if if it contains Excel content it is not built to handle. The devs seems to be updating the package on an as-needed basis as people come to them with corrupted workbooks.
    – grapestory
    Aug 25, 2021 at 14:16
2

I've found this package. It depends on openxlsx and helps to insert many sheets on a xlsx file. Maybe it makes easier:

Package documentation

library(xlsx2dfs)
# However, be careful, the function xlsx2dfs assumes
# that all sheets contain simple tables. If that is not the case,
# use the accepted answer!
dfs <- xlsx2dfs("Revenue.xlsx") # all sheets of file as list of dfs
dfs["Data"] <- Rev_4   # replace df of sheet "Data" by updated df Rev_4
dfs2xlsx(dfs, "Revenue.xlsx") # this overwrites the existing file! cave!
3
  • When answering a question, it is best to write out the suggested code, using the example code/data provided in the original question. Welcome to SO!
    – Josh
    Mar 12, 2020 at 13:22
  • @Josh I added some code using the code/data of original question. Jun 3, 2020 at 10:20
  • 1
    @Gwang-JinKim, thanks. I made my comment because I was asked by the Review Queue to review Americo's answer (possibly his first?), but the details you added will be useful to others.
    – Josh
    Jun 4, 2020 at 11:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.