POI 로 비밀번호로 보호된 엑셀 다운하는 jsp 

POI 3.10 관련 라이브러리가 필요

MS2007 에서는 안됨 07이상에서 사용


 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);

Encryptor enc = info.getEncryptor();

enc.confirmPassword("AA");

OPCPackage opc = OPCPackage.open(filein);

OutputStream os = enc.getDataStream(fs);

opc.save(os);

opc.close();

System.out.println("File created!!");

 

OutputStream fileOut2 = null;

out.clear();

out = pageContext.pushBody();

fileOut2 = response.getOutputStream(); 

fs.writeFilesystem(fileOut2);

fileOut2.close();

fileOut.close();




}catch(Exception e){


e.printStackTrace();


}



블로그 이미지

hibik

카테고리

분류 전체보기 (12)
개발 (9)
기타 (1)

티스토리 툴바