using write.xlsx to replace an existing sheet with R package xlsx

坚强是说给别人听的谎言 提交于 2019-12-05 06:07:36

If you want to save your new dataframe in an existing excel file, you first have to load the xlsx-file:

wb <- loadWorkbook(file)

which sheets you have you'll get like this:

sheets <- getSheets(wb)

you can easily remove and add (and thus replace) sheets with:

removeSheet(wb, sheetName="Sheet1")
yourSheet <- createSheet(wb, sheetName="Sheet1")

than you can fill the sheets with dataframes:

addDataFrame(yourDataFrame, yourSheet, <options>)
addDataFrame(anotherDataFrame, yourSheet, startRow=nrow(yourDataFrame)+2)

and last step is saving the whole workbook as .xlsx:

saveWorkbook(wb, file)

btw: the documentation of the xlsx-package is really good and helpful on such questions :) http://cran.r-project.org/web/packages/xlsx/xlsx.pdf

David C.

It may be that the Java installed on your computer is incompatible with the xlsx library. The following thread discusses a similar issue with regard to the same package: enter link description here

Alternatively, your issue may be solved by a different Excel-related package, such as XLConnect. See this post: enter link description here

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!