使用Java Servlet从Excel工作表的响应中设置密码OutputStream

时间:2017-11-02 08:13:14

标签: java servlets encryption passwords outputstream

如果从响应中获取OutputStream,您是否可以将OutputStream的内容写入响应,并将其发送回浏览器? 在我的场景中,我想在下载工作表时为Excel文件设置密码。 所以写了这样的代码。

response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=TestFile.xls");
XSSFWorkbook wb = new XSSFWorkbook();
Sheet s = wb.createSheet("Demo");
Row row1 = s.createRow(0);
Row row2 = s.createRow(1);
// create cells in the row
Cell row1col1 = row1.createCell(0);
Cell row1col2 = row1.createCell(1);
Cell row1col3 = row2.createCell(0);
Cell row1col4 = row2.createCell(1);
// add data to the cells
row1col1.setCellValue("City Name");
row1col2.setCellValue("University");
row1col3.setCellValue("Hyderabad");
row1col4.setCellValue("JNTU");
// Add password protection and encrypt the file
POIFSFileSystem fs = new POIFSFileSystem();
EncryptionInfo info = new EncryptionInfo(fs, EncryptionMode.agile);
Encryptor enc = info.getEncryptor();
// set the password
enc.confirmPassword("123");
// encrypt the file
OPCPackage opc = wb.getPackage();
OutputStream os = enc.getDataStream(fs);
opc.save(os);
opc.close();
fs.writeFilesystem(response.getOutputStream());

此处密码已设置为Excel工作表,但在获取以下错误时无法打开数据。 enter image description here请检查并提供您的答案为什么我没有获取数据。提前谢谢。

1 个答案:

答案 0 :(得分:-1)

Apache POI使用HSSFWorkbook对象为xlsx文件创建xls文件和XSSFWorkbook对象。请检查您用于创建xls的对象。

response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader("Content-Disposition", "attachment; filename="TestFile.xlsx");

                        File fileVal = new File("TestFile.xlsx");

                        XSSFWorkbook wb = new XSSFWorkbook();
                        Sheet s = wb.createSheet("Demo");

                              //Add data to your sheet

                           // write the excel to a file
                            try {
                                FileOutputStream fileOut = new FileOutputStream(fileVal);
                                wb.write(fileOut);
                                fileOut.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }

                            // Add password protection and encrypt the file
                            POIFSFileSystem fs = new POIFSFileSystem();
                            EncryptionInfo info = new EncryptionInfo(fs, EncryptionMode.agile);
                            Encryptor enc = info.getEncryptor();

                            // set the password
                            enc.confirmPassword("123");

                            // encrypt the file
                            OPCPackage opc = OPCPackage.open(fileVal, PackageAccess.READ_WRITE);
                            OutputStream os = enc.getDataStream(fs);
                            opc.save(os);
                            opc.close();

                            // save the file back to the filesystem
                            FileOutputStream fos = new FileOutputStream(fileVal);
                            fs.writeFilesystem(response.getOutputStream());
                            fos.close();