dhatim / fastexcel

Generate and read big Excel files quickly
Other
669 stars 121 forks source link

How to read password protected Excel files #244

Closed neoamuro closed 1 year ago

neoamuro commented 1 year ago

If I read password protected Excel files, it is occur the exception "InvalidDataException".

Is there any options to read password protected Excel files by the FastExcel?

meiMingle commented 1 year ago

As far as I know, this project does not currently support reading encrypted documents. But you can get the decrypted data stream with the help of POI, and then use this library to read the data.

       try (POIFSFileSystem fileSystem = new POIFSFileSystem(new File("D://protectedTest.xlsx"))){
            EncryptionInfo info = new EncryptionInfo(fileSystem);
            Decryptor d = Decryptor.getInstance(info);
            if (!d.verifyPassword("1234")) {
                throw new RuntimeException("Unable to process: document is encrypted");
            }
            // parse dataStream
            try (InputStream dataStream = d.getDataStream(fileSystem); ReadableWorkbook fworkbook = new ReadableWorkbook(dataStream)) {
                Sheet sheet = fworkbook.getSheet(0).orElse(null);
                assert sheet != null;
                sheet.openStream().forEach(System.out::println);
            }
        } catch (GeneralSecurityException ex) {
            throw new RuntimeException("Unable to process encrypted document", ex);
        }

protectedTest.xlsx

meiMingle commented 1 year ago

You can also generate a protected excel file using a similar method, as follows.

    try (ByteArrayOutputStream bos = new ByteArrayOutputStream(); POIFSFileSystem fs = new POIFSFileSystem()) {
        Workbook wb = new Workbook(bos, "Test", "1.0");
        wb.setGlobalDefaultFont("Arial", 15.5);
        Worksheet ws = wb.newWorksheet("Worksheet 1");
        ws.value(0, 0, "Hello fastexcel");
        wb.finish();
        byte[] bytes = bos.toByteArray();
        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("1234");
        // 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(bytes)); OutputStream os = enc.getDataStream(fs)) {
            opc.save(os);
        }
        // Write out the encrypted version
        try (FileOutputStream fos = new FileOutputStream("D://protectedTest.xlsx")) {
            fs.writeFilesystem(fos);
        }
    }
neoamuro commented 1 year ago

You can also generate a protected excel file using a similar method, as follows.

    try (ByteArrayOutputStream bos = new ByteArrayOutputStream(); POIFSFileSystem fs = new POIFSFileSystem()) {
        Workbook wb = new Workbook(bos, "Test", "1.0");
        wb.setGlobalDefaultFont("Arial", 15.5);
        Worksheet ws = wb.newWorksheet("Worksheet 1");
        ws.value(0, 0, "Hello fastexcel");
        wb.finish();
        byte[] bytes = bos.toByteArray();
        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("1234");
        // 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(bytes)); OutputStream os = enc.getDataStream(fs)) {
            opc.save(os);
        }
        // Write out the encrypted version
        try (FileOutputStream fos = new FileOutputStream("D://protectedTest.xlsx")) {
            fs.writeFilesystem(fos);
        }
    }

Thank you for your help~! Your solution helped me a lot~!!