3

For a school project, I have to collect data from an Excel file uploaded by the user. I am using Symfony2 and have installed a bundle I found on knpbundles, named ExcelBundle. I read that to collect data with it from an Excel file, I should use the createWriter method of my phpExcel object. That is what I have done as shown below.

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');

}

But the thing is that actually, I do not really know how to use the writer to collect data from the cells of my excel datasheets.

Please, could anyone give me the trick to achieve my goal ?

1 Answer 1

7

You can iterate as this Example:

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

More samples here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.