使用PHPExcel插入进行导出需要很长时间

时间:2019-02-07 10:41:11

标签: php mysql

我面临的问题是代码生成Excel工作表所需的时间太长。因此,有时服务器会崩溃。在我的代码中,我正在运行两个循环。在Excel中也有80多个字段正在打印。请帮我离开这里,如何减少时间消耗?

这是我的代码

require("db_connect.php");
$con = db_connect();
if(isset($_REQUEST['project_id']))
$project_id =$_REQUEST['project_id'];
else
$project_id = '';

$file_name = $_GET['filename'];
//echo $file_name; exit;
error_reporting(E_ALL);
include 'Classes/PHPExcel.php';
include 'Classes/PHPExcel/IOFactory.php';


set_time_limit (0);

$cacheMethod = PHPExcel_CachedObjectStorageFactory:: cache_to_phpTemp;

$cacheSettings = array( 'memoryCacheSize' => '8MB');

PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);

$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);

$sql  = "select ";
$sql .="pm.start_date,pm.end_date,pm.tag_id,pdetails.circle_id,pm.project_name  ";
$sql .=", ";
$sql .="pdetails.site_id_A,pdetails.site_name_A,pdetails.site_id_B,pdetails.site_name_B,pdetails.ref_id,pdetails.link_id,pdetails.site_type,pdetails.sow_id,pdetails.uom_id,pdetails.quantity,pdetails.rate,pdetails.amount,pdetails.allocation_date,pdetails.installation_date,pdetails.integration_date,pdetails.swapping_date,pdetails.at_date,pdetails.at_status,pdetails.payment_term_id,pdetails.first_mile_amt ,pdetails.second_mile_amt,pdetails.third_mile_amt,pdetails.done_by,pdetails.team_leader_name,pdetails.team_contact_no,pdetails.remarks,pdetails.id as lookupid,pdetails.project_id,pdetails.po_number,pdetails.po_shipment_number as shipment_no,pdetails.po_shipment_combined_no ";
//$sql .= ",pmile.po_number,pmile.po_shipment_number,pmile.po_shipment_combined_no ";
$sql .= "from project_details_master as pdetails ";
$sql .= "inner join project_milestone_master as pmile ";
$sql .= "on pdetails.id=pmile.lookup_id ";
$sql .= "inner join project_master as pm ";
$sql .= "on pm.id=pmile.project_id ";


if(!empty($project_id))
$sql .= " and pm.id in($project_id) ";
$sql .= "group by pdetails.id ";
//echo $sql;
//exit;
$results=mysqli_query($con,$sql);
$row = mysqli_fetch_assoc($results);

$row_count=mysqli_num_rows($results);

if($row_count == 0){
    $file_path = "./excel_export/";
    $csv_handler = fopen ($file_path.$file_name,'w');
    fwrite ($csv_handler,'No record found');
    fclose ($csv_handler);

    //echo "No record found";
    exit;
}



$arr_key = array_keys($row);


$c=1;
$sheet = $objPHPExcel->getActiveSheet();
$sheet->SetCellValue('A1', 'SL NO');
foreach ($arr_key as $kv) {        
 $sheet->setCellValueByColumnAndRow($c, 1, $kv);
  $c++;
}

$mile_arr_heading = array('mile_stone','milestone_title','invoice_number','invoice_sent_date','invoice_status','tds_deduction','other_deduction_1','other_deduction_2','GST Amount','payment_status','clearance_date','payment_received'); 
$v2= $c;
for($i=1;$i<=3;$i++){
    for($p=0;$p<count($mile_arr_heading);$p++) { 
        $kv2 = $mile_arr_heading[$p];
        $sheet->setCellValueByColumnAndRow($v2,1, $kv2);
        $v2++;
    }
}


$results=mysqli_query($con,$sql);
$count = 0;
$rowcount = 2;
if($row_count>0){
    while($row= mysqli_fetch_assoc($results)){
        $col=1;
        $count++;
        $sheet->setCellValueByColumnAndRow(0,$rowcount,$count);
        foreach($row as $key=>$value){
            if($key=='at_status'){
                    if($value == 1)
                        $value = "Allocated";
                    if($value == 2)
                        $value = "Pending";
                    if($value == 3)
                        $value = "Work in Progress";
                    if($value == 4)
                        $value = "Completed";
                }    
        $f_val =  foreign_tble_val($key,$value);
        if($f_val == '')
 $sheet->setCellValueByColumnAndRow($col,$rowcount,$value); 
         else
 $sheet->setCellValueByColumnAndRow($col,$rowcount,$f_val);  


        $pro = $row['project_id'];
        $lkup = $row['lookupid'];


        $arr_mile = array("m1","m2","m3");
        $res = get_miestone_details($pro,$lkup);
        if(count($res)>0){
            $col2 = $col+1;
            for($i=0;$i<count($arr_mile);$i++){
                $mile_arr = $res[$arr_mile[$i]];
                if(count($mile_arr)>0){
                    foreach($mile_arr as $key2=>$value2){
                        if($key2=='invoice_status')                    {
                        if($value2 == 2)
                        $value2 = "Pending";
                        if($value2 == 3)
                        $value2 = "Cleared";
                        }
                        if($key2=='payment_status')                    {
                        if($value2 == 1)
                        $value2 = "Pending";
                        if($value2 == 2)
                        $value2 = "Cleared";
                        }    
                        $sheet->setCellValueByColumnAndRow($col2,$rowcount, $value2);
                        $col2++;
                    }
                }else{
                    $col2 = $col2+11;
                }
            }
        }


        if( $row['done_by'] == 'Vendor' ){
            $col3 = 70;
            $sql3 = "select vendor_id  ,vendor_sow_id,vendor_po_rate,po_number,invoice_number ";
            $sql3 .="from project_vendor_details ";
            $sql3 .="where project_id = '".$pro."' and 
            project_lookup_id = '".$lkup."'  ";
            //echo $sql3; exit;

            $query3=mysqli_query($con,$sql3);
            $v= $v2;
            while($row3=mysqli_fetch_assoc($query3)){
                foreach($row3 as $key3=>$value3){
            $sheet->setCellValueByColumnAndRow($v,1, $key3);
                $f_val =  foreign_tble_val($key3,$value3);
                if($f_val == '')
                $sheet->setCellValueByColumnAndRow($col3,$rowcount, $value3);
                else
                $sheet->setCellValueByColumnAndRow($col3,$rowcount, $f_val);    
                $col3++;
                $v++;
                }

            }
            $v=0;
        } 

        $col++;


        } 

        $rowcount++;
        $col=1;
    }
    //$sheet->getColumnDimension('E')->setVisible(false);
    $sheet->getColumnDimension('AG')->setVisible(false);
    $sheet->getColumnDimension('AH')->setVisible(false);


    $sheet->setTitle('Simple');



$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$file_path = "./excel_export/";
$filename=$file_name;
$path = $file_path.$filename;

$objWriter->save($path);
exit;
}
function get_miestone_details($project_id,$lookup_id){
    $arr_mile = array("m1","m2","m3");
    $con = db_connect();
    $sql2 = "select mile_stone,milestone_title,invoice_number,invoice_sent_date,invoice_status,tds_deduction,other_deduction_1,other_deduction_2,additional_payment_bonus,payment_status,clearance_date,payment_received ";
    $sql2 .="from project_milestone_master ";
    $sql2 .="where project_id = '".$project_id."' and lookup_id = '".$lookup_id."' and mile_stone in('m1','m2','m3') ";
    //echo $sql2; exit;
    $query2= mysqli_query($con,$sql2);
    $num2  = mysqli_num_rows($query2);
    //echo $num2; exit;
    $res = array();
    if($num2 > 0 ){
        while($row2=mysqli_fetch_assoc($query2)){
            $res[$row2['mile_stone']] = $row2;

        }
    }
    for($i=0;$i<count($arr_mile);$i++){
        if(!array_key_exists($arr_mile[$i],$res)){
            $res[$arr_mile[$i]] = array();
        }
    }
    return $res;


} 

0 个答案:

没有答案