How do I modify an existing sheet in an Excel Workbook using Openxlsx package?
Asked Answered
E

2

31

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?

Escurial answered 9/12, 2015 at 6:46 Comment(3)
I cannot test this in the computer I am using at the moment but have you tried using the argument append = TRUE?Allaallah
"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.Escurial
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.Dispread
F
41

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.

Footy answered 12/12, 2015 at 17:32 Comment(10)
This corrupts my file.Adown
@BrashEquilibrium just tried it with Excel 2013 and the latest version of openxlsx and it still works for me.Footy
That’s odd. I just ended up using writexl to construct a Woekbook anew.Adown
This doesn't work if the workbook doesn't exist for the first sheet.Dispread
@Dispread I tried changing the order of worksheets and this still writes the data on the intended sheet.Footy
@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.Dispread
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")Footy
@Dispread 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.Footy
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")Orangeman
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.Juglandaceous
F
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!
Franchescafranchise answered 12/3, 2020 at 13:3 Comment(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!Epinasty
@Epinasty I added some code using the code/data of original question.Gyromagnetic
@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.Epinasty

© 2022 - 2024 — McMap. All rights reserved.