2015-01-07 50 views
2

所以我大Excel文件工作,首先我先PHP,但我得到总是与内存大小的问题,即使我增加PHP内存限制,我有其他问题与Apache,我想每一件事情,但总是同样的问题。如何加载/创建Excel文件?

所以如果有人有任何想法如何处理大的Excel文件,我会非常感激。

+0

我以前有过这个问题!我得出结论,我必须使用Java以外的东西。 –

+0

*我有其他问题与Apache *和哪些问题?请更多信息 – donald123

+0

Apache停止工作时,我增加了PHP内存,该过程仍然工作,直到Apache崩溃 –

回答

1

请参考这个。这将帮助你阅读PHP Excel文件

Excel IO Factory

它可以读取在“块”一个工作表中读取使用的过滤器,请自行了解其工作

inputFileType = 'Excel5'; 
$inputFileName = './sampleData/example2.xls'; 


/** 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 = '') { 
     // Only read the heading row, and the rows that are configured in $this->_startRow and $this->_endRow 
     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); 



echo '<hr />'; 


/** 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 <= 240; $startRow += $chunkSize) { 
    echo 'Loading WorkSheet using configurable filter for headings row 1 and for rows ',$startRow,' to ',($startRow+$chunkSize-1),'<br />'; 
    /** 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 

    $sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true); 
    var_dump($sheetData); 
    echo '<br /><br />'; 
} 

请注意,此读取过滤器将始终读取工作表的第一行以及块规则定义的行。

当使用读过滤器时,PHPExcel仍然会解析整个文件,但只加载那些与定义的读过滤器匹配的单元,所以它只使用该单元所需的内存。但是,它会多次解析文件,每个块大小一次,因此速度会变慢。此示例一次读取20行:要逐行读取,只需将$ chunkSize设置为1即可。

如果您有用不同“块”引用单元格的公式,这也会导致问题,因为数据只是简单的' t可用于当前“块”之外的单元格。

+0

感谢马克我已经更新了该链接 –