error 'Cannot read encrypted file' when loading password protected file

Mar 20, 2009 at 3:45 AM
When i try and open a password protected file I get the following error.

Fatal error
: Uncaught exception 'Exception' with message 'Cannot read encrypted file' in C:\wamp\www\PHPExcel\Reader\Excel5.php:439 Stack trace: #0 C:\wamp\www\test_excel.php(9): PHPExcel_Reader_Excel5->load('nb.xls') #1 {main} thrown in C:\wamp\www\PHPExcel\Reader\Excel5.php on line 439

The following is the code

        require_once 'PHPExcel/Reader/Excel5.php';
        require_once 'PHPExcel/Writer/PDF.php';

        $objReader = new PHPExcel_Reader_Excel5();
        $objReader->setLoadSheetsOnly( array("sales summary") );
        $objReader->setReadDataOnly(true);
        $objPHPExcel = $objReader->load($_FILES["file"]["tmp_name"]);
        
        $objPHPExcel->setActiveSheetIndex(0);
        $cell_value = $objPHPExcel->getActiveSheet()->getCell('A1')->getValue();
Developer
Mar 20, 2009 at 4:27 AM
Sorry, reading / writing password protected (encrypted) Excel files is not possible. Neither for Excel5 nor Excel2007. See also:
http://phpexcel.codeplex.com/WorkItem/View.aspx?WorkItemId=9101

I'm not even sure what OpenOffice.org is capable of doing.

Mar 20, 2009 at 4:35 AM
<style> <!-- /* Font Definitions */ @font-face {font-family:"Cambria Math"; panose-1:2 4 5 3 5 4 6 3 2 4; mso-font-charset:1; mso-generic-font-family:roman; mso-font-format:other; mso-font-pitch:variable; mso-font-signature:0 0 0 0 0 0;} @font-face {font-family:Calibri; panose-1:2 15 5 2 2 2 4 3 2 4; mso-font-charset:0; mso-generic-font-family:swiss; mso-font-pitch:variable; mso-font-signature:-1610611985 1073750139 0 0 159 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-unhide:no; mso-style-qformat:yes; mso-style-parent:""; margin:0in; margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:12.0pt; font-family:"Times New Roman","serif"; mso-fareast-font-family:Calibri; mso-fareast-theme-font:minor-latin;} .MsoChpDefault {mso-style-type:export-only; mso-default-props:yes; font-size:10.0pt; mso-ansi-font-size:10.0pt; mso-bidi-font-size:10.0pt;} @page Section1 {size:8.5in 11.0in; margin:1.0in 1.0in 1.0in 1.0in; mso-header-margin:.5in; mso-footer-margin:.5in; mso-paper-source:0;} div.Section1 {page:Section1;} --> </style>

That’s what interesting. The file isint actually encrypted. I see the option to encrypt the file and I have not used it.

It appears that workbook protection is what is causing it. When I turn the protect workbook off it works fine.

Any chance this is repairable?


Will there be better error trapping for when an encrypted file is opened?

Developer
Mar 20, 2009 at 4:59 AM
Hmm... Interesting, I must admit you must be right. Can you try to comment that line 439 in Excel5 reader throwing the exception. It should then work.

This is something I will try to get fixed for next release.

Mar 20, 2009 at 6:37 AM
I commented out the line and now I get a huge amount of the following


Notice: Uninitialized string offset: 26 in C:\wamp\www\norwexcs\PHPExcel\Reader\Excel5.php on line 4602

Notice: Uninitialized string offset: 27 in C:\wamp\www\norwexcs\PHPExcel\Reader\Excel5.php on line 4602

Notice: Uninitialized string offset: 28 in C:\wamp\www\norwexcs\PHPExcel\Reader\Excel5.php on line 460

I realized i was using an older version of the class when I got the intial error so I upgraded to the latest and the same issue just on a different line number.

Do you suspect that this is something that is fixable in the future?
Developer
Mar 20, 2009 at 7:19 AM
Ok, commenting that line won't work. I now remember what the problem was. All records are encrypted whenever you enable workbook protection with a non-empty password.

There is no fix for this around the corner. I think this is something we would look at in the future, but it is probably not easy to solve. Unfortunately.

Nov 9, 2012 at 3:09 PM

 

a stable solution to date?

Coordinator
Nov 9, 2012 at 4:56 PM

No change. This is a complex problem, and low priority

Feb 14, 2013 at 7:01 PM
hi
i found the solution in Perl. the name librarie is Spreadsheet-ParseExcel-0.59

I use perl to run the function on PHP $lastLine = system('file.pl file.xls 2>&1',$output);

The documentation is excellent.

http://search.cpan.org/~jmcnamara/Spreadsheet-ParseExcel-0.59/