Reputation: 691
I have generated an xlsx password protected file using Apache POI framework, below added my code. however, Im unable to open the generated file using Excel tool. I am able to open it using Plan Maker free tool. any advises are welcome.
I have below jars used, commons-collections4-4.4.jar, commons-compress-1.20.jar, commons-math3-3.0.jar, poi-4.1.2.jar, poi-ooxml-4.1.2.jar, poi-ooxml-schemas-4.1.2.jar, SparseBitSet-1.1.jar, xmlbeans-3.1.0.jar
package com.pega.gcs.createworkbook;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ProtectedExcelFile {
public static void main(final String... args) throws Exception {
String fname = "FromJavaSecure1.xlsx";
String label="Sheet1-Abc";
String cellvalue="Cell1";
org.apache.poi.xssf.usermodel.XSSFSheet sheet;
org.apache.poi.xssf.usermodel.XSSFRow row;
org.apache.poi.xssf.usermodel.XSSFCell cell;
org.apache.poi.hssf.usermodel.HSSFSheet hsheet;
org.apache.poi.hssf.usermodel.HSSFRow hrow;
org.apache.poi.hssf.usermodel.HSSFCell hcell;
FileInputStream fileInput = null;
BufferedInputStream bufferInput = null;
POIFSFileSystem poiFileSystem = null;
FileOutputStream fileOut = null;
try {
try(
HSSFWorkbook workbook = new HSSFWorkbook();
)
{
hsheet=workbook.createSheet(label); hrow=hsheet.createRow(0);
hcell=hrow.createCell(0); hcell.setCellValue(cellvalue);
Biff8EncryptionKey.setCurrentUserPassword("secret");
fileOut = new FileOutputStream(fname);
workbook.writeProtectWorkbook(Biff8EncryptionKey.getCurrentUserPassword(), "");
workbook.write(fileOut);
}
}
catch (Exception ex) {
System.out.println(ex.getMessage());
}
finally {
/*
* try {
*
* bufferInput.close(); } catch (IOException ex) {
*
* System.out.println(ex.getMessage()); }
*
* try {
*
* fileOut.close(); } catch (IOException ex) {
*
* System.out.println(ex.getMessage()); }
*/
}
}
}
Upvotes: 0
Views: 1395
Reputation: 691
After referring to https://poi.apache.org/encryption.html , manage to get below code working for my requirement.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package excelpasswordgeneration;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackageAccess;
import org.apache.poi.poifs.crypt.EncryptionInfo;
import org.apache.poi.poifs.crypt.EncryptionMode;
import org.apache.poi.poifs.crypt.Encryptor;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
/**
*
* @author venu
*/
public class ExcelPasswordGeneration {
public static void main(String[] args) throws Exception {
try (POIFSFileSystem fs = new POIFSFileSystem()) {
EncryptionInfo info = new EncryptionInfo(EncryptionMode.agile);
// EncryptionInfo info = new EncryptionInfo(EncryptionMode.agile, CipherAlgorithm.aes192, HashAlgorithm.sha384, -1, -1, null);
Encryptor enc = info.getEncryptor();
enc.confirmPassword("foobaa");
String label = "Sheet1-Raja";
String cellvalue = "Cell1";
org.apache.poi.xssf.usermodel.XSSFSheet sheet;
org.apache.poi.xssf.usermodel.XSSFRow row;
org.apache.poi.xssf.usermodel.XSSFCell cell;
try (
XSSFWorkbook workbook = new XSSFWorkbook()) {
sheet = workbook.createSheet(label);
row = sheet.createRow(0);
cell = row.createCell(0);
cell.setCellValue(cellvalue);
ByteArrayOutputStream out = new ByteArrayOutputStream();
workbook.write(out);
// Read in an existing OOXML file and write to encrypted output stream
// don't forget to close the output stream otherwise the padding bytes aren't added
try (OPCPackage opc = OPCPackage.open(new ByteArrayInputStream(out.toByteArray()));
OutputStream os = enc.getDataStream(fs)) {
opc.save(os);
}
// Write out the encrypted version
try (FileOutputStream fos = new FileOutputStream("sample-encrypted.xlsx")) {
fs.writeFilesystem(fos);
}
}
}
}
}
Upvotes: 1