如何使用symfony2读取Excel文件?

如何使用symfony2读取Excel文件?,symfony,phpexcel,Symfony,Phpexcel,我用PhpExcel库安装了Excelbundle。我想读取我找到此函数的excel文件 我如何使用它?有什么建议吗 public function xlsAction() { $filenames = "your-file-name"; $phpExcelObject = $this->get('phpexcel')->createPHPExcelObject($filenames); foreach ($phpExcelObject ->getWo

我用PhpExcel库安装了Excelbundle。我想读取我找到此函数的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();
                    }
                }
        }
    }
}

我的建议是阅读文档并开始破解它。根据我的经验,使用excel非常复杂和耗时,所以不要指望其他人在线解决您的问题

看起来你在谈论这个捆绑包:


它有很好的文档,甚至完整的示例都可以看到Fake Controller。

使用PHPExcel,阅读Excel文档非常容易

参见我的示例:

$dir       = $this->getContainer()->getParameter("kernel.root_dir") . "/../../data/import/";
$file_name = "my_excel_file.xlsx";
$excel = $this->getContainer()->get('phpexcel')->createPHPExcelObject($dir . DIRECTORY_SEPARATOR . $file_name);
$sheet = $excel->getActiveSheet();

$row = 0;
 while ($sheet->getCellByColumnAndRow(1, $row)->getValue()) {

    $data = $sheet->getCellByColumnAndRow(2, $row)->getValue(); // get value from nth line and 2nf column

    //do stuff -- see doc for functions on $sheet

 }

请准确回答你的问题。你有什么问题?我不知道如何使用这个包读取excel文件