I'm a bit impressed because I couldn't find an answer so far.
I have a small R script that is loading data from one source and writing a few dozens Excel files from it. The problem now is: those files need a password protection.
Currently my script is using the XLConnect library to save the excel files. The script is something like this:
# 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)
}
So, making it simple: how do I create an Excel file with password protection in R?
You should specify whether you're looking to protect an entire workbook or a single sheet. If you're looking to password protect a single sheet, you can use the following rJava function (before saving the workbook):
The ".jcall" function applies the password to the indicated sheet within the workbook. The "rJava" library must be installed and working properly for this to work.
Note
This function only works when using the XLSX package to manipulate Excel files in R. I worked with XLConnect to write the files initially, then, using XLSX, read them back in, apply password protection, and re-save the files to the same path.
Overview
After reading How do I password-protect an Excel file created in R with write.xlsx?, it seems versioning of certain packages is at play here.
This might be particular to someone's environment, but this solution just helped a colleague and I write a
data.frame
to a password protected.xlsx
file:If you have access to a file archiver and can't install
rJava
, a possible workaround that also works with other output formats is to save the files as usual and then archive them with password protection.E.g. using 7-zip: