[英]Change cell value in openxlsx workbook
I want to use openxlsx to change an individual cell in a workbook sheet and write it back out as the same .xlsx without losing the styling, validation, etc of the original .xlsx file.我想使用openxlsx更改工作簿表中的单个单元格并将其写回相同的 .xlsx 而不丢失原始 .xlsx 文件的样式、验证等。 I stipulate openxlsx because it doesn't have a rJava dependency.
我规定 openxlsx 是因为它没有rJava依赖项。
Here's a dummy workbook:这是一个虚拟的工作簿:
library(openxlsx)
## Make a dummy workbook to read in
write.xlsx(list(iris = iris, mtcars = mtcars), file = 'test.xlsx')
wb <- loadWorkbook('test.xlsx')
isS4(wb)
How can I change the value of cell [2,1] so that it essentially is identical to the original .xlsx file but with on cell altered?如何更改单元格 [2,1] 的值,使其与原始 .xlsx 文件基本相同,但更改了单元格?
I can of course read in the workbook but I don't know what good that does me.我当然可以阅读工作簿,但我不知道这对我有什么好处。
m <- readWorkbook(wb)
m[2, 1] <- 20
m[1:5,]
writeData can help you with this. writeData可以帮助您解决这个问题。
test.fpath <- 'test.xlsx'
openxlsx::write.xlsx(list(iris = iris, mtcars = mtcars), file = test.fpath)
.wb <- openxlsx::loadWorkbook(test.fpath)
openxlsx::writeData(
wb = .wb,
sheet = 1,
x = 20,
xy = c(2,1)
)
openxlsx::saveWorkbook(
.wb,
test.fpath,
overwrite = TRUE
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.