使用write.xlsx替换R包xlsx的现有工作表 [英] using write.xlsx to replace an existing sheet with R package xlsx

查看:3095
本文介绍了使用write.xlsx替换R包xlsx的现有工作表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用包 xlsx 版本:0.5.7日期:2014-08-01。在R版本3.0.1(2013-05-16) - 好运动平台:i386-w64-mingw32 / i386(32位)。



有一个xlsx文件至少有2张(比如说A和B)。我需要从A读取数据,编辑它们并将其保存在B中。这必须在期刊基础上进行。



我可以从A读取数据 read.xlsx 。编辑数据框后,我想将其保存在同一个xlsx文件中的现有工作表B中。



我尝试使用这一行

  write.xlsx(down, paste0(root,'/ registration reports / registration complete_WK.xlsx'),sheet =data_final,col.names = T,row.names = F,append = T,showNA = F)

但它给我这个错误:


错误在 .jcall(wb,Lorg / apache / poi / ss / usermodel / Sheet;,createSheet,
java.lang.IllegalArgumentException:该工作簿已经包含这个名字的一张表


我真的需要多次替换现有的表单,我该怎么做? / p>

谢谢,
Filippo

解决方案

将您的新数据框保存在现有的Excel文件中,您首先必须加载xlsx文件:

  wb<  -  loadWorkbook文件)

您将获得这样的工作表:

  sheet<  -  get您可以轻松地删除并添加(并因此替换)表格:  

>

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

比用数据框填充表格:

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

最后一步是将整个工作簿保存为.xlsx:

  saveWorkbook(wb,file)

btw:xlsx包的文档真的很好,有帮助的这样的问题:)
http://cran.r- project.org/web/packages/xlsx/xlsx.pdf


I am using package xlsx Version:0.5.7 Date: 2014-08-01. in R version 3.0.1 (2013-05-16) -- "Good Sport" Platform: i386-w64-mingw32/i386 (32-bit).

I have an xlsx file with at least 2 sheets (say A and B). I need to read data from A, edit them and save them in B. This has to be done on a periodical base.

I am able to read data from A with read.xlsx. After editing the data frame I want to save it in an existing sheet B in the same xlsx file.

I try with this line

write.xlsx(down, paste0(root,'/registration reports/registration complete_WK.xlsx'), sheet="data_final", col.names=T, row.names=F, append=T, showNA=F)

but it give me this error:

Error in .jcall(wb, "Lorg/apache/poi/ss/usermodel/Sheet;", "createSheet", : java.lang.IllegalArgumentException: The workbook already contains a sheet of this name

I really need to replace that existing sheet multiple times. How can I do that?

thanks, Filippo

解决方案

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

这篇关于使用write.xlsx替换R包xlsx的现有工作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆