添加边框需要太长时间才能加载

时间:2014-10-05 02:13:36

标签: phpexcel

不确定这是否正常但是我的PHPExcel输出了大约400条记录并且我创建了一个复杂的设置(PHPExcel help - How to Export data with PHPexcel in an custom table layout)但是当我尝试在几个单元格周围添加边框时,我的输出需要45秒到5分钟。有没有比下面显示的更好的方法可以提供更好的性能?

//Border settings
        $borders = array(
      'borders' => array(
        'inside'     => array(
          'style' => PHPExcel_Style_Border::BORDER_THIN,
          'color' => array(
            'argb' => 'FFFF0000'
          )
        ),
        'outline' => array(
          'style' => PHPExcel_Style_Border::BORDER_THIN,
          'color' => array(
             'argb' => 'FFFF0000'
          )
        )
      )
    );

    $sheet->getStyle('B'.$rows.':E5'.($rows+11))->applyFromArray($borders); 

这是我的完整代码,如果您有任何建议可以提高我的导出性能,请告诉我。任何反馈都非常感谢。

<?php
/** PHPExcel */  
require_once '/Excelphp/Classes/PHPExcel.php';  

// Create new PHPExcel object  
$objPHPExcel = new PHPExcel();  
$rows=2;
$sheet=$objPHPExcel->getActiveSheet(); 

// Set properties  
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")  
->setLastModifiedBy("Maarten Balliauw")  
->setTitle("Office 2007 XLSX Test Document")  
->setSubject("Office 2007 XLSX Test Document")  
->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")  
->setKeywords("office 2007 openxml php")  
->setCategory("Test result file");  

//This is the hard coded *non dynamic* cell formatting
    $sheet->getColumnDimension('A')->setWidth(5);  
    $sheet->getColumnDimension('B')->setWidth(15);  
    $sheet->getColumnDimension('C')->setWidth(50);  
    $sheet->getColumnDimension('D')->setWidth(50);  
    $sheet->getSheetView()->setZoomScale(90);

    //Font Setting for the Support group title.
    $Support_team = array(
    'font'  => array(
        'bold'  => true,
        'color' => array('rgb' => '808080'),
        'size'  => 22,
        'name'  => 'Arial'
    ));

    //Font settings for the header cells only.
    $headers = array(
    'font'  => array(
        'bold'  => true,
        'color' => array('rgb' => '808080'),
        'size'  => 12,
        'name'  => 'Arial'
    ));


//  SQl database connections
    $db = mysql_connect("localhost", "IMC_COE2", "IMC123");  
    mysql_select_db("IMC_COE2",$db);  

    $sql="select client, team_name,support_team_prime,prime_comments,support_team_backup,backup_comments,escalation1,escalation1_comments,escalation2,escalation2_comments,escalation3,escalation3_comments,escalation4,escalation4_comments,note from tbl_address ORDER BY team_name";  
    $result=mysql_query($sql);  
        $numrows=mysql_num_rows($result);  
        if ($numrows>0)  
        {  
            while($data=mysql_fetch_array($result))  
            {  
//Cell Merging 
$sheet
    ->mergeCells('B'.$rows.':D'.$rows)  
    ->mergeCells('B'.($rows+2).':D'.($rows+2))  
    ->mergeCells('B'.($rows+5).':D'.($rows+5))  
    ->mergeCells('B'.($rows+10).':D'.($rows+10))  
    ->mergeCells('C'.($rows+1).':D'.($rows+1))
    ->mergeCells('B'.($rows+11).':D'.($rows+11)); 

// Add some data  
$objPHPExcel->setActiveSheetIndex(0)  
    ->setCellValue('B'.($rows+1), 'Client:')
    ->setCellValue('B'.($rows+2), 'Support group contacts')     
    ->setCellValue('B'.($rows+3), 'Prime:')
    ->setCellValue('B'.($rows+4), 'Backup:')
    ->setCellValue('B'.($rows+5), 'Escalations') 
    ->setCellValue('B'.($rows+6), 'Escalation 1:') 
    ->setCellValue('B'.($rows+7), 'Escalation 2:') 
    ->setCellValue('B'.($rows+8), 'Escalation 3:') 
    ->setCellValue('B'.($rows+9), 'Escalation 4:') 
    ->setCellValue('B'.($rows+10), 'Notes'); 

//Format the hardcoded text 
$sheet->getStyle('B'.$rows)->applyFromArray($Support_team);
$sheet->getStyle('B'.($rows+2))->applyFromArray($headers);      
$sheet->getStyle('B'.($rows+5))->applyFromArray($headers);  
$sheet->getStyle('B'.($rows+10))->applyFromArray($headers); 

//Row height adjustments
$sheet->getRowDimension($rows+3)->setRowHeight(60);
$sheet->getRowDimension($rows+4)->setRowHeight(60);
$sheet->getRowDimension($rows+6)->setRowHeight(60);
$sheet->getRowDimension($rows+7)->setRowHeight(60);
$sheet->getRowDimension($rows+8)->setRowHeight(60);
$sheet->getRowDimension($rows+9)->setRowHeight(60);
$sheet->getRowDimension($rows+11)->setRowHeight(100);   


//Cell Wraptext
$sheet->getStyle('C'.($rows+1).':D'.($rows+1))->getAlignment()->setWrapText(true);
$sheet->getStyle('C'.($rows+3).':D'.($rows+3))->getAlignment()->setWrapText(true);
$sheet->getStyle('C'.($rows+4).':D'.($rows+4))->getAlignment()->setWrapText(true);
$sheet->getStyle('C'.($rows+6).':D'.($rows+6))->getAlignment()->setWrapText(true);
$sheet->getStyle('C'.($rows+7).':D'.($rows+7))->getAlignment()->setWrapText(true);
$sheet->getStyle('C'.($rows+8).':D'.($rows+8))->getAlignment()->setWrapText(true);
$sheet->getStyle('C'.($rows+9).':D'.($rows+9))->getAlignment()->setWrapText(true);
$sheet->getStyle('B'.($rows+11).':D'.($rows+11))->getAlignment()->setWrapText(true);

//Background color on cells 
$sheet->getStyle('B'.$rows.':D'.$rows)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB('FF9BC2E6');
$sheet->getStyle('B'.($rows+2).':D'.($rows+2))->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB('FF9BC2E6');
$sheet->getStyle('B'.($rows+5).':D'.($rows+5))->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB('FF9BC2E6');
$sheet->getStyle('B'.($rows+10).':D'.($rows+10))->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB('FF9BC2E6');
$sheet->getStyle('B'.($rows+1))->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB('FFE6F1FA');
$sheet->getStyle('B'.($rows+3))->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB('FFE6F1FA');
$sheet->getStyle('B'.($rows+4))->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB('FFE6F1FA');
$sheet->getStyle('B'.($rows+6))->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB('FFE6F1FA');
$sheet->getStyle('B'.($rows+7))->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB('FFE6F1FA');
$sheet->getStyle('B'.($rows+8))->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB('FFE6F1FA');
$sheet->getStyle('B'.($rows+9))->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB('FFE6F1FA');         



//This section is the actual data imported from the SQL database *don't touch*
$objPHPExcel->setActiveSheetIndex(0)              
    ->setCellValue('C'.($rows+1), $data['client']) //this will give cell C2. 
    ->setCellValue('B'.$rows, $data['team_name']) // this will give cell B2 
    ->setCellValue('C'.($rows+3), $data['support_team_prime']) //this will give C5 
    ->setCellValue('D'.($rows+3), $data['prime_comments'])  // This will give D5
    ->setCellValue('C'.($rows+4), $data['support_team_backup'])  //This will give C6
    ->setCellValue('D'.($rows+4), $data['backup_comments']) //This will give D6  
    ->setCellValue('C'.($rows+6), $data['escalation1'])//THis will give you C8
    ->setCellValue('D'.($rows+6), $data['escalation1_comments'])//This will give you D8
    ->setCellValue('C'.($rows+7), $data['escalation2'])//This will give you C9
    ->setCellValue('D'.($rows+7), $data['escalation2_comments'])//This will give you D9
    ->setCellValue('C'.($rows+8), $data['escalation3'])//This will give you C10
    ->setCellValue('D'.($rows+8), $data['escalation3_comments'])//This will give you D10
    ->setCellValue('C'.($rows+9), $data['escalation4'])//This will give you C11
    ->setCellValue('D'.($rows+9), $data['escalation4_comments'])//This will give you D11
    ->setCellValue('B'.($rows+11), $data['note']); //This will give you B13

$rows+=14; 
            }  
        }          

// Rename sheet  
$sheet->setTitle('Directory Tool Full dump');

// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);

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

1 个答案:

答案 0 :(得分:0)

感谢MAX带来了很大的不同。这是(&#39; B&#39;。$ rows。&#39;:E5&#39;。($ rows + 11))行使其减速。一旦我将其更改为(&#39; B&#39;。$ rows。&#39;:E&#39;。($ rows + 11)),花了不到一分钟。谢谢MAX。标记为已解决。

//Border settings
        $borders = array(
      'borders' => array(
        'inside'     => array(
          'style' => PHPExcel_Style_Border::BORDER_THIN,
          'color' => array(
            'argb' => 'FFFF0000'
          )
        ),
        'outline' => array(
          'style' => PHPExcel_Style_Border::BORDER_THIN,
          'color' => array(
             'argb' => 'FFFF0000'
          )
        )
      )
    );

    $sheet->getStyle('B'.$rows.':E'.($rows+11))->applyFromArray($borders);