简体   繁体   English

PhpSpreadsheet 读取 php 中的多个文件

[英]PhpSpreadsheet reading through multiple files in php

Hello I am trying to build multiple xls files using PhpSpreadsheet library in a foreach loop but instead of reading data it shows the file names only.If I use the same code for a single file it works.您好,我正在尝试在foreach循环中使用 PhpSpreadsheet 库构建多个xls文件,但它没有读取数据,而是仅显示文件名。如果我对单个文件使用相同的代码,它就可以工作。 I am not sure what I am doing wrong.我不确定我做错了什么。

require_once __DIR__.'/vendor/autoload.php';
require_once __DIR__."/export.php";

        use PhpOffice\PhpSpreadsheet\Helper\Sample;
        use PhpOffice\PhpSpreadsheet\IOFactory;
        use PhpOffice\PhpSpreadsheet\Spreadsheet;
        use PhpOffice\PhpSpreadsheet\Writer\Xls;

define('ROOT_PATH', dirname(__DIR__));
define('INSTALL_FOLDER', '/combine'); //Script installation directory if any leave '' if in root
define('DAT_FILES_FOLDER','/CompactedData'); // .dat files folder where generated
define('MAIN_PATH', ROOT_PATH.INSTALL_FOLDER);

function checkIsAValidDate($myDateString){
    return (bool)strtotime($myDateString);
}

$datFilesPath = MAIN_PATH.DAT_FILES_FOLDER;
$getAllFiles = glob($datFilesPath . "/*.xls");
$content = null;
$content_data = null;
$rowKey = null;
$file_name = null;


foreach ($getAllFiles as $fileIndex => $getfileName):
    $rows = null;
    $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load( $getfileName );
    $worksheet = $spreadsheet->getActiveSheet();
    $rows = [];
    foreach ($worksheet->getRowIterator() AS $row) {
        $cellIterator = $row->getCellIterator();
        $cellIterator->setIterateOnlyExistingCells(FALSE); // This loops through all cells,
        $cells = [];
        foreach ($cellIterator as $cell) {
            $cells[] = $cell->getValue();
        }
        $rows[] = $cells;
    }

$filesDataComplete = array(
    "filename" => $getfileName,
    "fileData" => $rows
);
endforeach;

echo "<pre>";
print_r($filesDataComplete);
echo "</pre>";

You need to disconnect from the worksheet and also unset the variable to make it work like this您需要断开与工作表的连接并取消设置变量以使其像这样工作

$spreadsheet->disconnectWorksheets();
unset($spreadsheet);

In your case you code will look like this在您的情况下,您的代码将如下所示

foreach ($getAllFiles as $fileIndex => $getfileName):
    $rows = null;
    $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load( $getfileName );
    $worksheet = $spreadsheet->getActiveSheet();
    $rows = [];
    foreach ($worksheet->getRowIterator() AS $row) {
        $cellIterator = $row->getCellIterator();
        $cellIterator->setIterateOnlyExistingCells(FALSE); // This loops through all cells,
        $cells = [];
        foreach ($cellIterator as $cell) {
            $cells[] = $cell->getValue();
        }
        $rows[] = $cells;
    }

    $spreadsheet->disconnectWorksheets();
    unset($spreadsheet);

$filesDataComplete = array(
    "filename" => $getfileName,
    "fileData" => $rows
);
endforeach;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM