如何使用R中的Openxlsx包修改Excel工作簿中的现有工作表? [英] How do I modify an existing a sheet in an Excel Workbook using Openxlsx package in R?

查看:428
本文介绍了如何使用R中的Openxlsx包修改Excel工作簿中的现有工作表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用"openxlsx"包来读取和写入Excel文件.我有一个固定的文件,其中包含一个名为数据"的工作表,供其他工作表中的公式使用.我想更新此数据表而又不碰其他数据表. 我正在尝试以下代码:

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")

但是,这会删除excel文件并仅使用数据"表中的新数据创建一个新文件,而其他所有文件都将被删除.有什么建议吗?

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?

推荐答案

尝试一下:

wb <- loadWorkbook("Revenue.xlsx")
writeData(wb, sheet = "Data", Rev_4, colNames = F)
saveWorkbook(wb,"Revenue.xlsx",overwrite = T)

您需要加载整个工作簿,然后修改其数据,然后将其保存到磁盘.使用writeData,您还可以指定开始的行和列.而且,您还可以在保存到磁盘之前修改其他部分.

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.

这篇关于如何使用R中的Openxlsx包修改Excel工作簿中的现有工作表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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