PHPExcel内存不足错误

时间:2013-01-24 10:40:46

标签: codeigniter phpexcel

我在CodeIgniter中使用PhpExcel。当我在Excel中生成大量数据时会产生致命错误:

Out of memory (allocated 883949568) (tried to allocate 93 bytes) in /home/abc/public_html/xxx/application/libraries/excel/PHPExcel/Style.php on line 465.

即使我在控制器中设置内存限制ini_set(“memory_limit”,“5120M”);

如何解决该错误?

1 个答案:

答案 0 :(得分:0)

我在控制器中编写了以下代码:

public function export_to_excel($ sheets,$ num_cols,$ tab,$ declared_file_name,$ download_option)     {

          $this->load->library('excel/PHPExcel/iofactory');

    // Cell caching to reduce memory usage.
    $cacheMethod = PHPExcel_CachedObjectStorageFactory:: cache_to_phpTemp;
    $cacheSettings = array( ' memoryCacheSize ' => '5120MB');
    PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
            // Create new PHPExcel object
            $this->load->library('excel/PHPExcel');       

            /************ COLUMNS TO CALCULATE SUB TOTAL *******************/
            $col_heads = explode(",",rtrim($num_cols,","));

             foreach($col_heads as $key => $val)
                {
                 if(($val == 'PRODUCT LIABILITY') || ($val == 'PUBLIC LIABILITY') || ($val == 'GIT'))
                    {
                     unset($col_heads[$key]);
                    }
                }
            /************ COLUMNS WITH CURRENCY FORMAT *******************/
            $numeric_cols = array_merge($col_heads, array("LOSS OF RENT",
                                                            "TURNOVER","PRODUCT LIABILITY","PUBLIC LIABILITY","GIT"));

            /********************** SHEETS STARTS ********************************/
            $active_sheet_index = 0;
            foreach ($sheets as $sheet_name=>$rows)
            {
                if ($active_sheet_index > 0)
                {
                    // Create a new worksheet, after the default sheet
                    $this->phpexcel->createSheet();
                }
                $this->phpexcel->setActiveSheetIndex($active_sheet_index);

                $i=1; // start from 2nd row                    
                $numeric_format_col = array();

                /********************** ROWS STARTS ********************************/
                foreach ($rows as $key=>$val)
                {
                    $index = 65;
                    $a = 65;
                    $prefix_code = 65;
                    $prefix = '';

                /********************** COLS STARTS ********************************/
                    foreach ($val as $values)
                    {
                        // from A to Z
                        if ($index <= $a+25)
                        {
                            $cell = $prefix.chr($index).$i;
                        }
                        else
                        {
                            $previou_prefix = '';
                            $prefix = $previou_prefix.chr($prefix_code);
                            if ($prefix_code < $a+25)
                            {
                                $prefix_code++;
                            }
                            else
                            {
                                $prefix_code = 65;
                            }
                            $index = 65;
                            $cell = $prefix.chr($index).$i;
                        }
                        $col_dimension = $prefix.chr($index); // e.g. A,B,C
                        $this->phpexcel->getActiveSheet()->getColumnDimension($col_dimension)->setAutoSize(true);

                        //echo '<br>'.$cell;
                        /******* STORE COLUMN NUMBER FOR SHOWING SUB TOTAL OF COLUMNS ********/
                        if ($i == 1)
                        {
                            $col_val = str_replace('"','',$values);
                            if (in_array($col_val,$col_heads) || in_array(str_replace(' ','_',$col_val),$col_heads) 
                                || (strpos($col_val,'COMMISSION(') !== false && strpos($col_val,'%)') !== false))
                            {
                                $col_name = str_replace(' ','_',$col_val);
                                $tot[$col_name] = $prefix.chr($index);
                            }
                            if (in_array($col_val,$numeric_cols) || in_array(str_replace(' ','_',$col_val),$numeric_cols) 
                                || (strpos($col_val,'COMMISSION(') !== false && strpos($col_val,'%)') !== false))
                            {
                                $numeric_format_col[] = $prefix.chr($index);
                            }
                            if ($col_val == "FULL ADDRESS")
                            {
                                $business_address_col = $prefix.chr($index);
                            }
                        }                           
                        /***********************************************************/

                        $values = str_replace("\"","",$values);
                        if (isset($business_address_col) && $cell != $business_address_col.$i)
                        {                           
                            $values = str_replace("\n"," ",$values);
                            $values = str_replace("\t"," ",$values);
                            $values = str_replace("\r\t"," ",$values);
                        }                       

                        if($i == 1)
                        {
                            // FOR BROKER ACCOUNTS STATEMENT START
                            if ($tab == 'BROKER NAME' && (!(strpos($values,'BROKER COMMISSION') === false) || $values == 'BROKER FEE' || $values == 'BROKER NET') && $sheet_name != 'DBI')
                            {
                                $values = str_replace('BROKER',$sheet_name,$values);
                            }
                            // FOR BROKER ACCOUNTS STATEMENT END

                            $this->phpexcel->getActiveSheet()->getStyle($cell)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_CENTER);
                            //$this->PHPExcel->getActiveSheet()->getRowDimension("$i")->setRowHeight(25);
                            $this->phpexcel->getActiveSheet()->setCellValue($cell, trim($values));
                         }
                         else
                         {                           
                            if (isset($business_address_col) && $cell == $business_address_col.$i)
                            {
                                $val_arr = explode("|",$values);
                                $val_str = implode("\n",$val_arr);
                                $this->phpexcel->getActiveSheet()->setCellValue($cell, $val_str);
                                $this->phpexcel->getActiveSheet()->getStyle($cell)->getAlignment()->setWrapText(true);
                                //$this->phpexcel->getActiveSheet()->getStyle($cell)->getBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
                            }
                            else
                            {
                                $this->phpexcel->getActiveSheet()->setCellValue($cell, trim($values));
                            }

                            if (in_array($col_dimension,$numeric_format_col))
                            {
                                $format ='[$£-809]#,##0.00;[Red]-[$£-809]#,##0.00';   
                                //$format = utf8_encode('[$£-809]#,##0.00;[Red]-[$£-809]#,##0.00');
                                $this->phpexcel->getActiveSheet()->getStyle($cell)->getNumberFormat()->setFormatCode($format);
                            }
                         }

                        /******** SET HEADER COLOR ***********/
                        if ($i == 1)
                        {

                            if($this->color1=="#000" && $this->color2=="#000")
                            {
                            if($this->header_alignment=='text-align-left')
                            {
                                $this->phpexcel->getActiveSheet()->getStyle($cell)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_LEFT);
                            }
                            else if($this->header_alignment=='text-align-right')
                            {
                                $this->phpexcel->getActiveSheet()->getStyle($cell)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_RIGHT);                                    
                            }
                            else 
                            {
                            $this->phpexcel->getActiveSheet()->getStyle($cell)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_CENTER);
                            }

                            if($this->header_font_style=="bold italic")
                                    {
                                    $header_font_style=explode(" ",$this->header_font_style);
                                    $header_font=array(
                                                        $header_font_style[0] => true,
                                                        $header_font_style[1] => true,
                                                        'color'=> array('argb' => 'FFFFFFFF'),
                                                        'size'=>$this->body_font_size,
                                                        'name'=>$this->body_font

                                          );
                                    }
                                    else 
                                    {
                                    $header_font=array(
                                                        $this->header_font_style => true,
                                                        'color'=> array('argb' => 'FFFFFFFF'),
                                                        'size'=>$this->header_font_size,
                                                        'name'=>$this->header_font,
                                                        'Alignment'=>$this->header_alignment
                                        );
                                    }

                            $this->phpexcel->getActiveSheet()->getStyle($cell)->applyFromArray(
                            array('fill'     => array(
                                                        'type'        => PHPExcel_Style_Fill::FILL_SOLID,
                                                        'color'        => array('argb' => 'FF000000')
                                                    ),
                                'font' => $header_font ,
                                'borders' => array('outline' => array('style' => PHPExcel_Style_Border::BORDER_THICK,

                                                         )
                                                        )
                            )

                            );
                            }
                            else 
                            {
                            if($this->header_alignment=='text-align-left')
                            {
                                $this->phpexcel->getActiveSheet()->getStyle($cell)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_LEFT);
                            }
                            else if($this->header_alignment=='text-align-right')
                            {
                                $this->phpexcel->getActiveSheet()->getStyle($cell)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_RIGHT);                                    
                            }
                            else 
                            {
                            $this->phpexcel->getActiveSheet()->getStyle($cell)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_CENTER);
                            }
                        if($this->header_font_style=="bold italic")
                                    {
                                    $header_font_style=explode(" ",$this->header_font_style);
                                    $header_font=array(
                                                        $header_font_style[0] => true,
                                                        $header_font_style[1] => true,
                                                        'color'=> array('rgb' =>$this->color1),
                                                        'size'=>$this->body_font_size,
                                                        'name'=>$this->body_font

                                          );
                                    }
                                    else 
                                    {
                                    $header_font=array(
                                                        $this->header_font_style => true,
                                                        'color'=> array('rgb' =>$this->color1),
                                                        'size'=>$this->header_font_size,
                                                        'name'=>$this->header_font,
                                                        'Alignment'=>$this->header_alignment
                                        );
                                    }
                        $this->phpexcel->getActiveSheet()->getStyle($cell)->applyFromArray(
                            array('fill'     => array(
                                                        'type'        => PHPExcel_Style_Fill::FILL_SOLID,
                                                        'color'        => array('rgb' =>$this->color2)
                                                    ),
                                'font' => $header_font,
                                'borders' => array('outline' => array('style' => PHPExcel_Style_Border::BORDER_THICK,

                                                         )
                                                        )
                            )

                            );

                        }
                        }


                        $index++;
                    }
                    /********************** COLS ENDS ********************************/
                    $final_cell = $cell;
                    $i++;                       
                }
                /********************** ROWS ENDS ********************************/
                 /********************** Body Format********************************/
                 if($this->color3!="#000" || $this->color4!="#000")
                 {
                         if($this->body_alignment=='text-align-left')
                            {
                                $this->phpexcel->getActiveSheet()->getStyle("A2:$final_cell")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_LEFT);
                            }
                            else if($this->body_alignment=='text-align-right')
                            {
                                $this->phpexcel->getActiveSheet()->getStyle("A2:$final_cell")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_RIGHT);                                 
                            }
                            else 
                            {
                            $this->phpexcel->getActiveSheet()->getStyle("A2:$final_cell")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_CENTER);
                            }
                            if($this->body_font_style=="bold italic")
                            {

                                $body_font_style=explode(" ",$this->footer_font_style);
                                $body_font=array(
                                                        $body_font_style[0] => true,
                                                        $body_font_style[1] => true,
                                                        'color'=> array('rgb' =>$this->color3),
                                                        'size'=>$this->body_font_size,
                                                        'name'=>$this->body_font
                                          );
                            }
                            else 
                            {
                                $body_font=array(
                                                        $this->body_font_style => true,
                                                        'color'=> array('rgb' =>$this->color3),
                                                        'size'=>$this->body_font_size,
                                                        'name'=>$this->body_font
                                        );
                            }

                            $this->phpexcel->getActiveSheet()->getStyle("A2:$final_cell")->applyFromArray(
                                array(
                                        'fill'     => array(
                                                            'type'        => PHPExcel_Style_Fill::FILL_SOLID,
                                                            'color'        => array('rgb' =>$this->color4)
                                                        ),
                                        'font' => $body_font,
                                        'borders' => array('outline' => array('style' => PHPExcel_Style_Border::BORDER_THICK,))
                                    )                                  
                            );
                      }
                      else 
                      {
                            if($this->body_alignment == 'text-align-left')
                            {
                                $this->phpexcel->getActiveSheet()->getStyle("A2:$final_cell")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_LEFT);
                            }
                            else if($this->body_alignment == 'text-align-right')
                            {
                                $this->phpexcel->getActiveSheet()->getStyle("A2:$final_cell")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_RIGHT);                                 
                            }
                            else 
                            {
                                $this->phpexcel->getActiveSheet()->getStyle("A2:$final_cell")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_CENTER);
                            }

                            if ($this->body_font_style == "bold italic")
                            {
                                    $body_font_style = explode(" ",$this->footer_font_style);
                                    $body_font = array(
                                                            $body_font_style[0] => true,
                                                            $body_font_style[1] => true,
                                                            'size'=>$this->body_font_size,
                                                            'name'=>$this->body_font
                                                    );
                            }
                            else 
                            {
                                    $body_font = array(
                                                            $this->body_font_style => true,
                                                            'size'=>$this->body_font_size,
                                                            'name'=>$this->body_font
                                                    );
                            }

                            $this->phpexcel->getActiveSheet()->getStyle("A2:$final_cell")->applyFromArray(
                                array(
                                            'font' => $body_font,
                                            'borders' => array('outline' => array('style' => PHPExcel_Style_Border::BORDER_THICK,) )
                                        )                               
                                );

                        }

                /***************** SHOW SUB TOTAL ROW ************************/                   
                $count_tot = 0;
                if (isset($tot))
                {
                    foreach ($tot as $col_name=>$c)
                    {

                            $count_tot++;
                            $k = $i + 1;
                            $cell = $c.$k;
                            if ($count_tot == 1)
                            {
                                $first_col_name = $cell;
                            }                           
                            $j = $i - 1;               

                            $this->phpexcel->getActiveSheet()->setCellValue($cell, '=SUM('.$c.'2:'.$c.$j.')');

                            $format ='[$£-809]#,##0.00;[Red]-[$£-809]#,##0.00';
                            //$format = utf8_encode('[$£-809]#,##0.00;[Red]-[$£-809]#,##0.00');
                            $this->phpexcel->getActiveSheet()->getStyle($cell)->getNumberFormat()->setFormatCode($format);                           


                    }
                }

                $last_col_name = $cell;                   

                /***************** SET BG COLOR IN SUB TOTAL CELLS ****************/
                if (isset($first_col_name))
                {

                        if($this->color5=="#000" && $this->color6=="#000")
                        {
                            if($this->footer_alignment=='text-align-left')
                            {
                                $this->phpexcel->getActiveSheet()->getStyle("$first_col_name:$last_col_name")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_LEFT);
                            }
                            else if($this->footer_alignment=='text-align-right')
                            {
                                $this->phpexcel->getActiveSheet()->getStyle($cell)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_RIGHT);                                    
                            }
                            else 
                            {
                            $this->phpexcel->getActiveSheet()->getStyle("$first_col_name:$last_col_name")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_CENTER);
                            }

                            if($this->footer_font_style=="bold italic")
                                    {

                                        $footer_font_style=explode(" ",$this->footer_font_style);
                                        $footer_font=array(
                                                        $footer_font_style[0] => true,
                                                        $footer_font_style[1] => true,
                                                        'size'=>$this->footer_font_size,
                                                        'name'=>$this->footer_font
                                          );
                                    }
                                    else 
                                    {
                                        $footer_font=array(
                                                        $this->footer_font_style => true,
                                                        'size'=>$this->footer_font_size,
                                                        'name'=>$this->footer_font
                                                        );
                                    }
                            $this->phpexcel->getActiveSheet()->getStyle("$first_col_name:$last_col_name")->applyFromArray(
                        array('fill'     => array(
                                                    'type'        => PHPExcel_Style_Fill::FILL_SOLID,
                                                    'color'        => array('argb' => 'FFCCFFCC')
                                                ),
                              'font'    =>$footer_font,
                              'borders' => array('outline' => array('style' => PHPExcel_Style_Border::BORDER_THICK,

                                                         )
                                                        )
                             )
                        );
                        }
                        else 
                        {

                            if($this->footer_alignment=='text-align-left')
                            {
                                $this->phpexcel->getActiveSheet()->getStyle("$first_col_name:$last_col_name")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_LEFT);
                            }
                            else if($this->footer_alignment=='text-align-right')
                            {
                                $this->phpexcel->getActiveSheet()->getStyle($cell)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_RIGHT);                                    
                            }
                            else 
                            {
                            $this->phpexcel->getActiveSheet()->getStyle("$first_col_name:$last_col_name")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_CENTER);
                            }
                            if($this->footer_font_style=="bold italic")
                            {

                                $footer_font_style=explode(" ",$this->footer_font_style);
                                $footer_font=array(
                                                        $footer_font_style[0] => true,
                                                        $footer_font_style[1] => true,
                                                        'color'=> array('rgb' =>$this->color5),
                                                        'size'=>$this->footer_font_size,
                                                        'name'=>$this->footer_font
                                          );
                            }
                            else 
                            {
                                $footer_font=array(
                                                        $this->footer_font_style => true,
                                                        'color'=> array('rgb' =>$this->color5),
                                                        'size'=>$this->footer_font_size,
                                                        'name'=>$this->footer_font
                                                        );
                            }


                    $this->phpexcel->getActiveSheet()->getStyle("$first_col_name:$last_col_name")->applyFromArray(
                    array('fill'     => array(
                                                        'type'        => PHPExcel_Style_Fill::FILL_SOLID,
                                                        'color'        => array('rgb' =>$this->color6)
                                                    ),
                                'font' =>$footer_font,
                                'borders' => array('outline' => array('style' => PHPExcel_Style_Border::BORDER_THICK,

                                                         )
                                                        )
                            )
                        );
                }
                }

                $this->phpexcel->getActiveSheet()->getStyle("A1:$final_cell")->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_TOP);

                // Rename sheet
                $this->phpexcel->getActiveSheet()->setTitle($sheet_name);
                $active_sheet_index++;                   
            }

           // $this->phpexcel->getActiveSheet()->setAutoFilter('A1:C9');
            /********************** SHEETS ENDS ********************************/
            $file_name = $declared_file_name.'.xls';
            if ($download_option == 'declare_report' || $download_option == 'save_report')
            {


                // Redirect output to a client’s web browser (Excel2007)                    
                header('Content-Type: application/vnd.ms-excel');
                header('Content-Disposition: attachment;filename="'.$file_name.'"');
                header('Cache-Control: max-age=0');

                $objWriter = PHPExcel_IOFactory::createWriter($this->phpexcel, 'Excel5');
                $objWriter->save('php://output'); 
                if ($download_option == 'declare_report')
                {
                $objWriter->save('uploads/declared_reports/'.$file_name);
                }
                else{
                $objWriter->save('uploads/saved_reports/'.$file_name);
                }
                //echo __FILE__;
                //$objWriter->save(str_replace(__FILE__,'uploads/declared_reports/'.$file_name,__FILE__));            

            }
            else
            {
                // Redirect output to a client’s web browser (Excel2007)
                header('Content-Type: application/vnd.ms-excel');
                header('Content-Disposition: attachment;filename="'.$file_name.'"');
                header('Cache-Control: max-age=0');

                $objWriter = PHPExcel_IOFactory::createWriter($this->phpexcel, 'Excel5');

                $objWriter->save('php://output');
            }   
            exit;

}