使用symfony 4中的PhpOffice \ PhpSpreadsheet从excel文件中读取数据

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.

php
drxvjnx58751
drxvjnx58751 你真的需要提供更多信息。“它没有做我想做的一切”并没有告诉我们任何事情。你想要它做什么?你试图让它起作用的是什么?
一年多之前 回复
doulu1968
doulu1968 那些是官方的PHPSpreadsheet文件,如果他们无法帮助你我怀疑我们可以。你有没有尝试过任何东西?如果不了解你想要实现的目标,我们将无法提供帮助。
一年多之前 回复
dongmaomou4117
dongmaomou4117 请告诉我们你做了什么,你需要分享一些代码才能提供帮助
一年多之前 回复

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/

dongpeng8994
dongpeng8994 非常感谢。 我是symfony的初学者,我不知道这个安全功能
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐