I split a large xls file into several smaller ones using setReadFilter and $ chunkFilter. I save each piece to another xls file. But it turns out that each next saved file contains empty cells.
For example, there were 5 lines in the file, I need to divide this file into 2 files with 3 lines each. In the first saved file will be the first 3 lines of a large file, and in the second - the first 3 lines will be empty, and then the remaining 2 lines.
Here's the question of how to get rid of these blank lines?
Here is the code itself:
$file_path = dirname(__FILE__).'/'; require_once $file_path . '../../PHPExcel/Classes/PHPExcel.php'; class chunkReadFilter implements PHPExcel_Reader_IReadFilter { private $_startRow = 0; private $_endRow = 0; /** Set the list of rows that we want to read */ public function setRows($startRow, $chunkSize) { $this->_startRow = $startRow; $this->_endRow = $startRow + $chunkSize; } public function readCell($column, $row, $worksheetName = '') { // Only read the heading row, and the rows that are configured in $this->_startRow and $this->_endRow if (($row >= $this->_startRow && $row < $this->_endRow)) { return true; } return false; } } $file_name = 'test1'; $file = $file_path."../../vigruzka_aptek/".$file_name.".xls"; /* some vars */ $chunkSize = 3; //размер считываемых строк за раз $startRow = 1; $exit = false; //флаг выхода $empty_value = 0; //счетчик пустых знаений /* some vars */ if (!file_exists($file)) { exit(); } $objReader = new PHPExcel_Reader_Excel5($file); $objReader->setReadDataOnly(true); $chunkFilter = new chunkReadFilter(); $objReader->setReadFilter($chunkFilter); //внешний цикл, пока файл не кончится $j = 1; while ( !$exit ) { $chunkFilter->setRows($startRow,$chunkSize); //устанавливаем знаечние фильтра $objPHPExcel = $objReader->load($file); //открываем файл $objPHPExcel->setActiveSheetIndex(0); //устанавливаем индекс активной страницы $objWorksheet = $objPHPExcel->getActiveSheet(); //делаем активной нужную страницу for ($i = $startRow; $i < $startRow + $chunkSize; $i++) //внутренний цикл по строкам { $value = trim(htmlspecialchars($objWorksheet->getCellByColumnAndRow(0, $i)->getValue())); //получаем первое знаение в строке if ( empty($value) ) //проверяем значение на пустоту $empty_value++; if ($empty_value == 3) //после трех пустых значений, завершаем обработку файла, думая, что это конец { $exit = true; continue; } } //сохраняем файл $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel); $objWriter->save($file_path."../export/".$file_name.'-'.$j.".xls"); $objPHPExcel->disconnectWorksheets(); //чистим unset($objPHPExcel); //память $startRow += $chunkSize; //переходим на следующий шаг цикла, увеличивая строку, с которой будем читать файл $j++; }