How can I read Excel files with symfony2?

偶尔善良 提交于 2019-12-14 03:36:44

问题


I installed Excelbundle with PhpExcel library. I want to read excel files I found this function.

How can I use it? Any suggestion?

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();
                    }
                }
        }
    }
}

回答1:


My suggestion is "Read the documentation" and start hacking at it. Working with excel is, in my experience, quite complex and time consuming so don't expect other people to solve your problem online.

It seems like you're talking about this bundle: https://github.com/liuggio/ExcelBundle

It has great documentation, even full examples (see "Fake Controller").




回答2:


With PHPExcel it is quite easy to read a Excel document.

See my example :

$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

 }


来源:https://stackoverflow.com/questions/35985505/how-can-i-read-excel-files-with-symfony2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!