1 minute read

<?php

error_reporting(E_ALL); set_time_limit(0);

date_default_timezone_set(‘Europe/London’);

/** Include path **/ set_include_path(‘PHPExcel/Classes/’);

/** PHPExcel_IOFactory */ include ‘PHPExcel/IOFactory.php’;

$inputFileType = ‘Excel2007’; $inputFileName = ‘page_201710_201712.xlsx’;

/  Create a new Reader of the type defined in $inputFileType  **/ $objReader = PHPExcel_IOFactory::createReader($inputFileType); /  Load $inputFileName to a PHPExcel Object  **/ $objPHPExcel = $objReader->load($inputFileName);

echo ‘<hr />’;

echo ‘Reading the number of Worksheets in the WorkBook<br />’; /**  Use the PHPExcel object’s getSheetCount() method to get a count of the number of WorkSheets in the WorkBook  */ $sheetCount = $objPHPExcel->getSheetCount(); echo ‘There ‘,(($sheetCount == 1) ? ‘is’ : ‘are’),’ ‘,$sheetCount,’ WorkSheet’,(($sheetCount == 1) ? ‘’ : ‘s’),’ in the WorkBook<br /><br />’;

echo ‘Reading the names of Worksheets in the WorkBook<br />’;

$rowss = array(); /**  Use the PHPExcel object’s getSheetNames() method to get an array listing the names/titles of the WorkSheets in the WorkBook  */ $sheetNames = $objPHPExcel->getSheetNames();

foreach($sheetNames as $sheetIndex => $sheetName) { echo $sheetIndex,’=’,$sheetName,”\n”; $sheet = $objPHPExcel->getSheet($sheetIndex);  $maxRow = $sheet->getHighestRow();          // 마지막 라인 $maxColumn = $sheet->getHighestColumn();    // 마지막 칼럼 $target = “A”.”1”.”:”.”$maxColumn”.”$maxRow”; $rows = $sheet->rangeToArray($target, NULL, TRUE, FALSE); // if($sheetIndex=0){ // array_shift($rows); // }else{ // foreach($rows as $k=>$v){ // $rowss[$sheetIndex][$k] // } // } $rowss[$sheetIndex]  = $rows; } // print_r($rowss[0]); // print_r($rowss); // exit; //============= $inputFileNames = array( // ‘page_201710_201712.xlsx’, ‘page_201707_201709.xlsx’, ‘page_201704_201706.xlsx’, ‘page_201701_201703.xlsx’, ‘page_201610_201612.xlsx’, ‘page_201607_201609.xlsx’, ‘page_201604_201606.xlsx’, ‘page_201601_201603.xlsx’, ‘page_201510_201512.xlsx’, ); $inputFileType = ‘Excel2007’;

foreach ($inputFileNames as $inputFileName) { // $inputFileName = ‘page_201707_201709.xlsx’;

/**  Create a new Reader of the type defined in $inputFileType  **/
$objReader2 = PHPExcel_IOFactory::createReader($inputFileType);
/**  Load $inputFileName to a PHPExcel Object  **/
$objPHPExcel2 = $objReader-&gt;load($inputFileName);

$sheetNames = $objPHPExcel2-&gt;getSheetNames();
foreach($sheetNames as $sheetIndex =&gt; $sheetName) {
	echo $sheetIndex,'=',$sheetName,"\n";
	$sheet = $objPHPExcel2-&gt;getSheet($sheetIndex); 
	$maxRow = $sheet-&gt;getHighestRow();          // 마지막 라인
	$maxColumn = $sheet-&gt;getHighestColumn();    // 마지막 칼럼
	if($sheetIndex==0){
		$target = "A"."3".":"."$maxColumn"."$maxRow";	
		$rows = $sheet-&gt;rangeToArray($target, NULL, TRUE, FALSE);	
	}else{
		$target = "A"."2".":"."$maxColumn"."$maxRow";	
		$rows = $sheet-&gt;rangeToArray($target, NULL, TRUE, FALSE);	
	}
	
	
	
	// var_dump($rowss[$sheetIndex]);
	$rowss[$sheetIndex] = array_merge($rowss[$sheetIndex],$rows);
} }

$sheetNames = $objPHPExcel->getSheetNames();

foreach($sheetNames as $sheetIndex => $sheetName) { echo $sheetIndex,’=’,$sheetName,”\n”; $sheet = $objPHPExcel->getSheet($sheetIndex);  $maxRow = $sheet->getHighestRow();          // 마지막 라인 $maxColumn = $sheet->getHighestColumn();    // 마지막 칼럼 $target = “A”.”1”.”:”.”$maxColumn”.”$maxRow”; // $rowss[$sheetIndex] = $sheet->rangeToArray($target, NULL, TRUE, FALSE); $sheet->fromArray($rowss[$sheetIndex], ‘’, ‘A1’); } $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, ‘Excel2007’); $objWriter->save(‘result.xlsx’);

?>


🔗original-link

Updated: