我想将 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
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');`
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->setReadDataOnly(true);
$spreadsheet = $reader->load($file);
foreach($spreadsheet->getSheetNames() as $sheet_name ){
$clonedWorksheet = clone $spreadsheet->getSheetByName($sheet_name);
$clonedWorksheet->setTitle($sheet_name);
$merge_spreadsheet->addSheet($clonedWorksheet);
}
}
$merge_spreadsheet->removeSheetByIndex(0);
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet,'Xlsx');
$writer->save($outfile);
您可以检查每个xls内容。可能没有您要加载/复制的工作表。
第 1 步是选择要复制的工作表名称。
步骤 2 确保名称区分大小写。
步骤3 通过测试对象返回NULL来检查加载是否失败?
第 4 步,phpspreadsheet 似乎更新了,您可能需要再次调整脚本以避免错误或警告
if(!file_exists($file)){
continue;
}
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader("Xlsx");
$reader->setReadDataOnly(true);
$spreadsheet = $reader->load($file);
foreach($spreadsheet->getSheetNames() as $sheet_name ){
$clonedWorksheet = clone $spreadsheet->getSheetByName($sheet_name);
$clonedWorksheet->setTitle($sheet_name);
$merge_spreadsheet->addSheet($clonedWorksheet);
}
说明: