如何使用 symfony2 excelbundle 从 excel 文件中检索数据?

How to retrieve data from excel file using symfony2 excelbundle?

对于学校项目,我必须从用户上传的 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 数据表的单元格中收集数据。

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

您可以像这个例子一样迭代:

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