使用PHPExcel

时间:2016-04-08 06:49:22

标签: php sql excel phpexcel

我需要一个PHP代码解决方案来将选定的SQL查询数据转换为excel工作簿格式。我已经实现了生成excel工作簿文件的代码。但结果不应该是我需要的excel格式。

我需要的Excel报告格式
 excel report

我成功的SQL查询结果

SQL result

我的PHP编码

$get_mode = "SELECT
                sh_sso.showrooms.showroom_id,
                sh_sso.showrooms.showroom_name,
                Count(abans_crm.customers.purchase_type) AS mode_count,
                abans_crm.customers.purchase_date,
                abans_crm.pay_mode.pay_mode
                FROM
                        sh_sso.showrooms
                INNER JOIN abans_crm.customers ON sh_sso.showrooms.showroom_id = abans_crm.customers.location_id
                INNER JOIN abans_crm.pay_mode ON abans_crm.customers.purchase_type = abans_crm.pay_mode.mode_id
                WHERE DATE(abans_crm.customers.purchase_date) >= '$fromDate' AND DATE(abans_crm.customers.purchase_date) <= '$toDate'
                GROUP BY
                abans_crm.pay_mode.pay_mode,
                sh_sso.showrooms.showroom_name";

    $result = mysql_query($get_mode) or die(mysql_error());;

    $row = 6; 
    while($row_mode = mysql_fetch_array($result)){    

              $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A'.$row, $row_mode['showroom_name']);   

              if($row_mode['pay_mode'] == 'Cash'){
                    if($row_mode['mode_count'] > 1){
                     $objPHPExcel->setActiveSheetIndex(0)->setCellValue('B'.$row,$row_mode['mode_count']);
                    }
                    elseif($row_mode['mode_count'] == 1){
                      $objPHPExcel->setActiveSheetIndex(0)->setCellValue('B'.$row,'1');   
                    }
                    else {
                      $objPHPExcel->setActiveSheetIndex(0)->setCellValue('B'.$row,'0');   
                    }
              }
              if($row_mode['pay_mode'] == 'Credit Card'){
                    if($row_mode['mode_count'] > 1){
                     $objPHPExcel->setActiveSheetIndex(0)->setCellValue('C'.$row,$row_mode['mode_count']);
                    }
                    elseif($row_mode['mode_count'] == 1){
                      $objPHPExcel->setActiveSheetIndex(0)->setCellValue('C'.$row,'1');   
                    }
                    else {
                      $objPHPExcel->setActiveSheetIndex(0)->setCellValue('C'.$row,'0');   
                    } 
              }
              if($row_mode['pay_mode'] == 'Hire Purchase'){
                if($row_mode['mode_count'] > 1){
                     $objPHPExcel->setActiveSheetIndex(0)->setCellValue('D'.$row,$row_mode['mode_count']);
                    }
                    elseif($row_mode['mode_count'] == 1){
                      $objPHPExcel->setActiveSheetIndex(0)->setCellValue('D'.$row,'1');   
                    }
                    else {
                      $objPHPExcel->setActiveSheetIndex(0)->setCellValue('D'.$row,'0');   
                }  
              }
              if($row_mode['pay_mode'] == 'Lease'){
                if($row_mode['mode_count'] > 1){
                     $objPHPExcel->setActiveSheetIndex(0)->setCellValue('E'.$row,$row_mode['mode_count']);
                    }
                    elseif($row_mode['mode_count'] == 1){
                      $objPHPExcel->setActiveSheetIndex(0)->setCellValue('E'.$row,'1');   
                    }
                    else {
                      $objPHPExcel->setActiveSheetIndex(0)->setCellValue('E'.$row,'0');   
                } 
              }
              ++$row;
            }
   //}
//genarate report data
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="test-report 2016-04-3.xls"');
header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');

作为示例   突出显示的记录数据应该是单行及其总数。在这种情况下,TOTAL应为37

0 个答案:

没有答案