Fatal error: Uncaught exception 'Exception' with message 'Could not open spreadsheet.xls for reading!

Topics: Developer Forum, Project Management Forum, User Forum
Sep 20, 2012 at 5:08 PM
Edited Sep 20, 2012 at 5:09 PM

Recently I have been working on some code that reads an Excel spreadsheet and imports the data into a remote database. When I run my script I get the following error message.....

Fatal error: Uncaught exception 'Exception' with message 'Could not open spreadsheet.xls for reading! File does not exist.' in /home/xoxox/public_html/xoxo/source/vendors/PHPExcel/Classes/PHPExcel/Reader/Excel5.php:519 Stack trace: #0 /home/xoxox/public_html/xoxox/source/vendors/PHPExcel/Classes/PHPExcel/IOFactory.php(258): PHPExcel_Reader_Excel5->canRead('spreadsheet.xls') #1 /home/xoxox/public_html/xoxox/source/vendors/PHPExcel/Classes/PHPExcel/IOFactory.php(192): PHPExcel_IOFactory::createReaderForFile('spreadsheet.xls') #2 /home/xoxox/public_html/xoxox/source/app/controllers/coverages_controller.php(209): PHPExcel_IOFactory::load('spreadsheet.xls') #3 [internal function]: CoveragesController->processSpreadsheet() #4 /home/xoxox/public_html/xoxox/source/cake/dispatcher.php(204): call_user_func_array(Array, Array) #5 /home/xoxox/public_html/xoxox/source/cake/dispatcher.php(171): Dispatcher->_invoke(Object(CoveragesController), Array) #6 /home/xoxox/public_html/xoxox/source/app/webroot/index.php(83): Dispatcher->dispatch() #7 {main} in /home/xoxox/public_html/xoxox/source/vendors/PHPExcel/Classes/PHPExcel/Reader/Excel5.php on line 519

Now with that said, I have read a couple of posts concerning the same issue but I didn't find a definite solution to the problem. Here is my script

 

public function processSpreadsheet() {
$saveTimeZone = date_default_timezone_get();
date_default_timezone_set('UTC'); // Php's date function uses this value!

require_once '/home/xoxox/public_html/xoxoxo/source/vendors/PHPExcel/Classes/PHPExcel/IOFactory.php';
$objPHPExcel = PHPExcel_IOFactory::load("spreadsheet.xls");
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
    $worksheetTitle     = $worksheet->getTitle();
    $highestRow         = $worksheet->getHighestRow(); // e.g. 10
    $highestColumn      = $worksheet->getHighestColumn(); // e.g 'F'
    $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
    $nrColumns = ord($highestColumn) - 64;
    echo "
The Worksheet ".$worksheetTitle." has "; echo $nrColumns . ' columns (A-' . $highestColumn . ') '; echo ' and ' . $highestRow . ' rows.'; echo '
Data: '; for ($row = 1; $row <= $highestRow; ++ $row) { $rw1 = $worksheet->getCellByColumnAndRow(1,$row)->getValue(); $rw2 = $worksheet->getCellByColumnAndRow(2,$row)->getValue(); $rw3 = $worksheet->getCellByColumnAndRow(3,$row)->getValue(); $rw4 = $worksheet->getCellByColumnAndRow(4,$row)->getValue(); $rw5 = $worksheet->getCellByColumnAndRow(5,$row)->getValue(); $rw6 = $worksheet->getCellByColumnAndRow(6,$row)->getValue(); $rw7 = $worksheet->getCellByColumnAndRow(7,$row)->getValue(); $rw8 = $worksheet->getCellByColumnAndRow(8,$row)->getValue(); $rw9 = $worksheet->getCellByColumnAndRow(9,$row)->getValue(); $rw10 = $worksheet->getCellByColumnAndRow(10,$row)->getValue(); $rw11 = $worksheet->getCellByColumnAndRow(11,$row)->getValue(); $slic = mysql_connect("xoxoxoxo", "xoxoxoxo", "xoxoxo"); if(!$slic) { die('Could not connect' . mysql_error());; } mysql_select_db("xoxoxo", $slic); $dbc = "INSERT INTO Coverages( Name, Comments, Premium, Effective_Date, Expiration_Date, Broker_First_Name , Broker_Middle_Initial, Broker_Last_Name, Broker_Suffix, Broker_License_Number, DateCreated) VALUES ('$rw1','$rw10','$rw2','$rw3', '$rw4', '$rw5','$rw6', '$rw7', '$rw8', '$rw9', '$rw11')"; for ($col = 0; $col < $highestColumnIndex; ++ $col) { $cell = $worksheet->getCellByColumnAndRow($col, $row); $val = $cell->getFormattedValue(); $dataType = PHPExcel_Cell_DataType::dataTypeForValue($val); echo ''; } $conn = mysql_query($dbc, $conn); { if (!msql_query($dbc, $conn)) { die ('Error: ' . mysql_error()); echo "recorded added"; } } } } date_default_timezone_set($saveTimeZone); }

 

' . $val . '

Having said all that, I think I have some errors in my coding. Any input on this issue would be greatly appreciated! Thank You!

Coordinator
Sep 20, 2012 at 6:40 PM

You're looking for the file spreadsheet.xls in the current working directory of your script (which can be ascertained using the getcwd() function in PHP): either the file is not at that location, or your PHP execution doesn't have permissions to access the file.

Sep 20, 2012 at 8:12 PM

Thanks Mark!! You are awesome!!