Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- error_reporting(E_ALL);
- set_time_limit(0);
- ini_set("memory_limit","-1");
- /** Set Include path to point at the PHPExcel Classes folder **/
- set_include_path(get_include_path() . PATH_SEPARATOR . 'xls/Classes/');
- /** Include PHPExcel_IOFactory **/
- include 'xls/Classes/PHPExcel/IOFactory.php';
- $inputFileName = 'FILE.xls';
- $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
- /** Define a Read Filter class implementing PHPExcel_Reader_IReadFilter */
- 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 = '')
- {
- if (($row == 1) || ($row >= $this->_startRow && $row < $this->_endRow))
- {
- return true;
- }
- return false;
- }
- }
- echo 'Loading file ',pathinfo($inputFileName,PATHINFO_BASENAME),' using IOFactory with a defined reader type of ',$inputFileType,'<br />';
- /** Create a new Reader of the type defined in $inputFileType **/
- $objReader = PHPExcel_IOFactory::createReader($inputFileType);
- //$objReader->setLoadAllSheets();
- /*print_r($objReader);
- exit;*/
- /** Define how many rows we want to read for each "chunk" **/
- $chunkSize = 100;
- //total rows in excel
- /** Create a new Instance of our Read Filter **/
- $chunkFilter = new chunkReadFilter();
- /** Tell the Reader that we want to use the Read Filter that we've Instantiated **/
- $objReader->setReadFilter($chunkFilter);
- $objReader->setReadDataOnly(true);
- /** Loop to read our worksheet in "chunk size" blocks **/
- /*
- $cacheMethod = PHPExcel_CachedObjectStorageFactory:: cache_to_phpTemp;
- $cacheSettings = array( ' memoryCacheSize ' => '1000MB');
- PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
- $cacheMethod=PHPExcel_CachedObjectStorageFactory::cache_in_memory_serialized;
- PHPExcel_Settings::setCacheStorageMethod($cacheMethod);
- $cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_in_memory_gzip;
- if (!PHPExcel_Settings::setCacheStorageMethod($cacheMethod)) {
- die($cacheMethod . " caching method is not available" . EOL);}// echo date('H:i:s') , " Enable Cell Caching using " , $cacheMethod , " method" , PHP_EOL;
- */
- $spreadsheetInfo = $objReader->listWorksheetInfo($inputFileName);
- //print_r($spreadsheetInfo);
- // exit;
- for($l=0;$l<3;++$l){
- $sheetnames = array($spreadsheetInfo[$l]['worksheetName']);
- $objReader->setLoadSheetsOnly($sheetnames);
- $totalRows=$spreadsheetInfo[$l]['totalRows'];
- for ($startRow = 2; $startRow <= $totalRows; $startRow += $chunkSize) {
- //echo "in for loop<br>";
- //echo 'Loading WorkSheet using configurable filter for headings row 1 and for rows ',$startRow,' to ',($startRow+$chunkSize-1),'<br />';
- $chunkFilter->setRows($startRow,$chunkSize);
- // Load only the rows that match our filter from $inputFileName to a PHPExcel Object
- $objPHPExcel = $objReader->load($inputFileName);
- //$objPHPExcel->setActiveSheetIndex(1);
- $objWorksheet = $objPHPExcel->getActiveSheet();
- //$highestColumn = $objWorksheet->getHighestColumn();
- //echo 'A'.$startRow.':'.$highestColumn.($startRow + $chunkSize-1);exit;
- //$EndRow=$startRow + $chunkSize-1;
- //if($EndRow>$totalRows)$EndRow=$totalRows;
- //$sheetData = $objWorksheet->rangeToArray('A'.$startRow.':'.$highestColumn.($EndRow),null, false, false, true);
- // print_r($sheetData);
- // $row=$objWorksheet->toArray();
- //print_r($row);
- /*
- $cell = $objWorksheet->getCellByColumnAndRow(1, 1);
- // читаем значение ячейки
- $value = $cell->getValue();
- print_r($value);
- exit;*/
- $worksheet=$objWorksheet;
- $highestRow = $worksheet->getHighestRow(); // e.g. 10
- $highestColumn ='K';// = $worksheet->getHighestColumn(); // e.g 'F'
- $highestColumnIndex= 11;//PHPExcel_Cell::columnIndexFromString($highestColumn);
- for ($row = 1; $row <= $highestRow; ++ $row) {
- //echo '<tr>';
- $rowArray=array();
- for ($col = 0; $col < $highestColumnIndex; ++ $col) {
- $cell = $worksheet->getCellByColumnAndRow($col, $row);
- $val = $cell->getValue();
- $rowArray[]=$val;
- //$dataType = PHPExcel_Cell_DataType::dataTypeForValue($val);
- // echo '<td>' . $val . '<br>(Typ ' . $dataType . ')</td>';
- }
- print_r($rowArray);
- // echo '</tr>';
- }
- break;
- $objPHPExcel->disconnectWorksheets();
- unset($objPHPExcel);
- //exit;
- break;
- }
- }
- //print_r($spreadsheetInfo);
- $time=time();
- for($l=0;$l<3;++$l){
- $hnd=fopen($l.'.txt','w');
- $sheetnames = array($spreadsheetInfo[$l]['worksheetName']);
- $objReader->setLoadSheetsOnly($sheetnames);
- $totalRows=$spreadsheetInfo[$l]['totalRows'];
- for ($startRow = 2; $startRow <= $totalRows; $startRow += $chunkSize) {
- $chunkFilter->setRows($startRow,$chunkSize);
- $objPHPExcel = $objReader->load($inputFileName);
- $objWorksheet = $objPHPExcel->getActiveSheet();
- /* $worksheet=$objWorksheet;
- $highestRow = $worksheet->getHighestRow(); // e.g. 10
- $highestColumn ='K';// = $worksheet->getHighestColumn(); // e.g 'F'
- $highestColumnIndex= 11;//PHPExcel_Cell::columnIndexFromString($highestColumn);
- for ($row = $startRow; $row <= $highestRow; ++ $row) {
- //echo '<tr>';
- $rowArray=array();
- for ($col = 0; $col < $highestColumnIndex; ++ $col) {
- $cell = $worksheet->getCellByColumnAndRow($col, $row);
- $val = $cell->getValue();
- $rowArray[]=$val;
- }
- // print_r($rowArray);
- fwrite($hnd,implode(';',$rowArray).PHP_EOL);
- // echo '</tr>';
- }*/
- $EndRow=$startRow + $chunkSize-1;
- if($EndRow>$totalRows)$EndRow=$totalRows;
- $rowArray = $objWorksheet->rangeToArray('A'.$startRow.':K'.($EndRow),null, false, false, false);
- // print_r($rowArray);
- // exit;
- foreach($rowArray as $rowVal){
- foreach($rowVal as $val) fwrite($hnd,$val.';');
- fwrite($hnd,PHP_EOL);
- }
- // print_r( $rowArray);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement