Use POI to decrypt a non-XML encrypted Excel spreadsheet

Leaving this here in case anyone else needs to use Apache POI to decrypt binary Excel files:

	// --> Source File: /tmp/EncryptedSpreadsheet.xls
	// --> Password: S3cr37P@ssw0rd
	// <-- Output File: /tmp/UnencryptedSpreadsheet.xls
	// Uses poi-4.0.1.jar from cfsimplicity/lucee-spreadsheet installed in /spreadsheetLibrary

	// Load an encrypted Excel spreadsheet using the password
	jFile = createObject( "java", "java.io.File").init( "/tmp/EncryptedSpreadsheet.xls" );
	jWorkbookFactory = createObject( "java", "org.apache.poi.ss.usermodel.WorkbookFactory", expandPath( "/spreadsheetLibrary/lib/poi-4.0.1.jar" ) ).create( jFile, "S3cr37P@ssw0rd" );

	// Output it as an unencrypted file
	jFileOutputStream = createObject( "java", "java.io.FileOutputStream" ).init( "/tmp/UnencryptedSpreadsheet.xls" );
	jWorkbookFactory.write( jFileOutputStream );
	jFileOutputStream.close();

	// See: https://gist.github.com/jeaguilar/157aa208ee90a36e53fc3ca0e012f179

I am using cfsimplicity/lucee-spreadsheet elsewhere, which allows me to rely on their version of the POI library (poi-4.0.1.jar), but you could use your own version, too.

1 Like