Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R使用密码保护保存excel工作簿_R_Excel_Xlconnect - Fatal编程技术网

R使用密码保护保存excel工作簿

R使用密码保护保存excel工作簿,r,excel,xlconnect,R,Excel,Xlconnect,我有点印象深刻,因为到目前为止我还没有找到答案 我有一个小的R脚本,它从一个源加载数据并从中编写几十个Excel文件。现在的问题是:这些文件需要密码保护 目前我的脚本正在使用库保存excel文件。脚本如下所示: # Load data from source source = <connection string to source> dataFromSource = <read from source> # Process for (data in dataFromS

我有点印象深刻,因为到目前为止我还没有找到答案

我有一个小的R脚本,它从一个源加载数据并从中编写几十个Excel文件。现在的问题是:这些文件需要密码保护

目前我的脚本正在使用库保存excel文件。脚本如下所示:

# Load data from source
source = <connection string to source>
dataFromSource = <read from source>

# Process
for (data in dataFromSource) {
    wb = loadWorkbook( paste("myWorkbook", sourceName, ".xlsx"), create = TRUE)
    createSheet(wb, name = "sheet")
    <put data into the "sheet">
    saveWorkbook(wb)
}
#从源加载数据
来源=
dataFromSource=
#过程
for(数据源中的数据){
wb=loadWorkbook(粘贴(“myWorkbook”,sourceName,.xlsx),创建=TRUE)
createSheet(wb,name=“sheet”)
保存工作簿(wb)
}

因此,简单一点:如何在R中创建具有密码保护的Excel文件?

您应该指定是要保护整个工作簿还是单个工作表。如果要对单个工作表进行密码保护,可以使用以下rJava功能(在保存工作簿之前):

.jcall函数将密码应用于工作簿中指定的工作表。必须安装“rJava”库并正常工作,才能使其正常工作

注意


此功能仅在使用XLSX软件包操作R中的Excel文件时有效。我使用XLConnect最初编写文件,然后使用XLSX将其读回,应用密码保护,并将文件重新保存到同一路径。

如果您有权访问文件归档程序,但无法安装
rJava
,与其他输出格式一起使用的一种可能的解决方法是,像往常一样保存文件,然后使用密码保护对其进行归档

例如,使用7-zip:

setwd("c:/Program Files/7-Zip/")
pwd <- "123"
shell(paste0("7z a d:/Data/myfile.zip d:/Data/myfile.xlsx -p", pwd))
setwd(“c:/ProgramFiles/7-Zip/”)
pwd概述
在阅读之后,似乎某些包的版本控制在这里起作用

这可能是特定于某人的环境,但此解决方案只是帮助我和同事将
data.frame
写入受密码保护的
.xlsx
文件:

# install necessary packages -----
# note: downloading the latest versions of all packages from CRAN
install.packages(c("XLConnect", "XLConnectJars", "rJava", "xlsx", "openxlsx"))

# attach necessary packages ----
library(XLConnect)

# load but do not attach the write.xlsx() function from the xlsx package ----
xlsx::write.xlsx(x = mtcars, file = "mtcars.xlsx", password = "1234_fghj")

# confirm that the .xlsx file is password protected ----
openxlsx::openXL("mtcars.xlsx")

# session info ----
sessionInfo()
# R version 3.5.1 (2018-07-02)
# Platform: x86_64-redhat-linux-gnu (64-bit)
# Running under: Red Hat Enterprise Linux
# 
# Matrix products: default
# BLAS/LAPACK: /usr/lib64/R/lib/libRblas.so
# 
# locale:
# [1] LC_CTYPE=en_US.UTF-8         
# [2] LC_NUMERIC=C                 
# [3] LC_TIME=en_US.UTF-8          
# [4] LC_COLLATE=en_US.UTF-8       
# [5] LC_MONETARY=en_US.UTF-8      
# [6] LC_MESSAGES=en_US.UTF-8      
# [7] LC_PAPER=en_US.UTF-8         
# [8] LC_NAME=en_US.UTF-8          
# [9] LC_ADDRESS=en_US.UTF-8       
# [10] LC_TELEPHONE=en_US.UTF-8     
# [11] LC_MEASUREMENT=en_US.UTF-8   
# [12] LC_IDENTIFICATION=en_US.UTF-8
# 
# attached base packages:
# [1] stats     graphics  grDevices utils     datasets 
# [6] methods   base     
# 
# other attached packages:
# [1] XLConnect_0.2-15     XLConnectJars_0.2-15
# 
# loaded via a namespace (and not attached):
# [1] compiler_3.5.1 tools_3.5.1    yaml_2.2.0    
# [4] xlsxjars_0.6.1 rJava_0.9-10   xlsx_0.6.1  

嗨,瑞克,我的回答提供了你想要的帮助吗?@Dale抱歉,当你回答的时候,我已经不需要了。但我将尝试创建一个小演示来检查您的解决方案。谢谢:)
# install necessary packages -----
# note: downloading the latest versions of all packages from CRAN
install.packages(c("XLConnect", "XLConnectJars", "rJava", "xlsx", "openxlsx"))

# attach necessary packages ----
library(XLConnect)

# load but do not attach the write.xlsx() function from the xlsx package ----
xlsx::write.xlsx(x = mtcars, file = "mtcars.xlsx", password = "1234_fghj")

# confirm that the .xlsx file is password protected ----
openxlsx::openXL("mtcars.xlsx")

# session info ----
sessionInfo()
# R version 3.5.1 (2018-07-02)
# Platform: x86_64-redhat-linux-gnu (64-bit)
# Running under: Red Hat Enterprise Linux
# 
# Matrix products: default
# BLAS/LAPACK: /usr/lib64/R/lib/libRblas.so
# 
# locale:
# [1] LC_CTYPE=en_US.UTF-8         
# [2] LC_NUMERIC=C                 
# [3] LC_TIME=en_US.UTF-8          
# [4] LC_COLLATE=en_US.UTF-8       
# [5] LC_MONETARY=en_US.UTF-8      
# [6] LC_MESSAGES=en_US.UTF-8      
# [7] LC_PAPER=en_US.UTF-8         
# [8] LC_NAME=en_US.UTF-8          
# [9] LC_ADDRESS=en_US.UTF-8       
# [10] LC_TELEPHONE=en_US.UTF-8     
# [11] LC_MEASUREMENT=en_US.UTF-8   
# [12] LC_IDENTIFICATION=en_US.UTF-8
# 
# attached base packages:
# [1] stats     graphics  grDevices utils     datasets 
# [6] methods   base     
# 
# other attached packages:
# [1] XLConnect_0.2-15     XLConnectJars_0.2-15
# 
# loaded via a namespace (and not attached):
# [1] compiler_3.5.1 tools_3.5.1    yaml_2.2.0    
# [4] xlsxjars_0.6.1 rJava_0.9-10   xlsx_0.6.1