如何读取Excel文件并提取数组中列的值

时间:2019-06-17 13:08:39

标签: php excel symfony

我想读取一个excel文件并返回一个包含A列值的数组,我正在处理一个现有项目,他们使用LiuggioExcelBundle

我发现它们是一个从excel文件读取的函数,但是我不知道如何在A列上进行迭代并将值放入数组中并返回它。

filename = $this->container->getParameter('xls_fixture_absolute_path');
    // load the factory
    /** @var \Liuggio\ExcelBundle\Factory $reader */
    $factory = $this->get('phpexcel');
    // create a reader
    /** @var \PHPExcel_Reader_IReader $reader */
    $reader = $factory->createReader('Excel5');
    // check that the file can be read
    $canread = $reader->canRead($filename);
    // check that an empty temporary file cannot be read
    $someFile = tempnam($this->getParameter('kernel.root_dir'), "tmp");
    $cannotread = $reader->canRead($someFile);
    unlink($someFile);
    // load the excel file
    $phpExcelObject = $reader->load($filename);
    // read some data
    $sheet = $phpExcelObject->getActiveSheet();
    $hello = $sheet->getCell('A1')->getValue();// instead of a value of 
    $world = $sheet->getCell('B2')->getValue();// A1 how to extract all 
                                               // A vlaues   

    return new Response($canread && !$cannotread ? "$hello $world" : 'I   should no be able to read this.');

1 个答案:

答案 0 :(得分:0)

您可以尝试这样(我没有测试过):

JSONObject json = new JSONObject(jsonString);
Object value = interateNestedJson(jsonObject, "key5");