使用phpSpreadSheet合并excel

时间:2018-05-09 09:51:54

标签: php excel merge phpspreadsheet

我想将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);

但是当我执行该代码时,我收到错误:'Sheet not exists'

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

如果您需要,可以在此下载我的Excel和代码以供尝试: https://drive.google.com/open?id=1qkI8jdYDuA6e5CW6z3k0r_7j1pTvW_6D

2 个答案:

答案 0 :(得分:0)

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');`

答案 1 :(得分: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);