普通网友 2019-05-31 14:59
浏览 184
已采纳

使用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.

  • 写回答

1条回答 默认 最新

  • dongxin0031 2019-05-31 23:22
    关注

    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/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵