Free Support Forum -

Find out if Excel file is protected with password

I am using Aspose Cells java library. My application will receive an excel file that maybe password protected or may not be protected. I will not know that before hand, I need to find out in my application. What is the java code to find out if the excel file is password protected?


See the following sample code for your reference:
Sample code:

FileFormatInfo info = FileFormatUtil.detectFileFormat("f:\\files\\workbook_protected.xlsx");
        if (info.isEncrypted())
        	//since the file is protected, so you got to load it by specifying valid password in code.
        	com.aspose.cells.LoadOptions loadoptions = new com.aspose.cells.LoadOptions(com.aspose.cells.LoadFormat.XLSX);
            //give the valid password
        	Workbook wb = new Workbook("f:\\files\\workbook_protected.xlsx", loadoptions);
        	System.out.println("loaded password procted file");
		Workbook wb = new Workbook("f:\\files\\workbook_protected.xlsx");
		System.out.println("loaded file"); 

Hope, this helps a bit.