Free Support Forum - aspose.com

Find out if Excel file is protected with password

Hello,
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?
Thanks,
Shashi.

@shashimn,

See the following sample code for your reference:
e.g
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
            loadoptions.setPassword("test1");
            
        	Workbook wb = new Workbook("f:\\files\\workbook_protected.xlsx", loadoptions);
        	
        	System.out.println("loaded password procted file");
        }
        else
        {
		Workbook wb = new Workbook("f:\\files\\workbook_protected.xlsx");
		System.out.println("loaded file"); 
       }

Hope, this helps a bit.