2012-04-02 5 views
2

PHPExcel 라이브러리를 사용하여 약 1500 개의 행을 반복하려고합니다. 각 행에는 약 25 개의 열이 있습니다.청크 루프에서 PHPExcel 라이브러리를 사용하여 파일의 끝을 어떻게 결정합니까?

내가 (PHPExcel runs out of 256, 512 and also 1024MB of RAM에서 가져온)이 코드를 사용하고 있습니다 :

/** Create a new Reader of the type defined in $inputFileType **/ 
$objReader = PHPExcel_IOFactory::createReader($inputFileType); 
/** Define how many rows we want to read for each "chunk" **/ 
$chunkSize = 20; 
/** 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); 

/** Loop to read our worksheet in "chunk size" blocks **/ 
/** $startRow is set to 2 initially because we always read the headings in row #1 **/ 
for ($startRow = 2; $startRow <= 65536; $startRow += $chunkSize) { 
    /** Tell the Read Filter, the limits on which rows we want to read this iteration  **/ 
$chunkFilter->setRows($startRow,$chunkSize); 
/** Load only the rows that match our filter from $inputFileName to a PHPExcel Object **/ 
$objPHPExcel = $objReader->load($inputFileName); 
// Do some processing here 

// Free up some of the memory 
$objPHPExcel->disconnectWorksheets(); 
unset($objPHPExcel); 

} 나는 많은 행이 데이터를 처리 할

. 그렇다면

function _emptyRow($row) { 
    $empty=true; 

    foreach($row as $r) { 
    if ($r!='') { 
     $empty = false; 
     } 
    } 

    return $empty; 

} 

: 다음 검색 행이 작은 함수를 작성하여 비어있는 경우 나 또한 확인하려고 나는 워크 시트 개체에서 메소드 getHighestRow()를 사용했지만, 그것은 단지

A1

를 반환 유지 _emptyRow() 나는 루프 밖으로 break 것입니다. 이 일은 나를 위해 일하지 않았다.

누구나 데이터가있는 모든 레코드 만 검색하는 방법을 제안 할 수 있습니까? 이것을 실행하면 약 1500 개의 행에 데이터가 있지만 시간이 만료되기 전에 약 23000에 도달합니다 (set_time_limit (240) 사용).

답변

4

나는 일반적으로 이런 짓을 했을까 :

$objPHPExcel = $objReader->load($inputFileName);  
$rows = count($objPHPExcel->getActiveSheet()->toArray());  
for ($start_row = 1; $start_row < $rows; $start_row ++) 
// ... 

$excel->getActiveSheet()->toArray()는 배열 (데이터) 각 행을 반환합니다

.

3

버전 1.7.6 이하의 경우 Christopher Mullins는 전체 파일을 읽기 전에 워크 시트 정보를 읽을 수있는 a patch을 작성했습니다. 이 정보에는 각 워크 시트의 행 및 열 개수가 포함되므로 실제 통합 문서 데이터를 "청크 읽기"전에이 값을 사용하여 행 개수를 검색 할 수 있습니다.

이 패치는 최신 SVN 코드에 통합되었으므로 나중에 PHPExcel 코어에 포함될 것입니다.

관련 문제