Read Excel file with PHP

1

Good, I need to create a form where users can upload an xlsx file, and the web app validate the file then read it, and finally I need to put the information after evaluating it in a database, I'm using a PHPExcel library , the problem is that it gives me an error on the page, When I place the xlsx file that will read, it marks me error on the web, (This page does not work) HTTP ERROR 500. Debugeando, it is not a route error since I was placing routes one by one, and put the name of the file wrong to test and gave me the catch, error that the file does not exist, but when I place the route correctly, PAM gives me error.

require_once('../phpexcel/Classes/PHPExcel.php');
require_once('../phpexcel/Classes/PHPExcel/Reader/Excel2007.php');
require_once '../phpexcel/Classes/PHPExcel/IOFactory.php'; 
$inputFileName = '../phpexcel/Classes/PHPExcel/jd.xlsx';
try {
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load($inputFileName);
$objWorksheet = $objPHPExcel->getActiveSheet();
echo '<table>' . "\n";
foreach ($objWorksheet->getRowIterator() as $row) {
echo '<tr>' . "\n";
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false); // This loops all cells,
foreach ($cellIterator as $cell) {
echo '<td>' . $cell->getValue() . '</td>' . "\n";
}
 
echo '</tr>' . "\n";
}
echo '</table>' . "\n";
} catch(Exception $e) {
	die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
}


echo '<hr />';
echo "<pre>";

Result : Failed to load resource: the server responded with a status of 500 (Internal Server Error)
    
asked by Carlos Estarita 30.03.2017 в 21:43
source

2 answers

1

Verify the path of the library, and also mention that it is not necessary to add all those include that you have in your code, just include IOFactory.php .

require_once 'php/ext/PHPExcel-1.7.7/Classes/PHPExcel/IOFactory.php';
//cargamos el archivo que deseamos leer
$objPHPExcel = PHPExcel_IOFactory::load($nombreArchivo);
//obtenemos los datos de la hoja activa (la primera)
$objHoja=$objPHPExcel->getActiveSheet()->toArray(null,true,true,true);

foreach ($objHoja as $iIndice=>$objCelda) 
{
  echo $objCelda['B'];
}
    
answered by 30.03.2017 в 22:14
0

I suggest you check your error log file, since your example works well in a protoype that I created in my localhost. (If you can not find the location of your error log, tell me your Operating System and I will give you instructions on how to find it).

Also remember that to use PHPExcel, you need to have PHP version 5.2.0 or higher and have the following extensions configured:

  • php_zip (to handle .xlsx .ods or .gnumeric)
  • php_xml
  • php_gd2 (optional, but required to autocalculate the width of the columns).

You can verify this with phpinfo

Greetings.

    
answered by 30.03.2017 в 22:47