使用php在excel中导出报告不在服务器上工作但在localhost上工作?

时间:2016-01-31 10:14:53

标签: php sql excel

我正在尝试使用php导出报告。它可以在localhost中正常工作,但是当我在服务器上传时,它将无法正常工作,并提供如下所示的输出。(我使用的所有变量都没有在这里显示为全局因此在变量声明中没有问题。) This is the image which i am getting while trying to export report

$arrayissue = array();

            $count = 0;
            $screenshot_name = '';
            $productid = (string) filter_input(INPUT_GET, 'productid');


            $cycle = $_SESSION['Cycle'];
            $objPHPExcel = new PHPExcel();
            $query2 = "SELECT Cycle_name FROM cycle_master WHERE Cycle_id='$cycle'";
            $result2 = $conn->query($query2);
            $row12 = mysqli_fetch_array($result2);


            $ews = $objPHPExcel->getSheet(0);
            $ews->setCellValue('a1', 'This is the Result For : ' . $row12[0]);
            $rowCount = 2;
            $title = 'a1:s1';
            $header = 'a2:s2';

            $objPHPExcel->getActiveSheet()->getStyle($title)->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB('A9F5E1');

            $objPHPExcel->getActiveSheet()->getStyle($header)->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB('FFFF00');

            $style = array(
                'font' => array('bold' => true,),
                'alignment' => array('horizontal' => \PHPExcel_Style_Alignment::HORIZONTAL_CENTER,),
            );

            $objPHPExcel->getActiveSheet()->getStyle($header)->applyFromArray($style);
            $objPHPExcel->getActiveSheet()->getStyle(
                    'A2:' .
                    $objPHPExcel->getActiveSheet()->getHighestColumn() .
                    $objPHPExcel->getActiveSheet()->getHighestRow()
            )->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
            $objPHPExcel->getDefaultStyle()->getAlignment()->setWrapText(true);
            $styleArray = array(
                'font' => array(
                    'bold' => FALSE,
                    // 'color' => array('rgb' => 'FF0000'),
                    'size' => 10,
                    'name' => 'Calibri'
            ));
            for ($col = 'A'; $col != 'S'; $col++) {

                $objPHPExcel->getActiveSheet()->getColumnDimension($col)->setWidth(20);
                $objPHPExcel->getActiveSheet()->getStyle($col)->applyFromArray($styleArray);
            }



            $queryforissue = "Select Issue_name from tmtool.project_issue_master where Project_id IN(Select Project_id from product_master Where Product_id='$productid')";
            $resultforissue = mysqli_query($conn, $queryforissue) or die(mysqli_error($conn));



            $countforissue = 0;
            $customeTitle = array();
            $customTitle1 = array('Execution ID', 'TC ID', 'Parent Site', 'Redirection Site');
            $customTitle2 = array('Total Error Observed Count', 'Issue(Pass/Fail)', 'Circle Name', 'Mobile Number', 'Date', 'Time', 'Product Name', 'Screen Shot Name', 'Comment', 'Actual Submitted Date');
            while ($rowissue = mysqli_fetch_array($resultforissue)) {
                array_push($arrayissue, $rowissue['Issue_name']);
                $countforissue++;
            }
            //print_r($arrayissue);

            $customTitle3 = array_merge($customTitle1, $arrayissue);
            $customTitle = array_merge($customTitle3, $customTitle2);
            print_r($customTitle);

            $alph = 'A';
            foreach ($customTitle as $value) {
                $objPHPExcel->getActiveSheet()->SetCellValue($alph . $rowCount, $value);
                $alph++;
            }
            $rowCount++;

            $queryresult = "SELECT testcase_master.`Testcase_id`,CONVERT(SUBSTRING_INDEX(testcase_master.`Testcase_id`,'_', -1),UNSIGNED INTEGER) as num ,testcase_master.`Testcase_title`,testcase_master.`Subscriber_type`,defect_master.`Priority`,defect_master.`Category`,test_result_url.`Test_id`,test_result_url.`Assign_to`,test_result_url.`Child_site`,test_result_url.`Circle_name`,test_result_url.`Performed_date`,test_result_url.`Status`,test_result_url.`Comment`,test_result_url.`UrlProduct_name`,test_result_url.`Device_used`,test_result_url.`Simcard_no`,test_result_url.`Time` As Time_pass,defect_master.`Defect_id`,defect_master.`Defect_title`,defect_master.`Month`,defect_master.`Created_date`,defect_master.`Week`,defect_master.`Time`,defect_master.`Mobile`,defect_master.`Comment` as dcomment FROM tmtool.test_result_url LEFT JOIN tmtool.testcase_master  ON test_result_url.`Testcase_id`=testcase_master.`Testcase_id` LEFT JOIN tmtool.defect_master ON test_result_url.`Defect_id`=defect_master.`Defect_id` WHERE testcase_master.`Product_id` = '$productid' AND (test_result_url.`Performed_date` BETWEEN '$startdate' AND '$enddate') AND testcase_master.`Testcase_id` IN (SELECT Testcase_id FROM assigned_testsuite_testcase WHERE Testsuite_id IN (SELECT Testsuite_id FROM testsuite_master WHERE Cycle_id = '$cycle')) order by num";
            //$queryresult = "SELECT testcase_master.`Testcase_id`,testcase_master.`Testcase_title`,defect_master.`dcomment`,defect_master.`Defect_title`,defect_master.`Created_date`,defect_master.`Mobile`,defect_master.`Time`,test_result_url.`Test_id`, FROM tmtool.test_result_url LEFT JOIN tmtool.testcase_master ON test_result_url.`Testcase_id` = testcase_master.`Testcase_id` LEFT JOIN tmtool.defect_master ON test_result_url.`Defect_id` = defect_master.`Defect_id` WHERE testcase_master.`Product_id` = '$productid' AND testcase_master.`Testcase_id` IN (SELECT Testcase_id FROM assigned_testsuite_testcase WHERE Testsuite_id IN (SELECT Testsuite_id FROM testsuite_master WHERE Cycle_id = '$cycle'))";
            //$queryresult = "SELECT testcase_master.`Testcase_id`,testcase_master.`Testcase_title`,defect_master.`Comment`,defect_master.`Defect_title`,defect_master.`Created_date`,defect_master.`Mobile`,defect_master.`Time`,test_result_url.* FROM tmtool.test_result_url LEFT JOIN tmtool.testcase_master ON test_result_url.`Testcase_id` = testcase_master.`Testcase_id` LEFT JOIN tmtool.defect_master ON test_result_url.`Defect_id` = defect_master.`Defect_id` WHERE testcase_master.`Product_id` = '$productid' AND testcase_master.`Testcase_id` IN (SELECT Testcase_id FROM assigned_testsuite_testcase WHERE Testsuite_id IN (SELECT Testsuite_id FROM testsuite_master WHERE Cycle_id = '$cycle'))";
            $resultquery = mysqli_query($conn, $queryresult) or die(mysqli_error($conn));



            while ($row = mysqli_fetch_array($resultquery)) {
                $objPHPExcel->getActiveSheet()->SetCellValue('A' . $rowCount, $row['Test_id']);
                $objPHPExcel->getActiveSheet()->SetCellValue('B' . $rowCount, $row['Testcase_id']);
                $objPHPExcel->getActiveSheet()->SetCellValue('C' . $rowCount, $row['Testcase_title']);
                $objPHPExcel->getActiveSheet()->SetCellValue('D' . $rowCount, $row['Child_site']);


                for ($i = 0, $alpha2 = 'E'; $i < $countforissue; $i++, $alpha2++) {

                    $querygetResult = "Select $arrayissue[$i] from test_result_url where Test_id='$row[Test_id]'";
                    $getResult = mysqli_query($conn, $querygetResult) or die('Error querying database');
                    $rowResult = mysqli_fetch_array($getResult);
                    if($arrayissue[$i] == 'Landing_page_issue')
                    {
                        if($rowResult[0] == 'Fail')
                        {
                            $rowResult[0]='Yes';
                        }
                        else
                        {
                            $rowResult[0]='No';
                        }
                    }
                    $objPHPExcel->getActiveSheet()->SetCellValue($alpha2 . $rowCount, $rowResult[0]);
                    if ($rowResult[0] == 'Fail') {
                        $count++;
                    }
                }



                $objPHPExcel->getActiveSheet()->SetCellValue($alpha2 . $rowCount, $count);

                $alpha2++;
                $objPHPExcel->getActiveSheet()->SetCellValue($alpha2 . $rowCount, $row['Status']);
                $alpha2++;
                $objPHPExcel->getActiveSheet()->SetCellValue($alpha2 . $rowCount, $row['Circle_name']);
                $alpha2++;

                $elsealpha = $alpha2;
                $lastalpha = $alpha2;
                for ($j = 0; $j < 6; $j++) {
                    $lastalpha++;
                }


                if ($row['Status'] == 'Fail') {
                    $objPHPExcel->getActiveSheet()->SetCellValue($alpha2 . $rowCount, $row['Mobile']);
                    $alpha2++;
                    $objPHPExcel->getActiveSheet()->SetCellValue($alpha2 . $rowCount, $row['Created_date']);
                    $alpha2++;
                    $objPHPExcel->getActiveSheet()->SetCellValue($alpha2 . $rowCount, $row['Time']);
                    $alpha2++;
                    $objPHPExcel->getActiveSheet()->SetCellValue($alpha2 . $rowCount, $row["UrlProduct_name"]);
                    $alpha2++;
                    $objPHPExcel->getActiveSheet()->SetCellValue($alpha2 . $rowCount, $row["Defect_title"]);
                    $alpha2++;
                    $objPHPExcel->getActiveSheet()->SetCellValue($alpha2 . $rowCount, $row["dcomment"]);
                    $alpha2++;
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue($elsealpha . $rowCount, $row['Simcard_no']);
                    $elsealpha++;
                    $objPHPExcel->getActiveSheet()->SetCellValue($elsealpha . $rowCount, $row['Performed_date']);
                    $elsealpha++;
                    $objPHPExcel->getActiveSheet()->SetCellValue($elsealpha . $rowCount, $row['Time_pass']);
                    $elsealpha++;
                    $elsealpha++;
                    $elsealpha++;
                    $objPHPExcel->getActiveSheet()->SetCellValue($elsealpha . $rowCount, $row['Comment']);
                }



                $objPHPExcel->getActiveSheet()->SetCellValue($lastalpha . $rowCount, $row['Performed_date']);
                $alpha2 = '';
                $elsealpha = '';
                $count = 0;
                $rowCount++;
            }
            unset($count);
            unset($lastalpha);
            unset($elsealpha);
            unset($alpha2);

            $objPHPExcel->getActiveSheet()->setTitle("Product Wise");
            header('Content-Type: application/vnd.openxmlformats-   officedocument.spreadsheetml.sheet');
            header('Content-Disposition: attachment;filename="TestResults.xlsx"');
            header('Cache-Control: max-age=0');

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

1 个答案:

答案 0 :(得分:0)

你开始打印一些变量,强制一个header()并破坏excel文件。

删除print_r($ customTitle);