Skip to content

Instantly share code, notes, and snippets.

@ozero
Created July 13, 2011 10:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ozero/1080046 to your computer and use it in GitHub Desktop.
Save ozero/1080046 to your computer and use it in GitHub Desktop.
apache poi excel password test
package jp.ozero.poitest;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
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;
public class Poitest {
public static void main(final String[] args) throws Exception {
// http://poi.apache.org/encryption.html
String fname = "c:\\enc.xls";
FileInputStream input = null;
BufferedInputStream binput = null;
POIFSFileSystem poifs = null;
try {
input = new FileInputStream(fname);
binput = new BufferedInputStream(input);
poifs = new POIFSFileSystem(binput);
Biff8EncryptionKey.setCurrentUserPassword("MYPASSWORD");
HSSFWorkbook workbook = new HSSFWorkbook(poifs);
//
HSSFSheet sheet = workbook.getSheetAt(0);
int rows = sheet.getLastRowNum();
System.out.println("rows:"+rows);
for (int i = 0; i <= rows; i++) {
HSSFRow row = sheet.getRow(i);
if (row == null)
continue;
int cols = row.getLastCellNum();
System.out.println("cols:"+cols);
for (int j = 0; j <= cols; j++) {
HSSFCell cell = row.getCell((int) j);
if (cell == null)
continue;
if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
HSSFRichTextString val = cell.getRichStringCellValue();
System.out.println(val);
}
if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
double val = cell.getNumericCellValue();
System.out.println(val);
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
binput.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("done");
}
}
@LamaBimal
Copy link

what encryption key is used for the xssfWorkbook?

@ozero
Copy link
Author

ozero commented Mar 8, 2018

Did you mean this line?
https://gist.github.com/ozero/1080046#file-poitest-java-L31
Biff8EncryptionKey.setCurrentUserPassword

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment