我有两个Excel文件,我需要将第一个文件中的数据传输到第二个文件中。
我将首先切碎文件中的某些列,并将它们放在目标文件的4个单独的列中。保存文件时,目标文件中的数据验证功能会丢失。
对不起,我的英语不好。
$sourceExcel = \PhpOffice\PhpSpreadsheet\IOFactory::load($this->sourceExcelPath);
$targetExcel = \PhpOffice\PhpSpreadsheet\IOFactory::load($this->targetExcelPath);
$sourceExcelRowLength = $sourceExcel->getActiveSheet()->getHighestRow();
for ($i = 1; $i <= $sourceExcelRowLength; $i++) {
foreach ($this->pairedColumns as $column) { // the first column in the source file is equal to the second column in the target file etc.
$value = $sourceExcel->getActiveSheet()->getCellByColumnAndRow($column["sourceIndex"], ($this->sourceExcelHeaderStartingIndex + $i))->getValue();
$targetExcel->getActiveSheet()->setCellValueByColumnAndRow($column["targetIndex"], ($this->targetExcelHeaderStartingIndex + $i), $value);
}
}
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($targetExcel, 'Xlsx');
$writer->save($this->sourceExcelPath);
phpspreadsheet版本:^ 1.6