在 excel 中与 phpSpreadSheet 合并

问题描述 投票:0回答:3

我想将 2 个或更多 excel 与 PhpSpreadsheet 合并。我试图保留每张纸的样式,这是我的代码:

<?php

    require 'vendor/autoload.php';


    use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

    $inputFileType = 'Xlsx';
    $inputFileNames = [
        'a.xlsx',
        'b.xlsx',
        'c.xlsx'
    ];
    $sheetnames = [
        'Worksheet',
        'Worksheet1',
        'Worksheet2'
        ];
    $reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
    $reader->setLoadSheetsOnly($sheetnames);
    $inputFileName = array_shift($inputFileNames);
    $spreadsheetMain = $reader->load($inputFileName);
    $spreadsheetMain->getActiveSheet()->setTitle('page0');
    $contador = 1;
    foreach ($inputFileNames as $book => $inputFileName) {
        echo ('$inputFileName: ' . $inputFileName) . '</br>';
        $spreadsheet = $reader->load($inputFileName);
        $clonedWorksheet = clone $spreadsheet->getActiveSheet()->setTitle($inputFileName[0]);
        $clonedWorksheet->setTitle('page' . $contador);
        $spreadsheetMain->addSheet($clonedWorksheet);
        $contador++;
    }
    $writer = new Xlsx($spreadsheetMain);
    $writer->save('prueba1.xlsx');

为了做我想做的事,我需要改变这一点

$spreadsheetMain->addSheet($clonedWorksheet);

为此

$spreadsheetMain->addExternalSheet($clonedWorksheet);

但是当我执行该代码时,我收到错误:“工作表不存在”

我认为它尚未实现,但在文档页面中:https://phpspreadsheet.readthedocs.io/en/develop/topics/worksheets/#copying-worksheets

如果你愿意的话,你可以在这里下载我的excel和代码来尝试: https://drive.google.com/open?id=1qkI8jdYDuA6e5CW6z3k0r_7j1pTvW_6D

php excel merge phpspreadsheet
3个回答
2
投票

addExternalSheet
的问题是我需要用相同的名称命名两个工作表(我加载的原始工作表和克隆工作表)。 (所有文件都在这里:https://drive.google.com/open?id=1qkI8jdYDuA6e5CW6z3k0r_7j1pTvW_6D,mergeSolved是正确的文件)这是包含解决方案的代码:

require 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

$inputFileType = 'Xlsx';
$inputFileNames = [
    'a.xlsx',
    'b.xlsx',
    'c.xlsx'
];
$sheetnames = [
    'Worksheet',
    'Worksheet1',
    'Worksheet2'
];
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$reader->setLoadSheetsOnly($sheetnames);
$inputFileName = array_shift($inputFileNames);
$spreadsheetMain = $reader->load($inputFileName);
$spreadsheetMain->getActiveSheet()->setTitle('page0');
$contador = 1;
foreach ($inputFileNames as $book => $inputFileName) {
    echo ('$inputFileName: ' . $inputFileName) . '</br>';
    $spreadsheet = $reader->load($inputFileName);
    $clonedWorksheet = clone $spreadsheet->getSheetByName('Worksheet'.$contador);
    $clonedWorksheet->setTitle('Worksheet'.$contador);
    $spreadsheetMain->addExternalSheet($clonedWorksheet);
    $contador++;
}
$writer = new Xlsx($spreadsheetMain);
$writer->save('prueba1.xlsx');`

0
投票
require 'vendor/autoload.php';

$lists = [
    'flie_01.xlsx',
    'flie_02.xlsx'
];

$outfile='/tmp/merge.xlsx';

$merge_spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();        
$merge_spreadsheet->getProperties()->setCreator("act");


foreach($lists as $file){

if(!file_exists($file)){
    continue;
}            

$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader("Xlsx");
$reader-&gt;setReadDataOnly(true);
$spreadsheet = $reader-&gt;load($file);

foreach($spreadsheet-&gt;getSheetNames() as $sheet_name ){
    $clonedWorksheet = clone $spreadsheet-&gt;getSheetByName($sheet_name);
    $clonedWorksheet-&gt;setTitle($sheet_name);
    $merge_spreadsheet-&gt;addSheet($clonedWorksheet);                
}
} $merge_spreadsheet->removeSheetByIndex(0); $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet,'Xlsx'); $writer->save($outfile);

0
投票

您可以检查每个xls内容。可能没有您要加载/复制的工作表。

第 1 步是选择要复制的工作表名称。

步骤 2 确保名称区分大小写。

步骤3 通过测试对象返回NULL来检查加载是否失败?

第 4 步,phpspreadsheet 似乎更新了,您可能需要再次调整脚本以避免错误或警告

if(!file_exists($file)){
    continue;
}            

$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader("Xlsx");
$reader-&gt;setReadDataOnly(true);
$spreadsheet = $reader-&gt;load($file);

foreach($spreadsheet-&gt;getSheetNames() as $sheet_name ){
    $clonedWorksheet = clone $spreadsheet-&gt;getSheetByName($sheet_name);
    $clonedWorksheet-&gt;setTitle($sheet_name);
    $merge_spreadsheet-&gt;addSheet($clonedWorksheet);                
}

说明:

  1. 记得辨别类型。当您处理不同类型时,添加的标识会对您有所帮助
  2. 过滤您的工作表。正如上面的答案提到的,您可能需要确保仅加载您需要的工作表以减少内存消耗。
  3. 记得设置标题并避免使用相同的名称。建议使用简称
  4. 您可以删除或注释代码中的echo。用于调试目的的回显
  5. 当您在 Excel 上找不到工作表时(C.xlsx 上的示例)。检查对象是否为NULL?如果您没有加载工作表,则会显示错误(在 null 上调用成员函数 setTitle()
  6. 您可以使用其他方法保存Excel。记得使用 spreadsheetMain
  7. 出于某种原因。我无法使用克隆。
  8. 脚本在 php 8.2 上测试
© www.soinside.com 2019 - 2024. All rights reserved.