RikyTres
RikyTres

Reputation: 676

Render XSL file to HTML using PHP

I need to read a excel (xls) file and render it to a HTML page.

I'm developing in PHP, using Symfony 2.6.5 and I want to have access to the excel cel data, in this way I can generate HTML in a Symfony Controller(or better in a a Template).

I'm using ExcelBundle and I've read the documentation, but I found only ways to set information and not to get.

Which is the best we to read the content of the excel?

PS: the excel is a price list in a tabular form like the follow:

+––––+––––––+–––––––––––––+–––––––+
| id | name | description | price |
|____|______|_____________|_______|
+––––+––––––+–––––––––––––+–––––––+
| 1  | foo  | foo desc    | 12€   |
+––––+––––––+–––––––––––––+–––––––+
| 2  | bar  | abr desc    | 65€   |
+––––+––––––+–––––––––––––+–––––––+
|... |...   | ...         | ...   |

Upvotes: 2

Views: 239

Answers (1)

Matteo
Matteo

Reputation: 39390

In according with the code source, simply pass the filename in the createPHPExcelObject method. As 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();
                }
            }
    }
    }
}

Simply pass the array to your view for rendering in the template.

Hope this help

Upvotes: 2

Related Questions