0

I develop an application with symfony 4 and I need to read data from an excel file. I integrate PhpOffice \ PhpSpreadsheet in my project but I can not find documentation to use it for now. I find this site to start but it does not do everything I want. https://phpspreadsheet.readthedocs.io/en/latest/topics/reading-and-writing-to-file/

If someone would have another track or know how to read data from an excel file using PhpSpreadsheet.

public function excelReader(){

    $reader = new Xlsx();
    $reader->setReadDataOnly(TRUE);
    $spreadsheet = $reader->load("test.xlsx");
    $worksheet = $spreadsheet->getActiveSheet();

    $highestRow = $worksheet->getHighestRow(); 
    $highestColumn = $worksheet->getHighestColumn(); 
    $highestColumnIndex = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($highestColumn);
$res = array();
    for($row=1; $row < $highestRow ; $row++){
        for($col = 1; $col <= $highestColumnIndex; $col++){
            $value = $worksheet->getCellByColumnAndRow($col,$row)->getValue();
            array_push($res,$value);
        }
    }


    return $this->render('table/excel.html.twig', [
        'list' => $res,
    ]);

 }

in this code I try to load and then read the test.xlsx file that I put in the controller folder. I get the following error: File "test.xlsx" does not exist.

3
  • Please show us what you have done, you need to share some code in order to help Commented May 31, 2019 at 15:07
  • Those are the official PHPSpreadsheet documents, if they can't help you I doubt we can. Have you tried anything as of yet? Without some idea of what you're trying to achieve we won't be able to help. Commented May 31, 2019 at 15:18
  • You really need to provide more info. "it does not do everything I want" does not tell us anything. What do you want it to do? What have you tried to make it work? Commented May 31, 2019 at 20:19

1 Answer 1

1

Try creating the folder {symfony folder}/public/uploads/ and put test.xlsx in that folder. Then change this line in your code: $spreadsheet = $reader->load("uploads/test.xlsx"); (The folder {symfony folder}/src/Controller/ is for code only for security reasons.)

There are examples of reading spreadsheets under {symfony directory}/vendor/phpoffice/phpspreadsheet/samples/Reader/

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much. I am a beginner with symfony and I was not aware of this security feature

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.