简体   繁体   中英

PHPexcel merge 9 xlsx to one

I have the following script.

I want to combine 9 (10000 rows per xlsx) xlsx to one.

I found this script and runs one time the first three and then other times the first four, but does not extend to 9.

Now a have add setREadDataOnly But that does not work either. Is there another way to merge or do i miss some settings them?

The script: require_once 'PHPExcel.php'; require_once 'PHPExcel/Writer/Excel2007.php'; require_once 'PHPExcel/IOFactory.php';

    $objReader = PHPExcel_IOFactory::createReaderForFile("big.xlsx");
    $objReader->setReadDataOnly(true);
    $objPHPExcel1 = $objReader->load("big.xlsx");

for( $x=1; $x <= 9;$x++  ){

    $objReader2 = PHPExcel_IOFactory::createReaderForFile("000$x.xlsx");
    $objReader2->setReadDataOnly(true);
    $objPHPExcel2 = $objReader2->load("000$x.xlsx");

    $findEndDataRow2        = $objPHPExcel2->getActiveSheet()->getHighestRow();
    $findEndDataColumn2     = $objPHPExcel2->getActiveSheet()->getHighestColumn();
    $findEndData2 = $findEndDataColumn2 . $findEndDataRow2;

    $data2 = $objPHPExcel2->getActiveSheet()->rangeToArray('A2:' . $findEndData2);

    $appendStartRow = $objPHPExcel1->getActiveSheet()->getHighestRow() + 1;
    $objPHPExcel1->getActiveSheet()->fromArray($data2, null, 'A' . $appendStartRow);

}

    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel1, 'Excel2007');
    $objWriter->save('big.xlsx');

Don't open and save your destination file ("big.xlsx") inside every iteration of the loop... open it once before the loop, and only save it after the last iteration of the loop has completed.... that will be faster and save memory:

And make sure you flush the smaller individual files, otherwise garbage collection won't clean them up completely from memory

require_once 'PHPExcel.php';
require_once 'PHPExcel/Writer/Excel2007.php';
require_once 'PHPExcel/IOFactory.php';

$objReader = PHPExcel_IOFactory::createReaderForFile("big.xlsx");
$objReader->setReadDataOnly(true);
$objPHPExcel1 = $objReader->load("big.xlsx");

$appendStartRow = $objPHPExcel1->getActiveSheet()->getHighestRow() + 1;

for( $x=1; $x <= 9;$x++  ){

    $objReader2 = PHPExcel_IOFactory::createReaderForFile("000$x.xlsx");
    $objReader2->setReadDataOnly(true);
    $objPHPExcel2 = $objReader2->load("000$x.xlsx");

    $findEndDataRow2        = $objPHPExcel2->getActiveSheet()->getHighestRow();
    $findEndDataColumn2     = $objPHPExcel2->getActiveSheet()->getHighestColumn();
    $findEndData2 = $findEndDataColumn2 . $findEndDataRow2;

    $data2 = $objPHPExcel2->getActiveSheet()->rangeToArray('A2:' . $findEndData2);

    $objPHPExcel2->disconnectWorksheets();
    unset($objPHPExcel2);

    $objPHPExcel1->getActiveSheet()->fromArray($data2, null, 'A' . $appendStartRow);
    $appendStartRow += count($data2);

    unset($data2);
}

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel1, 'Excel2007');
$objWriter->save('big.xlsx');

But if it's only running 3 or 4 iterations of the loop instead of 9, check why it's failing in the logs, hitting memory limits or timeouts are the most likely problems

My approach was to use a loop and set the cell value individually. I guess I could have used a part of @Mark Baker answer but this works for me. Note that all files should have the same number of fields.

<?php
@author: Winston Hope
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="merged.xls"');
header('Cache-Control: max-age=0');

//error_reporting(E_ALL);
set_time_limit(0);
date_default_timezone_set('America/Belize');

set_include_path('Classes/');
include 'PHPExcel/IOFactory.php';

$inputFileName = 'hub-project-assignment-Example.xlsx';
$inputFileName2 = 'hub-project-assignment-Example-2.xlsx';

$files = array();
$files[] = 'hub-project-assignment-Example.xlsx';
$files[] = 'hub-project-assignment-Example-2.xlsx';
$files[] = 'hub-project-assignment-Example-3.xlsx';
$files[] = 'hub-project-assignment-Example-4.xlsx';
$files[] = 'hub-project-assignment-Example-5.xlsx';

$objPHPExcel = PHPExcel_IOFactory::load($files[0]);
$sheetData = $objPHPExcel->getSheet(0)->toArray(null,true,true,true);
$firstSheetSize = count($sheetData);
$index = $firstSheetSize;
$activeFirstSheet = $objPHPExcel->getSheet(0);
unset($files[0]);
$total = $firstSheetSize;

foreach($files as $file) {

    $objPHPExcel2 = PHPExcel_IOFactory::load($file);
    $sheetData2 = $objPHPExcel2->getSheet(0)->toArray(null,true,true,true);

    $secondSheetSize = count($sheetData2);
    $total += $secondSheetSize-1;
    $pos = 2;

    for(; $index<$total; $index++) {
        $row = $sheetData2[$pos];
        foreach($row as $letter => $value) {
            $cell =  $letter.($index+1);
            $activeFirstSheet->setCellValue($cell, $value);
        }
        $pos++;
    }
}

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output'); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM