PhpSpreadsheet 通知:未定义的偏移量:4

PhpSpreadsheet Notice: Undefined offset: 4

我一直在努力寻找一种方法来读取目录中的 excel 文件,将名为 "Division" 的工作表复制到一个新文件中,然后将新工作表编号递增一个 - Division1, Division2 等...

以下是我的代码,我收到了未定义索引的警告,这没什么大不了的,但它只创建一个只有四个工作表的新 excel 文件,当应该结束时240 张工作表。

        ini_set('display_errors', 1);
        ini_set('display_startup_errors', 1);
        error_reporting(E_ALL);

        use PhpOffice\PhpSpreadsheet\Helper\Sample;
        use PhpOffice\PhpSpreadsheet\IOFactory;

        $exportedFiles = scandir('PhpSpreadsheet/Export');
        $inputFileNames = [];
        $sheetnames = [];
        $outfile = 'all-together-now.xlsx';

        foreach($exportedFiles as $key=> $value) {
            if ($value == '.' || $value == '..') {

            } else {
                array_push($inputFileNames, 'PhpSpreadsheet/Export/' . $value);
                array_push($sheetnames, 'Division');
            }
        }

        $inputFileType = 'Xlsx';
        $reader = IOFactory::createReader($inputFileType);
        $reader->setLoadSheetsOnly($sheetnames);
        $contador = 1;

        foreach ($inputFileNames as $book => $inputFileName) {
            echo ('$inputFileName: ' . $inputFileName) . '</br>'; 

            $reader = IOFactory::createReader("Xlsx");
            $spreadsheet = $reader->load($inputFileName);
            $clonedWorksheet = clone $spreadsheet->getSheetByName('Division');
            $clonedWorksheet->setTitle('Division' . $contador);
            $spreadsheetMain = $reader->load($outfile);
            $spreadsheetMain->addSheet($clonedWorksheet);
            $writer = IOFactory::createWriter($spreadsheetMain, "Xlsx");
            $writer->save($outfile);
            $contador++;
        }

警告信息如下:

        Notice: Undefined offset: 4 in
        PhpOffice\PhpSpreadsheet\Writer\Xlsx->save( )
        PhpOffice\PhpSpreadsheet\Spreadsheet->garbageCollect( )

像往常一样,提前致谢

部分解决方案是增加内存,另一个是使用 addSheet()

以下是有效的代码:

ini_set('memory_limit','32768M');

use PhpOffice\PhpSpreadsheet\IOFactory;

require_once 'PhpSpreadsheet/src/Bootstrap.php';

$exportedFiles = scandir('PhpSpreadsheet/Export');

$inputFileNames = [];

$sheetnames = [];

$outfile = 'all-together-now.xlsx';

foreach($exportedFiles as $key=> $value) {
    if ($value == '.' || $value == '..') {

    } else {
        array_push($inputFileNames, 'PhpSpreadsheet/Export/' . $value);
        array_push($sheetnames, 'Division');
    }
}

$inputFileType = 'Xlsx';
$reader = IOFactory::createReader($inputFileType);
$reader->setLoadSheetsOnly($sheetnames);
$contador = 1;

foreach ($inputFileNames as $book => $inputFileName) {
    echo ('$inputFileName: ' . $inputFileName) . '</br>';
    $reader = IOFactory::createReader("Xlsx");
    $spreadsheet = $reader->load($inputFileName);
    $spreadsheet->getSheetByName('Division')->getStyle('Division');
    $clonedWorksheet = clone $spreadsheet->getSheetByName('Division');
    $clonedWorksheet->setTitle('Division' . $contador);

    /* open new file for writing spread sheets to it */
    $spreadsheetMain = $reader->load($outfile);
    $spreadsheetMain->addSheet($clonedWorksheet);
    $writer = IOFactory::createWriter($spreadsheetMain, "Xlsx");
    $writer->save($outfile);
    $contador++;
}
$spreadsheet->disconnectWorksheets();
echo "The process has completed";
die();

上述脚本创建了一个 excel 工作簿,其中包含 247 个 excel 文件中的 247 个工作表