对于大量记录导出不能更快地工作..当我导出时超时错误下面是我正在使用的代码

时间:2014-11-12 13:04:21

标签: php export phpexcel



<?php 

require_once('../../config.php');


$MySQL_host     = $CFG->dbhost;
$MySQL_username = $CFG->dbuser;
$MySQL_password = $CFG->dbpass;
$MySQL_database = $CFG->dbname;
require_login();

$queries=$_POST['export'];



$Connect = @mysql_connect($MySQL_host, $MySQL_username, $MySQL_password) 
or die("Couldn't connect to MySQL:<br>" . mysql_error() . "<br>" . mysql_errno()); 
//select database 
$Db = @mysql_select_db($MySQL_database, $Connect) 
or die("Couldn't select database:<br>" . mysql_error(). "<br>" . mysql_errno()); 
//execute query 
$result = @mysql_query($queries,$Connect) 
or die("Couldn't execute query:<br>" . mysql_error(). "<br>" . mysql_errno()); 
error_reporting(E_ALL);

 require_once 'PHPExcel.php';
 $objPHPExcel = new PHPExcel();

 // Set the active Excel worksheet to sheet 0 

$objPHPExcel->setActiveSheetIndex(0);  

// Initialise the Excel row number 

$styleArray = array(
  'borders' => array(
    'allborders' => array(
      'style' => PHPExcel_Style_Border::BORDER_THIN
    )
  )
);


$current_date = date("d/m/y");
$filename = "Individual Employee Completion " . $current_date ;

$objPHPExcel->getActiveSheet()->SetCellValue('A2', "Individual Employee Completion");
//$objPHPExcel->getActiveSheet()->SetCellValue('A2', date('d-m-y'));
$objPHPExcel->getActiveSheet()->getStyle('A3:F3')->getFont()->setBold(true)->getColor()->setRGB('F0FFFF');
$objPHPExcel->getActiveSheet()->getStyle('A2:F2')->getFont()->setBold(true)->getColor()->setRGB('F0FFFF');
$objPHPExcel->getActiveSheet()->mergecells('A2:F2');
$objPHPExcel->getActiveSheet()->getStyle('A2:F2')->getAlignment('A2:F2')->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('A2:F2')->getAlignment('A2:F2')->setHorizontal(PHPExcel_Style_Alignment::VERTICAL_CENTER);
//$objPHPExcel->getActiveSheet()->getStyle('A')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);








function cellColor($cells,$color){
        global $objPHPExcel;
        $objPHPExcel->getActiveSheet()->getStyle($cells)->getFill()
        ->applyFromArray(array('type' => PHPExcel_Style_Fill::FILL_SOLID,
        'startcolor' => array('rgb' => $color)
        ));
    }
   
  cellColor('A3:F3', '000000');
	cellColor('A2:F2', '9966CC');

	
	
$rowCount = 3;  


//start of printing column names as names of MySQL fields  

 $column = 'A';

for ($i = 0; $i < mysql_num_fields($result); $i++)  

{
    $objPHPExcel->getActiveSheet()->setCellValue($column.$rowCount, mysql_field_name($result,$i));
    $column++;
}

//end of adding column names  
//start while loop to get data  

$rowCount = 4;  

while($row = mysql_fetch_row($result))  

{  
    $column = 'A';

   for($j=0; $j<mysql_num_fields($result);$j++)  
    {  
        if(!isset($row[$j]))  

            $value = NULL;  

        elseif ($row[$j] != "")  

            $value = strip_tags($row[$j]);  

        else  

            $value = "";  


        $objPHPExcel->getActiveSheet()->setCellValue($column.$rowCount, $value);
        $column++;
    }  

    $rowCount++;
	$objPHPExcel->getActiveSheet()->getStyle(
    'A3:' . 
    $objPHPExcel->getActiveSheet()->getHighestColumn() . 
    $objPHPExcel->getActiveSheet()->getHighestRow()
)->applyFromArray($styleArray);

} 

// Redirect output to a client’s web browser (Excel5) 
header('Content-Type: application/vnd.ms-excel'); 
header('Content-Disposition: attachment;filename="'.$filename.'".xls"'); 
header('Cache-Control: max-age=0'); 
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); 
$objWriter->save('php://output');
?>
&#13;
&#13;
&#13;

非常感谢您的任何帮助。我曾试图导出大量(超过10,000条)的记录。虽然我试图导出它开始挂起,最后它显示超时错误。请帮我解决这个问题。谢谢!

2 个答案:

答案 0 :(得分:1)

请在代码的第二行添加以下行:

set_time_limit(0);

它具有禁用脚本超时的作用。 ;)

答案 1 :(得分:0)

标准评论适用:

设置

$worksheet as $objPHPExcel->getActiveSheet();

在循环之外,然后使用

$worksheet->setCellValue($column.$rowCount, $value);

效率更高,因为它每次迭代都会保存对$objPHPExcel->getActiveSheet()的调用


如果某个单元格为空,那么请不要为该单元格设置值。这样可以节省在PHPExcel中实际创建单元格的性能开销,节省内存并减少生成文件的整体文件大小


移动

$objPHPExcel->getActiveSheet()->getStyle(
'A3:' . 
$objPHPExcel->getActiveSheet()->getHighestColumn() . 
$objPHPExcel->getActiveSheet()->getHighestRow()
)->applyFromArray($styleArray);

在您关闭行循环之后,因为您每行都冗余地执行它


尽量避免在列

上使用autosize
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);

如果可能的话

这是一项非常昂贵的操作,因为PHPExcel必须通过循环遍历该列中的每个单元格来计算每个自动调整列的实际大小,以确定最大大小应该是多少。如果您可以使用不需要计算的固定宽度列,那么它会快得多