【问题标题】:Unable to open password protected xlsx file generated from Apache Poi using Excel tool无法使用 Excel 工具打开从 Apache Poi 生成的受密码保护的 xlsx 文件
【发布时间】:2020-04-01 08:36:03
【问题描述】:

我使用 Apache POI 框架生成了一个 xlsx 密码保护文件,下面添加了我的代码。但是,我无法使用 Excel 工具打开生成的文件。我可以使用 Plan Maker 免费工具打开它。欢迎任何建议。

我使用了以下 jar,commons-collections4-4.4.jar,commons-compress-1.20.jar,commons-math3-3.0.jar,poi-4.1.2.jar,poi-ooxml-4.1.2.jar , poi-ooxml-schemas-4.1.2.jar, SparseBitSet-1.1.jar, xmlbeans-3.1.0.jar

package com.pega.gcs.createworkbook;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey;
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;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ProtectedExcelFile {

    public static void main(final String... args) throws Exception {

        String fname = "FromJavaSecure1.xlsx";
        String label="Sheet1-Abc";  
        String cellvalue="Cell1";  

        org.apache.poi.xssf.usermodel.XSSFSheet     sheet;  
        org.apache.poi.xssf.usermodel.XSSFRow       row;  
        org.apache.poi.xssf.usermodel.XSSFCell      cell;

        org.apache.poi.hssf.usermodel.HSSFSheet     hsheet;  
        org.apache.poi.hssf.usermodel.HSSFRow       hrow;  
        org.apache.poi.hssf.usermodel.HSSFCell      hcell;

        FileInputStream fileInput = null;
        BufferedInputStream bufferInput = null;
        POIFSFileSystem poiFileSystem = null;
        FileOutputStream fileOut = null;

        try {


            try(

                    HSSFWorkbook workbook = new HSSFWorkbook();
               ) 
            {
                     hsheet=workbook.createSheet(label); hrow=hsheet.createRow(0);
                     hcell=hrow.createCell(0); hcell.setCellValue(cellvalue);

         Biff8EncryptionKey.setCurrentUserPassword("secret");

             fileOut = new FileOutputStream(fname);
         workbook.writeProtectWorkbook(Biff8EncryptionKey.getCurrentUserPassword(), "");

            workbook.write(fileOut);
        }
        }
        catch (Exception ex) {

            System.out.println(ex.getMessage());
        }
        finally {
            /*
             * try {
             * 
             * bufferInput.close(); } catch (IOException ex) {
             * 
             * System.out.println(ex.getMessage()); }
             * 
             * try {
             * 
             * fileOut.close(); } catch (IOException ex) {
             * 
             * System.out.println(ex.getMessage()); }
             */
        }
    }
}

【问题讨论】:

  • 尝试使用 XSSF 和 .xlsx 文件?请参阅poi.apache.org/encryption.html 获取所需的代码 sn-p
  • 尝试String fname = "FromJavaSecure1.xls"; 而不是String fname = "FromJavaSecure1.xlsx";。由于您正在创建一个HSSFWorkbook,它是二进制BIFF 文件格式,所以这个不能被命名为*.xlsx。只有XSSFWorkbook,文件格式为Office Open XML,必须命名为*.xlsx
  • 感谢@Gagravarr,poi encryption.html 页面有很好的例子。我已经设法解决了这个问题。
  • 嗨@AxelRichter,实际上我尝试了扩展xls和xlsx。我使用了 XSSFWorkbook 本身。无论如何,我在我的工作代码下面发布。

标签: excel apache-poi password-protection fileopendialog


【解决方案1】:

在参考https://poi.apache.org/encryption.html 之后,设法使以下代码满足我的要求。

   /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package excelpasswordgeneration;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackageAccess;
import org.apache.poi.poifs.crypt.EncryptionInfo;
import org.apache.poi.poifs.crypt.EncryptionMode;
import org.apache.poi.poifs.crypt.Encryptor;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Workbook;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 *
 * @author venu
 */
public class ExcelPasswordGeneration {

    public static void main(String[] args) throws Exception {

        try (POIFSFileSystem fs = new POIFSFileSystem()) {
            EncryptionInfo info = new EncryptionInfo(EncryptionMode.agile);
            // EncryptionInfo info = new EncryptionInfo(EncryptionMode.agile, CipherAlgorithm.aes192, HashAlgorithm.sha384, -1, -1, null);
            Encryptor enc = info.getEncryptor();
            enc.confirmPassword("foobaa");



            String label = "Sheet1-Raja";
            String cellvalue = "Cell1";

            org.apache.poi.xssf.usermodel.XSSFSheet sheet;
            org.apache.poi.xssf.usermodel.XSSFRow row;
            org.apache.poi.xssf.usermodel.XSSFCell cell;
            try (
                    XSSFWorkbook workbook = new XSSFWorkbook()) {
                sheet = workbook.createSheet(label);
                row = sheet.createRow(0);
                cell = row.createCell(0);
                cell.setCellValue(cellvalue);
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                workbook.write(out);


               // Read in an existing OOXML file and write to encrypted output stream
            // don't forget to close the output stream otherwise the padding bytes aren't added
            try (OPCPackage opc = OPCPackage.open(new ByteArrayInputStream(out.toByteArray()));
                    OutputStream os = enc.getDataStream(fs)) {
                opc.save(os);
            }
            // Write out the encrypted version
            try (FileOutputStream fos = new FileOutputStream("sample-encrypted.xlsx")) {
                fs.writeFilesystem(fos);
            }

            }

        }
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-07
    • 2018-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-27
    • 1970-01-01
    相关资源
    最近更新 更多