如何用phpexcel包读取xls文件?在Symfony2中

时间:2012-02-24 19:10:06

标签: symfony

包phpExcel提供了写xls文件的方式,但我不知道如何用这个包读取文件xls .. 可以在以下地址找到该包:http://knpbundles.com/liuggio/ExcelBundle

感谢您的帮助

4 个答案:

答案 0 :(得分:5)

如果您真的想阅读EXCEL文件,可以在官方文档中找到一个示例: https://github.com/PHPOffice/PHPExcel/blob/develop/Examples/28iterator.php

代码:

$phpExcelObject = $this->get('phpexcel')->createPHPExcelObject();
    $file = $this->get('kernel')->getRootDir()."/../web/uploads/import/membres.xlsx";
    if (!file_exists($file)) {
        exit("Please run 05featuredemo.php first." );
    }
    $objPHPExcel = \PHPExcel_IOFactory::load($file);

    echo date('H:i:s') , " Iterate worksheets" , EOL;
    foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
            echo 'Worksheet - ' , $worksheet->getTitle() , EOL;

            foreach ($worksheet->getRowIterator() as $row) {
                    echo '    Row number - ' , $row->getRowIndex() , EOL;

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


    // Echo memory peak usage
    echo date('H:i:s') , " Peak memory usage: " , (memory_get_peak_usage(true) / 1024 / 1024) , " MB" , EOL;

答案 1 :(得分:1)

ExcelBundle只是围绕PHPExcel类的包装服务。

捆绑服务将返回PHPExcel_IOFactory,其中包含输入和输出中预期的特定文件格式(在本例中为xls5)

 $xl_obj=$this->get('xls.load_xls5')

您可以将其用作PHPExcel项,以便读取文件:

  $my_xl_file = $xl_obj->load($fileName);

如果你转到github page of ExcelBundle,那么你正在寻找的例子就是:

  

如果你想阅读xls

     
    

$ exelObj = $ this-> get('xls.load_xls5') - > load($ filename);

  

答案 2 :(得分:1)

读取文件就像创建文件

这将从xls文件开始创建一个对象:

$phpExcelObject = $this->get('phpexcel')->createPHPExcelObject('origin.xls');
// work on the object adding contents
// then write to a file
$writer = $this->get('phpexcel')->createWriter($phpExcelObject, 'Excel5');
$writer->save('dest.xls');

答案 3 :(得分:0)

看起来它只是使用 PHPExcel 库。您可以在其网站上找到示例和文档。

相关问题