How do I modify an existing a sheet in an Excel Workbook using Openxlsx package in R?

31,246

Solution 1

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.

Solution 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!
Share:
31,246

Related videos on Youtube

Nipun Arora
Author by

Nipun Arora

Updated on August 31, 2021

Comments

  • Nipun Arora
    Nipun Arora over 2 years

    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?

    • leosz
      leosz over 8 years
      I cannot test this in the computer I am using at the moment but have you tried using the argument append = TRUE?
    • Nipun Arora
      Nipun Arora over 8 years
      "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.
    • jzadra
      jzadra over 5 years
      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.
  • Brash Equilibrium
    Brash Equilibrium about 6 years
    This corrupts my file.
  • R. Schifini
    R. Schifini about 6 years
    @BrashEquilibrium just tried it with Excel 2013 and the latest version of openxlsx and it still works for me.
  • Brash Equilibrium
    Brash Equilibrium about 6 years
    That’s odd. I just ended up using writexl to construct a Woekbook anew.
  • jzadra
    jzadra over 5 years
    This doesn't work if the workbook doesn't exist for the first sheet.
  • R. Schifini
    R. Schifini over 5 years
    @jzadra I tried changing the order of worksheets and this still writes the data on the intended sheet.
  • jzadra
    jzadra over 5 years
    @R.Schifini The issue with my use case is that I don't start with an existing excel file. I just want to save multiple tables to a new file in a loop. Using write.xlsx() and specifying a sheet name should only overwrite that sheet if it exists, not the entire workbook.
  • R. Schifini
    R. Schifini over 5 years
    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")
  • R. Schifini
    R. Schifini over 5 years
    @jzadra your problem is different than the question asked. If you try the comment above you will see that you need to create an empty workbook, then start adding worksheets, then the data, loop it as many times as you want, and finallly write the workbook to hdd.
  • Malan Kriel
    Malan Kriel over 5 years
    A much-improved answer is given in link. Using example @R.Schifini used it would be: DFToExport = list(test1 = data1, test2 = data2) write.xlsx(DFToExport, "test.xlsx")
  • Josh
    Josh about 4 years
    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!
  • Gwang-Jin Kim
    Gwang-Jin Kim almost 4 years
    @Josh I added some code using the code/data of original question.
  • Josh
    Josh almost 4 years
    @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.
  • grapestory
    grapestory over 2 years
    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.