You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by "Simmons, Tom (GE Oil & Gas, VG)" <to...@ge.com> on 2011/09/02 13:14:41 UTC

How to check whether an XLSX has a read password

Hi

I am trying to check whether XLSX files that are being uploaded have a password.  I won't know the password, I simply want to prevent any password protected files being uploaded.

I've done a lot of search on Google and of the Apache POI site, but I'm afraid failed to find an answer, or at least one that works.

What I think I have gleaned so far is I should open the file...

POIFSFileSystem pfs = new POIFSFileSystem(new File("d:\\file.xlsx"));

And the do something like this...

EncryptionInfo e = new EncryptionInfo(pfs);
Decryptor d = Decryptor.getInstance(e);
d.verifyPassword(Decryptor.DEFAULT_PASSWORD);


However, every time the POIFSFileSystem line is executed I get an OfficeXmlFileException, the message is...

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)

The file I'm using is most certainly a valid xlsx file generated in Excel 2010, with nothing more complicated that a single cell with two words in it.  At this stage it's not even password protected!


Thanks for any help offered.

Tom

Re: How to check whether an XLSX has a read password

Posted by Nick Burch <ni...@alfresco.com>.
On Fri, 23 Sep 2011, ndrewh wrote:
> I'm trying to open an encrypted XLSX file with the following code:
>
> POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("Book1.xlsx"));
> EncryptionInfo info = new EncryptionInfo(fs);
> Decryptor d = new Decryptor(info);
> d.verifyPassword("hula");
> XSSFWorkbook wb = new XSSFWorkbook(d.getDataStream(fs));
>
> But, I still get this error: Exception in thread "main"
> org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data
> appears to be in the Office 2007+ XML.

Looks like your xlsx file isn't encrypted after all. Just pass the File to 
XSSFWorkbook and it should be able to open it just fine, no need to go via 
POIFSFileSystem and the Decryptor

Nick

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: How to check whether an XLSX has a read password

Posted by ndrewh <nd...@yahoo.com>.
I'm trying to open an encrypted XLSX file with the following code:

POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("Book1.xlsx"));
EncryptionInfo info = new EncryptionInfo(fs);
Decryptor d = new Decryptor(info);
d.verifyPassword("hula");
XSSFWorkbook wb = new XSSFWorkbook(d.getDataStream(fs));

But, I still get this error: 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).

Do you have any idea what I'm doing wrong and what the solution is?

--
View this message in context: http://apache-poi.1045710.n5.nabble.com/How-to-check-whether-an-XLSX-has-a-read-password-tp4761975p4835150.html
Sent from the POI - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: How to check whether an XLSX has a read password

Posted by Nick Burch <ni...@alfresco.com>.
On Fri, 2 Sep 2011, Simmons, Tom (GE Oil & Gas, VG) wrote:
> What I think I have gleaned so far is I should open the file...
>
> POIFSFileSystem pfs = new POIFSFileSystem(new File("d:\\file.xlsx"));

Things are a bit tricky for you. POIFSFileSystem is only used for OLE2 
based documents. That normally means .xls, .doc and .ppt (amongst others)

If you want to open a regular OOXML file (such as a .xlsx or a .pptx) you 
need to use OPCPackage (or just the appropriate usermodel class like 
XSSFWorkbook)

However, encrypted OOXML files are special. They're actually stored as a 
OLE2 file containing the encrypted xml contents


So, if you have a .xlsx file, and you want to know if it's encrypted or 
not, you could do:

boolean isEncrypted = false;
try {
    new POIFSFileSystem(new FileInputStream("question.xlsx"));
    isEncrypted = true;
} catch(OfficeXmlFileException e) {
    // This is a regular ooxml .xlsx file
}

Nick

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org