In the previous tutorials we saw how to read and write Excel files using the Apache POI library. In this tutorial we will see how to create password protected excel using Apache POI.
There are 2 types of password protections offered in Excel:
1) Protected Excel documents which need a password to open as shown in the screenshot below:
2) Protected Excel sheets which need password to edit
In this tutorial we will explain the first scenario and see how we can create password protected excel files using Apache POI.
Listed below is Java code which adds some simple content to the file and also enable password protection for the excel document.
Code to Create Password Protected Excel Using Apache POI:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
package
com.quicklyjava;
import
java.io.File;
import
java.io.FileOutputStream;
import
java.io.IOException;
import
java.io.OutputStream;
import
java.security.GeneralSecurityException;
import
java.util.Date;
import
org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import
org.apache.poi.openxml4j.opc.OPCPackage;
import
org.apache.poi.openxml4j.opc.PackageAccess;
import
org.apache.poi.poifs.crypt.*;
import
org.apache.poi.poifs.filesystem.POIFSFileSystem;
//apache poi imports
import
org.apache.poi.ss.usermodel.Cell;
import
org.apache.poi.ss.usermodel.CellStyle;
import
org.apache.poi.ss.usermodel.CreationHelper;
import
org.apache.poi.ss.usermodel.Row;
import
org.apache.poi.ss.usermodel.Sheet;
import
org.apache.poi.ss.usermodel.Workbook;
import
org.apache.poi.xssf.usermodel.XSSFWorkbook;
public
class
WritePasswordProtectedXLSX {
/**
* @param args
* @throws IOException
* @throws InvalidFormatException
* @throws GeneralSecurityException
*/
public
static
void
main(String[] args)
throws
IOException, InvalidFormatException, GeneralSecurityException {
//create a new workbook
Workbook wb =
new
XSSFWorkbook();
//add a new sheet to the workbook
Sheet sheet1 = wb.createSheet(
"Sheet1"
);
//add 2 row to the sheet
Row row1 = sheet1.createRow(
0
);
Row row2 = sheet1.createRow(
1
);
//create cells in the row
Cell row1col1 = row1.createCell(
0
);
Cell row1col2 = row1.createCell(
1
);
//add data to the cells
row1col1.setCellValue(
"Top Secret Data 1"
);
row1col2.setCellValue(
"Top Secret Data 2"
);
//write the excel to a file
try
{
FileOutputStream fileOut =
new
FileOutputStream(
"c:/test/excel.xlsx"
);
wb.write(fileOut);
fileOut.close();
}
catch
(IOException e) {
e.printStackTrace();
}
//Add password protection and encrypt the file
POIFSFileSystem fs =
new
POIFSFileSystem();
EncryptionInfo info =
new
EncryptionInfo(fs, EncryptionMode.agile);
Encryptor enc = info.getEncryptor();
enc.confirmPassword(
"s3cr3t"
);
OPCPackage opc = OPCPackage.open(
new
File(
"c:/test/excel.xlsx"
), PackageAccess.READ_WRITE);
OutputStream os = enc.getDataStream(fs);
opc.save(os);
opc.close();
FileOutputStream fos =
new
FileOutputStream(
"c:/test/excel.xlsx"
);
fs.writeFilesystem(fos);
fos.close();
System.out.println(
"File created!!"
);
}
}
|
Lets go through some of the important code lines:
1) As we saw in the previous tutorial, the code below will create a Workbook object, add a work sheet to it, then add a couple of rows and cells to the sheet, and finally add some data to the cells
1
2
3
4
5
6
7
8
9
10
11
12
|
Workbook wb =
new
XSSFWorkbook();
Sheet sheet1 = wb.createSheet(
"Sheet1"
);
Row row1 = sheet1.createRow(
0
);
Row row2 = sheet1.createRow(
1
);
Cell row1col1 = row1.createCell(
0
);
Cell row1col2 = row1.createCell(
1
);
row1col1.setCellValue(
"Top Secret Data 1"
);
row1col2.setCellValue(
"Top Secret Data 2"
);
|
2) The code below saves the Workbook object created earlier to an Excel file on your file system:
1
2
3
4
5
6
7
|
try
{
FileOutputStream fileOut =
new
FileOutputStream(
"c:/test/excel.xlsx"
);
wb.write(fileOut);
fileOut.close();
}
catch
(IOException e) {
e.printStackTrace();
}
|
3) The code below gets an Encryptor object in order to encrypt and add password to the Excel file:
1
2
3
|
POIFSFileSystem fs =
new
POIFSFileSystem();
EncryptionInfo info =
new
EncryptionInfo(fs, EncryptionMode.agile);
Encryptor enc = info.getEncryptor();
|
4)The code below sets the password for opening the Excel files:
1
|
enc.confirmPassword(
"s3cr3t"
);
|
5) The code below reads the file from the file system and encrypts it:
1
2
3
4
|
OPCPackage opc = OPCPackage.open(
new
File(
"c:/test/excel.xlsx"
), PackageAccess.READ_WRITE);
OutputStream os = enc.getDataStream(fs);
opc.save(os);
opc.close();
|
6) The code below finally save the file back to the file system:
1
2
3
|
FileOutputStream fos =
new
FileOutputStream(
"c:/test/excel.xlsx"
);
fs.writeFilesystem(fos);
fos.close();
|
You should see the below message once you successfully run the program:
The file should be created on your filesystem at c:\test\excel.xlsx or any other location as specified by you in the Java code
Open the file and it should prompt you for the password (s3cr3t or whatever you had set in Java code):
You should be able to see the contents of the file once you enter the correct password:
Here is the Eclipse project for the code explained in this tutorial:
- See more at: http://www.quicklyjava.com/create-password-protected-excel-using-apache-poi/#sthash.vjhgymMp.dpuf