java 使用 write.xlsx 用 R 包 xlsx 替换现有工作表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28053185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 12:56:33  来源:igfitidea点击:

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

javarexceloverwriter-xlsx

提问by Filippo

I am using package xlsxVersion: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).

我正在使用包xlsx版本:0.5.7 日期:2014-08-01。在 R 版本 3.0.1 (2013-05-16) 中——“Good Sport”平台:i386-w64-mingw32/i386(32 位)。

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.

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

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.

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

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

错误在.jcall(wb, "Lorg/apache/poi/ss/usermodel/Sheet;", "createSheet",:java.lang.IllegalArgumentException:工作簿已经包含此名称的工作表

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

我真的需要多次更换现有的工作表。我怎样才能做到这一点?

thanks, Filippo

谢谢,菲利波

回答by solmonta

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

如果要将新数据框保存在现有的 excel 文件中,首先必须加载 x​​lsx 文件:

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:

最后一步是将整个工作簿保存为 .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

顺便说一句:xlsx-package 的文档非常好,对此类问题很有帮助:) http://cran.r-project.org/web/packages/xlsx/xlsx.pdf

回答by 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

可能是您计算机上安装的 Java 与 xlsx 库不兼容。以下线程讨论了关于同一个包的类似问题: 在此处输入链接描述

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

或者,您的问题可以通过不同的 Excel 相关包来解决,例如 XLConnect。请参阅此帖子: 在此处输入链接描述