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



你可能感兴趣的:(POI)