출처 :
http://hibik.tistory.com/entry/엑셀-다운시-엑셀-파일에-암호-설정
POI 로 비밀번호로 보호된 엑셀 다운하는 jsp
POI 3.10 관련 라이브러리가 필요
MS2007 에서는 안됨 07이상에서 사용
java poi 3.15 사용했고
내용 아주 살짝 바꿈
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=test_"+timestamp+".xlsx;");
response.setHeader("Content-Description", "JSP Generated Data");
//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 {
ByteArrayOutputStream fileOut = new ByteArrayOutputStream();
wb.write(fileOut);
InputStream filein = new ByteArrayInputStream(fileOut.toByteArray());
POIFSFileSystem fs = new POIFSFileSystem();
//EncryptionInfo info = new EncryptionInfo(fs, EncryptionMode.agile);
EncryptionInfo info = new EncryptionInfo(EncryptionMode.agile);
Encryptor enc = info.getEncryptor();
enc.confirmPassword("AA");
OPCPackage opc = OPCPackage.open(filein);
OutputStream os = enc.getDataStream(fs);
opc.save(os);
opc.close();
OutputStream fileOut2 = null;
//out.clear();
//out = pageContext.pushBody();
fileOut2 = response.getOutputStream();
fs.writeFilesystem(fileOut2);
fileOut2.close();
fileOut.close();
}catch(Exception e){
e.printStackTrace();
}
http://www.quicklyjava.com/create-password-protected-excel-using-apache-poi/
이것도 참고