PHPExcel在执行过程中停止

时间:2014-02-22 21:00:28

标签: php cakephp

我使用PHPExcel来读取excel并将其存储在数据库中。 当记录的数量(少说少于500)时它工作正常。 如果在excel中有高达2000的记录,它会在中间停止(加载400到500行之后),没有任何错误。

注意:我正在使用sqlite来减少内存使用量,但我仍面临问题......

这是我的代码。请建议使其可行。

    ini_set('max_execution_time', 0);
    set_time_limit(0) ;
    //Set memory limit to maximum...

       ini_set('memory_limit', '-1');
    error_reporting(E_ALL);

        set_include_path(get_include_path() . PATH_SEPARATOR . 'Classes/');



/** PHPExcel_IOFactory */
    include $unsecured_param['home_dir'].'APIs/PHPExcelReader/Classes/PHPExcel/IOFactory.php';
    //echo 'Loading file ',pathinfo($inputFileName,PATHINFO_BASENAME),' using IOFactory to identify the format<br />';
    //if you only want to read certain cells within worksheets, you can add a filter:
                $inputFileType = 'Excel2007';

    /**  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  **/
            for ($startRow = 1; $startRow <= 2000; $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);
                $cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_sqlite3;
                $cacheSettings = array( 
                    'cacheTime' => 600
                );
                PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
                /**  Load only the rows that match our filter from $inputFileName to a PHPExcel Object  **/
                $objPHPExcel = $objReader->load($inputFileName);
                //  Do some processing here
                $range_xl="A".($startRow).":X".($startRow + $chunkSize);
                echo "$range_xl.<br>";
                $sheetData = $objPHPExcel->getActiveSheet()->rangeToArray($range_xl,null,true,true,true);
                //var_dump($sheetData);
                unset($objPHPExcel);
                //store data into array..
                $i=0;$h=1;
                foreach($sheetData as $rec)
                {
                    foreach($rec as $part)
                    {
                        $items[$i]=$part; //echo "items $i]=" ; echo $items[$i];echo "<br>";
                        $i=$i+1;            
                    }
                    $i=0;//1 row completed
                    $h=$h+1;//echo $items[0]."<br>";
    //echo "<br>---------------------<br>";             
    //start loading a row into database..
    if($h>4) {
    //store excel values
    $sno=trim($items[0]);echo $sno."<br>";
    $cat=trim($items[1]);
    $id_xl=trim($items[2]);
    $name=trim($items[3]);
    .... 

0 个答案:

没有答案