【问题标题】:How can I read Excel files with symfony2?如何使用 symfony2 读取 Excel 文件?
【发布时间】:2016-03-14 10:49:48
【问题描述】:

我安装了ExcelbundlePhpExcel 库。我想读取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();
                    }
                }
        }
    }
}

【问题讨论】:

  • 请准确回答您的问题。你有什么问题?
  • 不知道怎么用bundle读取excel文件

标签: symfony phpexcel


【解决方案1】:

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

您似乎在谈论这个捆绑包: https://github.com/liuggio/ExcelBundle

它有很好的文档,甚至是完整的例子(参见“假控制器”)。

【讨论】:

    【解决方案2】:

    使用 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
    
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多