Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHPExcel将SQL数据库中的数据提取到excel报表中_Php_Sql_Excel_Phpexcel - Fatal编程技术网

使用PHPExcel将SQL数据库中的数据提取到excel报表中

使用PHPExcel将SQL数据库中的数据提取到excel报表中,php,sql,excel,phpexcel,Php,Sql,Excel,Phpexcel,我需要一个PHP代码解决方案,将选定的SQL查询数据转换为excel工作簿格式。我已经实现了生成excel工作簿文件的代码。但结果不应该与我需要的excel格式一致 我需要的Excel报告格式 我成功的SQL查询结果 我的PHP编码 $get_mode = "SELECT sh_sso.showrooms.showroom_id, sh_sso.showrooms.showroom_name, C

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

我需要的Excel报告格式

我成功的SQL查询结果

我的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');
$get\u mode=“选择
sh_sso.showrooms.showroom_id,
sh_sso.showrooms.showroom_name,
计数(abans\u crm.customers.purchase\u type)作为模式计数,
abans\u crm.customers.purchase\u日期,
abans\u crm.pay\u mode.pay\u mode
从…起
上海展览馆
在sh\u sso.showrooms.showroom\u id=abans\u crm.customers.location\u id上内部加入abans\u crm.customers
abans\u crm.customers.purchase\u type=abans\u crm.pay\u mode\u id上的内部加入abans\u crm.pay\u mode

WHERE DATE(abans\u crm.customers.purchase\u DATE)>=“$fromDate”和DATE(abans\u crm.customers.purchase\u DATE)如何将展厅id等数值插入一个数组?