Unable to open excel file (.xls)

Hi all,

Codes as below and i have attached the error pic , thank you for help. :slight_smile:

[php]<?php

$filename = “C:\wamp\www\WebForm\book2.xls”;

$sheet1 = 1;

$arr=array(1=>‘a’,‘b’,‘c’,‘d’);

$sheet2 = “sheet2”;

$excel_app = new COM(“Excel.Application”) or Die (“Did not connect”);

print “Application name: {$excel_app->Application->value}
\n” ;

print “Loaded version: {$excel_app->Application->version}
\n”;

print $filename;

//$excel_app->Application->Visible = 1; #Make Excel visible.

$Workbook = $excel_app->Workbooks->Open($filename) or Die(“Did not open $filename $Workbook”);

$Worksheet = $Workbook->Worksheets($sheet1);

echo “AMIT YADAV
”;

$Worksheet->activate;

$excel_cell = $Worksheet->Range(“C1”);

$excel_cell->activate;

for($i=1; $i<4;$i++)

{

for($j=1; $j<4;$j++)

  {

     $excel_cell = $Worksheet->Range($arr[$i].$j);

     echo "<br> The value is $arr[$i].$j  => " . $excel_cell->value;

  }

}

$excel_result = $excel_cell->value;

$Workbook->Close;

unset($Worksheet);

unset($Workbook);

$excel_app->Workbooks->Close();

$excel_app->Quit();

unset($excel_app);

?>

[/php]

Brgds/Brandon Chau

Seems like you didn’t attach the error

Oh, sorry :o

Thanks ^^"


The exception should have more info in its description, do you have more data in the error log?

Or you can try to add “show all errors” to the environment

Or you can try to wrap it in a try/catch, if it really is an exception you can get the error message that way

Hi JimL,

  1. I don’t see any error in log file

  2. Actually, i have set error_reporting = E_ALL in php.ini file

  3. I tried to add try/catch exception , but it seems not correct, because the result isn’t expected ::slight_smile: The result has been attached and i modified the codes as below.

[php]

<?php try{ $filename = "C:\wamp\www\WebForm\book2.xls"; $sheet1 = 1; $arr=array(1=>'a','b','c','d'); $sheet2 = "sheet2"; $excel_app = new COM("Excel.Application") or Die ("Did not connect"); print "Application name: {$excel_app->Application->value}
\n" ; print "Loaded version: {$excel_app->Application->version}
\n"; print $filename; $Workbook = $excel_app->Workbooks->Open($filename) or Die("Did not open $filename $Workbook"); $Worksheet = $Workbook->Worksheets($sheet1); echo "AMIT YADAV
"; $Worksheet->activate; $excel_cell = $Worksheet->Range("C1"); $excel_cell->activate; for($i=1; $i<4;$i++) { for($j=1; $j<4;$j++) { $excel_cell = $Worksheet->Range($arr[$i].$j); echo "
The value is $arr[$i].$j => " . $excel_cell->value; } } $excel_result = $excel_cell->value; $Workbook->Close; unset($Worksheet); unset($Workbook); $excel_app->Workbooks->Close(); $excel_app->Quit(); unset($excel_app); } catch(Exception $e) { echo 'Message: ' .$e->getMessage(); } ?>

[/php]

Thank you.

Brgds/Brandon Chau


Seems like wamp is taking over the error handling, have you checked if wamp has any logs?

Hi JimL,

Do you mean the access.log file under C:\wamp\logs directory?

I see below entries mostly in that file as below. Thank you

127.0.0.1 - - [29/Apr/2014:11:36:46 +0800] “GET /webform/test.php HTTP/1.1” 200 1526
127.0.0.1 - - [29/Apr/2014:11:37:52 +0800] “GET /webform/test.php HTTP/1.1” 200 1526
127.0.0.1 - - [29/Apr/2014:11:39:31 +0800] “GET /webform/test_6mhsr4rwtv.php HTTP/1.1” 200 1570
127.0.0.1 - - [29/Apr/2014:11:39:55 +0800] “GET /webform/test.php HTTP/1.1” 200 1526
127.0.0.1 - - [29/Apr/2014:11:40:44 +0800] “GET /webform/test.php HTTP/1.1” 200 1526
127.0.0.1 - - [29/Apr/2014:11:40:54 +0800] “GET /webform/test.php HTTP/1.1” 200 1871
127.0.0.1 - - [29/Apr/2014:11:43:14 +0800] “GET /webform/test.php HTTP/1.1” 200 1526
127.0.0.1 - - [29/Apr/2014:11:43:45 +0800] “GET /webform/test.php HTTP/1.1” 200 1526

Brgds/Brandon Chau

Sponsor our Newsletter | Privacy Policy | Terms of Service