Phpspreadsheet : Uncaught InvalidArgumentException File "filename.xlsx" does not exist

474 views Asked by At

I am working on a project using PHPspreadsheet, it is a filter for an excel file.

I am facing a problem which is File does not exist, but I am sure that I am putting it correctly.

it was working but after some days I reviewed my code, and I am getting surprised with this error.

Can anyone tell me where is the problem please?

Here is my code :

    <?php

//include the file that loads the PhpSpreadsheet classes
require 'spreadsheet/vendor/autoload.php';

        
        
use PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column;
use PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule;


$inputFileType = 'Xlsx';
$inputFileName = "phpspreadsheet/répartition.xlsx";

/**  Create a new Reader of the type defined in $inputFileType  **/
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
/**  Advise the Reader that we only want to load cell data  **/
$reader->setReadDataOnly(true);
/**  Load $inputFileName to a Spreadsheet Object  **/
$spreadsheet = $reader->load($inputFileName);


//read excel data and store it into an array
$spreadsheet->getActiveSheet()->setAutoFilter(
    $spreadsheet->getActiveSheet()
        ->calculateWorksheetDimension()
);



                if(isset($_POST['select1'])){
                    $select1 = $_POST['select1'];
                    switch ($select1) {
                        case '1':
                            $autoFilter = $spreadsheet->getActiveSheet()->getAutoFilter();
                            $columnFilter = $autoFilter->getColumn('G');
                            $columnFilter->setFilterType(
                                \PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column::AUTOFILTER_FILTERTYPE_FILTER
                            );                          

                            $columnFilter->createRule()
                            ->setRule(
                                \PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_COLUMN_RULE_EQUAL, 'PASSIF'
                            );
                            break;
                        case '2':
                            $autoFilter = $spreadsheet->getActiveSheet()->getAutoFilter();
                            $columnFilter = $autoFilter->getColumn('G');
                            $columnFilter->setFilterType(
                                \PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column::AUTOFILTER_FILTERTYPE_FILTER
                            );                          

                            $columnFilter->createRule()
                            ->setRule(
                                \PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_COLUMN_RULE_EQUAL, 'ENCOURS'
                            );
                            break;
                        default:
                            # code...
                            break;
                    }
        }
        if(isset($_POST['select2'])){
            $select2 = $_POST['select2'];
            switch ($select2) {
                case '1':
                    $autoFilter = $spreadsheet->getActiveSheet()->getAutoFilter();
                    $columnFilter = $autoFilter->getColumn('L');
                    $columnFilter->setFilterType(
                            \PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column::AUTOFILTER_FILTERTYPE_FILTER
                    );                          

                    $columnFilter->createRule()
                        ->setRule(
                            \PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_COLUMN_RULE_EQUAL, 'ENREGISTRE'
                    );
                    break;
                case '2':
                    $autoFilter = $spreadsheet->getActiveSheet()->getAutoFilter();
                    $columnFilter = $autoFilter->getColumn('L');
                    $columnFilter->setFilterType(
                            \PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column::AUTOFILTER_FILTERTYPE_FILTER
                    );                          

                    $columnFilter->createRule()
                        ->setRule(
                            \PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_COLUMN_RULE_EQUAL, 'A_REENREGISTRER'
                    );
                    
                    break;

                case '3':
                    $autoFilter = $spreadsheet->getActiveSheet()->getAutoFilter();
                    $columnFilter = $autoFilter->getColumn('L');
                    $columnFilter->setFilterType(
                            \PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column::AUTOFILTER_FILTERTYPE_FILTER
                    );                          

                    $columnFilter->createRule()
                        ->setRule(
                            \PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_COLUMN_RULE_EQUAL, 'Non crée'
                    );
                    
                    
                    break;

                case '4':
                    $autoFilter = $spreadsheet->getActiveSheet()->getAutoFilter();
                    $columnFilter = $autoFilter->getColumn('L');
                    $columnFilter->setFilterType(
                            \PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column::AUTOFILTER_FILTERTYPE_FILTER
                    );                          

                    $columnFilter->createRule()
                        ->setRule(
                            \PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_COLUMN_RULE_EQUAL, 'SAUVEGARDE'
                    );;
                    break;
                default:
                    # code...
                    break;
            }
        }
        if(isset($_POST['select3'])){
            $select3 = $_POST['select3'];
            switch ($select3) {
                case '1':
                    $autoFilter = $spreadsheet->getActiveSheet()->getAutoFilter();
                    $columnFilter = $autoFilter->getColumn('M');
                    $columnFilter->setFilterType(
                            \PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column::AUTOFILTER_FILTERTYPE_FILTER
                    );                          

                    $columnFilter->createRule()
                        ->setRule(
                            \PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_COLUMN_RULE_EQUAL, 'Directeur  '
                    );
                    break;
                case '2':
                    $autoFilter = $spreadsheet->getActiveSheet()->getAutoFilter();
                    $columnFilter = $autoFilter->getColumn('M');
                    $columnFilter->setFilterType(
                            \PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column::AUTOFILTER_FILTERTYPE_FILTER
                    );                          

                    $columnFilter->createRule()
                        ->setRule(
                            \PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_COLUMN_RULE_EQUAL, 'Directeur Régional'
                    );
                    break;
                default:
                    # code...
                    break;
            }
        }
        if(isset($_POST['select4'])){
            $select4 = $_POST['select4'];
            switch ($select4) {
                case '1':
                    $autoFilter = $spreadsheet->getActiveSheet()->getAutoFilter();
                    $columnFilter = $autoFilter->getColumn('N');
                    $columnFilter->setFilterType(
                            \PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column::AUTOFILTER_FILTERTYPE_FILTER
                    );                          

                    $columnFilter->createRule()
                        ->setRule(
                            \PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_COLUMN_RULE_EQUAL, 'le Directeur'
                    );
                    break;
                case '2':
                    $autoFilter = $spreadsheet->getActiveSheet()->getAutoFilter();
                    $columnFilter = $autoFilter->getColumn('N');
                    $columnFilter->setFilterType(
                            \PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column::AUTOFILTER_FILTERTYPE_FILTER
                    );                          

                    $columnFilter->createRule()
                        ->setRule(
                            \PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_COLUMN_RULE_EQUAL, "l'Ordonnateur"
                    );
                    break;
                default:
                    # code...
                break;
            }
        }
            if(isset($_POST['select5'])){
                $select5 = $_POST['select5'];
                switch ($select5) {
                    case '1':
                        echo 'this is value1<br/>';
                        break;
                    case '2':
                        echo 'this isvalue2<br/>';
                        break;
                    default:
                        # code...
                        break;
                }
        }
                if(isset($_POST['select6'])){
                    $select6 = $_POST['select6'];
                    switch ($select6) {
                        case '1':
                            $autoFilter = $spreadsheet->getActiveSheet()->getAutoFilter();
                            $columnFilter = $autoFilter->getColumn('K');
                            $columnFilter->setFilterType(
                                    \PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column::AUTOFILTER_FILTERTYPE_FILTER
                            );                          

                            $columnFilter->createRule()
                                ->setRule(
                                    \PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_COLUMN_RULE_EQUAL, 'avec etat'
                            );
                            break;
                        case '2':
                            $autoFilter = $spreadsheet->getActiveSheet()->getAutoFilter();
                            $columnFilter = $autoFilter->getColumn('K');
                            $columnFilter->setFilterType(
                                    \PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column::AUTOFILTER_FILTERTYPE_FILTER
                            );                          

                            $columnFilter->createRule()
                                ->setRule(
                                    \PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_COLUMN_RULE_EQUAL, 'sans etat'
                            );
                            break;
                        default:
                            # code...
                            break;
                    }
        }
        


// ---------------------------------------------------------------------
// ----------This code is to display table from Excel Sheet-------------
// ---------------------------------------------------------------------

$autoFilter = $spreadsheet->getActiveSheet()->getAutoFilter();
$autoFilter->showHideRows();


$worksheet = $spreadsheet->getActiveSheet();


echo '<div class="card">';
echo '<div class="card-header text-center">Répartition 07-08</div>';
echo '<div class="card-body table-responsive">';
echo '<div class="tableFixHead">';
echo '<table class="table table-sm table-striped table-bordered table-hover">' . PHP_EOL;

foreach ($spreadsheet->getActiveSheet()->getRowIterator() as $row) {
    if ($spreadsheet->getActiveSheet()
        ->getRowDimension($row->getRowIndex())->getVisible()) {
        echo '<tr>' . PHP_EOL;    
        echo '<td>'.$spreadsheet->getActiveSheet()
            ->getCell(
                'A'.$row->getRowIndex()
            )
            ->getValue().'</td>' . PHP_EOL;

        echo '<td>'.$spreadsheet->getActiveSheet()
            ->getCell(
                'B'.$row->getRowIndex()
            )
            ->getValue().'</td>' . PHP_EOL;

        echo '<td>'.$spreadsheet->getActiveSheet()
            ->getCell(
                'C'.$row->getRowIndex()
            )
            ->getValue().'</td>' . PHP_EOL;

        echo '<td>'.$spreadsheet->getActiveSheet()
            ->getCell(
                'D'.$row->getRowIndex()
            )
            ->getValue().'</td>' . PHP_EOL;

        echo '<td>'.$spreadsheet->getActiveSheet()
            ->getCell(
                'E'.$row->getRowIndex()
            )
            ->getValue().'</td>' . PHP_EOL;
           
        echo  '<td>'.$spreadsheet->getActiveSheet()
            ->getCell(
                'F'.$row->getRowIndex()
            )
            ->getValue().'</td>' . PHP_EOL;

        echo  '<td>'.$spreadsheet->getActiveSheet()
            ->getCell(
                'G'.$row->getRowIndex()
            )
            ->getValue().'</td>' . PHP_EOL;
        
        echo  '<td>'.$spreadsheet->getActiveSheet()
            ->getCell(
                'H'.$row->getRowIndex()
            )
            ->getValue().'</td>' . PHP_EOL;

        echo  '<td>'.$spreadsheet->getActiveSheet()
            ->getCell(
                'I'.$row->getRowIndex()
            )
            ->getValue().'</td>' . PHP_EOL;
        
        echo  '<td>'.$spreadsheet->getActiveSheet()
            ->getCell(
                'J'.$row->getRowIndex()
            )
            ->getValue().'</td>' . PHP_EOL;

        echo  '<td>'.$spreadsheet->getActiveSheet()
            ->getCell(
                'K'.$row->getRowIndex()
            )
            ->getValue().'</td>' . PHP_EOL; 
            
        echo  '<td>'.$spreadsheet->getActiveSheet()
            ->getCell(
                'L'.$row->getRowIndex()
            )
            ->getValue().'</td>' . PHP_EOL; 

        echo  '<td>'.$spreadsheet->getActiveSheet()
            ->getCell(
                'M'.$row->getRowIndex()
            )
            ->getValue().'</td>' . PHP_EOL; 

        echo  '<td>'.$spreadsheet->getActiveSheet()
            ->getCell(
                'N'.$row->getRowIndex()
            )
            ->getValue().'</td>' . PHP_EOL; 

        echo  '<td>'.$spreadsheet->getActiveSheet()
            ->getCell(
                'O'.$row->getRowIndex()
            )
            ->getValue().'</td>' . PHP_EOL; 
        echo '</tr>' . PHP_EOL;

            } 
        }
echo '</table></div></div></div>' . PHP_EOL;






?>
0

There are 0 answers