【问题标题】:How to retrieve data from excel file using symfony2 excelbundle?如何使用 symfony2 excelbundle 从 excel 文件中检索数据?
【发布时间】:2015-06-04 12:48:13
【问题描述】:

对于一个学校项目,我必须从用户上传的 Excel 文件中收集数据。我正在使用 Symfony2 并安装了我在 knpbundles 上找到的一个名为 ExcelBundle 的包。我读到它是为了从 Excel 文件中收集数据,我应该使用我的 phpExcel 对象的 createWriter 方法。这就是我所做的,如下所示。

public function addContactsFromExcelAction(Request $request) {
    $uploadDir = '/var/www'.$request->getBasePath().'/uploads/';
    //die(var_dump($uploadDir));
    $file = $request->files->get('fichierExcel');
    $fileName = $file->getClientOriginalName();
    $fileSaved = $file->move($uploadDir,$fileName);
    $phpExcelObject = $this->get('phpexcel')->createPHPExcelObject($uploadDir.$fileName);
    $writer = $this->get('phpexcel')->createWriter($phpExcelObject, 'Excel2007');

}

但实际上,我真的不知道如何使用编写器从我的 excel 数据表的单元格中收集数据。

拜托,谁能给我实现目标的诀窍?

【问题讨论】:

    标签: php symfony bundle phpexcel


    【解决方案1】:

    你可以像这个例子那样迭代:

    public function xlsAction()
    {
        $filenames = "your-file-name";
        $phpExcelObject = $this->get('phpexcel')->createPHPExcelObject($filenames);
    
        foreach ($phpExcelObject ->getWorksheetIterator() as $worksheet) {
            echo 'Worksheet - ' , $worksheet->getTitle();
            foreach ($worksheet->getRowIterator() as $row) {
                echo '    Row number - ' , $row->getRowIndex();
                $cellIterator = $row->getCellIterator();
                $cellIterator->setIterateOnlyExistingCells(false); // Loop all cells, even if it is not set
                foreach ($cellIterator as $cell) {
                    if (!is_null($cell)) {
                        echo '        Cell - ' , $cell->getCoordinate() , ' - ' , $cell->getCalculatedValue();
                    }
                }
        }
        }
    }
    

    更多示例here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-01
      • 2021-03-16
      • 1970-01-01
      • 2020-08-30
      • 1970-01-01
      • 2016-01-30
      • 1970-01-01
      相关资源
      最近更新 更多