PHPExcel xlsx file into array ....Reader_Exception: Could not open

290 Views Asked by At

1.8.0"

the goal of my web application is to have a button that the user clicks on.

The code behind will start to scan all the folders that are in the main rdv folder.

in the rdv folder it contains three folders named 1000, 1001, 1002 these contain .xlsx files.

retrieves the last file .xlsx saved in each of the folders

but after that it doesn't work.

Fatal error: Uncaught PHPExcel_Reader_Exception: Could not open 2.xlsx for reading!

so how i can fix the fatal error and how is possible to read the file to continue the code, i want to get 3 values in the .xlsx file and insert into my database.

thank you for your time.

include 'database.php';
require 'PHPExcel/Classes/PHPExcel.php';
require_once 'PHPExcel/Classes/PHPExcel/IOFactory.php';
 
// receives the 'someAction' value from the index page button.
if($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['someAction']))
{
    func();
}

function func()
{
    //calcul the nomber of folder in folder rdv
    $howManyFolder = count(glob('rdv/*', GLOB_ONLYDIR));
    
    //retrieves the last file saved in each of the folders and insert into database
    for ($i = 0; $i < $howManyFolder; $i++) {
        
        $files = scandir("rdv/100$i", SCANDIR_SORT_DESCENDING);
        echo "Recover all files in the folder 100" . $i . "<br>";
        print_r($files ); echo "<br>";
        echo "get the last file inserted in the folder at index 0 <br>";
        $newest_file = $files[0]; print_r($newest_file); echo "<br>";
        
        //load the file .xlsx (but the code bug here -_-)
        $objExcel=PHPExcel_IOFactory::load($newest_file);

        //get all value insinde the .xlsx file
        $dossier = $objExcel->getActiveSheet()->getCell('A3')->getValue();
        $facture = $objExcel->getActiveSheet()->getCell('B21')->getValue();
        $date = $objExcel->getActiveSheet()->getCell('Q1')->getValue();

        //call function to insert these values inside dataBase.
        insertInto($dossier, $facture, $date );
    }
}       
1

There are 1 best solutions below

0
On

XML Parser: https://www.php.net/manual/en/book.xml.php

Parsing an XML document: https://www.php.net/manual/en/function.xml-parse.php

<?PHP
$stream = fopen('large.xml', 'r');
$parser = xml_parser_create();
// set up the handlers here
while (($data = fread($stream, 16384))) {
    xml_parse($parser, $data); // parse the current chunk
}
xml_parse($parser, '', true); // finalize parsing
xml_parser_free($parser);
fclose($stream);