POI 加密、解密xlsx文件

加密:

目前发现这种加密方式只支持 XSSFWorkbook创建的excel文件(Excel 2007 OOXML (.xlsx)格式),对于HSSFWorkbook不支持;

POI的jar包自己去官网下载。

加密:

@Test
public void encryptExcel_xlsx() throws Exception {
    //构建XSSFWorkbook
    XSSFWorkbook hssfWorkbook = new XSSFWorkbook();
    XSSFSheet sheet1 = hssfWorkbook.createSheet("sheet1");
    XSSFRow row1 = sheet1.createRow(0);
    XSSFCell cell1 = row1.createCell(0);
    cell1.setCellValue("cell1");
    cell1.setCellType(CellType.STRING);
    XSSFCell cell2 = row1.createCell(1);
    cell2.setCellValue(2);
    cell2.setCellType(CellType.NUMERIC);

    //保存此XSSFWorkbook对象为xlsx文件
    hssfWorkbook.write(new FileOutputStream(TEST_WORKBOOK_NAME));

    POIFSFileSystem fs = new POIFSFileSystem();
    EncryptionInfo info = new EncryptionInfo(EncryptionMode.standard);
    //final EncryptionInfo info = new EncryptionInfo(EncryptionMode.agile, CipherAlgorithm.aes256, HashAlgorithm.sha256, -1, -1, null);
    Encryptor enc = info.getEncryptor();

    //设置密码
    enc.confirmPassword(USER_PASSWORD);

    //加密文件
    OPCPackage opc = OPCPackage.open(new File(TEST_WORKBOOK_NAME), PackageAccess.READ_WRITE);
    OutputStream os = enc.getDataStream(fs);
    opc.save(os);
    opc.close();

    //把加密后的文件写回到流
    FileOutputStream fos = new FileOutputStream(TEST_WORKBOOK_NAME);
    fs.writeFilesystem(fos);
    fos.close();

}

解密:解密也只支持XSSFWorkbook创建的excel文件

有两种方式:

@Test
public void decryptExcel_xlsx1() throws IOException {
    Workbook wb = null;
    FileInputStream in = null;
    try {
        in = new FileInputStream(TEST_WORKBOOK_NAME);//读取xlsx文件
        wb = WorkbookFactory.create(in,USER_PASSWORD);//设置密码打开
    } catch (Exception e) {
        e.printStackTrace();
    }finally {
        in.close();
    }
    System.out.println("=================================");
    System.out.println("Number of Sheets:" + wb.getNumberOfSheets());
    System.out.println("Sheet3's name:" + wb.getSheetName(0));
    System.out.println();
}

第二种:

@Test
public void decryptExcel_xlsx2() throws Exception{
    Workbook wb = null;
    FileInputStream in = null;
    try {
        in = new FileInputStream(TEST_WORKBOOK_NAME);
        POIFSFileSystem poifsFileSystem = new POIFSFileSystem(in);
        EncryptionInfo encInfo = new EncryptionInfo(poifsFileSystem);
        Decryptor decryptor = Decryptor.getInstance(encInfo);
        decryptor.verifyPassword(USER_PASSWORD);
        wb = new XSSFWorkbook(decryptor.getDataStream(poifsFileSystem));
    } catch (Exception e) {
        e.printStackTrace();
    }finally {
        in.close();
    }
    System.out.println("=================================");
    System.out.println("Number of Sheets:" + wb.getNumberOfSheets());
    System.out.println("Sheet3's name:" + wb.getSheetName(0));
    System.out.println();
}

对于HSSFWorkbook文件的加密:目前只知道一种保护加密,就是加密后还是能以只读打开,如果修改就需要输入密码,百度说有一个付费的jar包支持加密,jxcell.jar

@Test
public void test1() throws IOException {
    HSSFWorkbook hssfWorkbook = new HSSFWorkbook();

    HSSFSheet myseet = hssfWorkbook.createSheet("myseet");
    HSSFSheet sheet2 = hssfWorkbook.createSheet("sheet2");
    // 在索引0的位置创建行(最顶端的行)
    HSSFRow hssfRow = myseet.createRow(0);
    //在索引0的位置创建单元格(左上端)
    HSSFCell cell = hssfRow.createCell(0);
    // 定义单元格为字符串类型
    cell.setCellType(CellType.STRING);
    // 在单元格中输入一些内容
    cell.setCellValue("poi生成Excel");

    OutputStream out = new FileOutputStream(userDir + "\\src\\test1.xlsx");

    //加密
    hssfWorkbook.writeProtectWorkbook(USER_PASSWORD,"admin");
    hssfWorkbook.write(out);
    out.close();
}
jxcell方式加密、解密

@Test
public void encryptExcel_xls() {
    try {
        View view = new View();
        view.read(TEST_WORKBOOK_NAME);//这是一个路径
        view.write(TEST_WORKBOOK_NAME, USER_PASSWORD); //参数为文件路径和密码

    } catch (Exception e) {
        e.printStackTrace();
    }
}
@Test
public void decryptExcel_xls() {
    try {
        View view = new View();
        view.read(TEST_WORKBOOK_NAME,USER_PASSWORD);
        view.write(TEST_WORKBOOK_NAME);

    } catch (Exception e) {
        e.printStackTrace();
    }
}



  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
您好!对于Java POI库,您可以使用以下代码来加密解密Excel文件(兼容xls和xlsx格式): 加密Excel文件: ```java import org.apache.poi.poifs.crypt.EncryptionInfo; import org.apache.poi.poifs.crypt.Encryptor; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import java.io.FileInputStream; import java.io.FileOutputStream; public class ExcelEncryptionExample { public static void main(String[] args) { String inputFile = "input.xlsx"; String outputFile = "output.xlsx"; String password = "password"; try { FileInputStream fis = new FileInputStream(inputFile); FileOutputStream fos = new FileOutputStream(outputFile); POIFSFileSystem fs = new POIFSFileSystem(); EncryptionInfo info = new EncryptionInfo(EncryptionInfo.OFFICE_OPENXML); Encryptor enc = info.getEncryptor(); enc.confirmPassword(password); enc.encryptDocument(fis, fos); fis.close(); fos.close(); System.out.println("Excel file encrypted successfully."); } catch (Exception e) { e.printStackTrace(); } } } ``` 解密Excel文件: ```java import org.apache.poi.poifs.crypt.Decryptor; import org.apache.poi.poifs.crypt.EncryptionInfo; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import java.io.FileInputStream; import java.io.FileOutputStream; public class ExcelDecryptionExample { public static void main(String[] args) { String inputFile = "encrypted.xlsx"; String outputFile = "decrypted.xlsx"; String password = "password"; try { FileInputStream fis = new FileInputStream(inputFile); FileOutputStream fos = new FileOutputStream(outputFile); POIFSFileSystem fs = new POIFSFileSystem(fis); EncryptionInfo info = new EncryptionInfo(fs); Decryptor dec = info.getDecryptor(); dec.verifyPassword(password); dec.decryptDocument(fis, fos); fis.close(); fos.close(); System.out.println("Excel file decrypted successfully."); } catch (Exception e) { e.printStackTrace(); } } } ``` 请确保将`input.xlsx`和`encrypted.xlsx`替换为您要加密解密的实际文件名,并将`password`替换为您选择的密码。加密后的文件将保存在`output.xlsx`中,解密后的文件将保存在`decrypted.xlsx`中。 希望能对您有所帮助!如有任何问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值