Reading an xlsx file using POIFSFileSystem

I need to cancel the protected xlsx.eg file Book1.xlsx The code below works fine for the first time, reads Book1.xlsx, decrypts it and writes it to the same file name again.

public static void unprotectXLSXSheet(String fileName, String password) {
        try{

            POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(fileName));
            EncryptionInfo info = new EncryptionInfo(fs);
            Decryptor d = Decryptor.getInstance(info);
            d.verifyPassword(password);
            InputStream is = d.getDataStream(fs);
            System.out.println(is.available());
            XSSFWorkbook wb = new XSSFWorkbook(OPCPackage.open(is));
            FileOutputStream fileOut;
            fileOut = new FileOutputStream(fileName);
            wb.write(fileOut);
             fileOut.flush();
            fileOut.close();
            }catch(FileNotFoundException ex){
                ex.printStackTrace();
            }catch(IOException ex){
                ex.printStackTrace();

But when the same code tries to access the newly created insecure Book1.xlsx (or any other insecure xlsx file), it does not work and shows

Exception in thread "main" org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)
    at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:131)
    at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:104)
    at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:138)
    at com.wolseley.Excel.TestMainDummy.unprotectXLSXSheet(TestMainDummy.java:113)
    at com.wolseley.Excel.TestMainDummy.main(TestMainDummy.java:52)

I need help reading the xlsx file, as well as unlocking it with a password, as done above.

+4
source share
2 answers

Basically, the following line of code does not work for Office 2007+ XML documents:

POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(fileName));

So, first you need to check the header in the input stream, regardless of whether it supports this:

POIFSFileSystem.hasPOIFSHeader(is)

, true. hasPOIFSHeader , mark/reset, PushbackInputStream, .

, :

public static void unprotectXLSXSheet(String fileName, String password) throws Exception {
    InputStream is = null;
    FileOutputStream fileOut = null;

    try {
        is = new FileInputStream(fileName);
        if (!is.markSupported()) {
            is = new PushbackInputStream(is, 8);
        }

        if (POIFSFileSystem.hasPOIFSHeader(is)) {
            POIFSFileSystem fs = new POIFSFileSystem(is);
            EncryptionInfo info = new EncryptionInfo(fs);
            Decryptor d = Decryptor.getInstance(info);
            d.verifyPassword(password);
            is = d.getDataStream(fs);
        }

        System.out.println(is.available());
        XSSFWorkbook wb = new XSSFWorkbook(OPCPackage.open(is));
        fileOut = new FileOutputStream(fileName);
        wb.write(fileOut);
        fileOut.flush();
    } finally {
        if (is != null) {
            is.close();
        }
        if (fileOut != null) {
            fileOut.close();
        }
    }
}
+5

.

Office 2007+ java poi

, , POIXMLProperties, - :

OPCPackage pkg = OPCPackage.open(new File("file.xlsx"));
POIXMLProperties props = new POIXMLProperties(pkg);
System.out.println("The title is " + props.getCorePart().getTitle());

POIXMLProperties !

0

All Articles