使用php在excel中导出报告时,它在本地工作但不在服务器上?

时间:2016-03-23 09:20:16

标签: php mysql

我正在尝试使用php导出报告。它可以在localhost中正常工作但是当我在服务器上运行时它不起作用并给出如下输出。(我使用的所有未在此处显示的变量都被声明为全局变量因此在变量声明中没有问题。) 输出:

enter image description here

我尝试的代码是:

 $arrayissue = array();

            $count = 0;
            $screenshot_name = '';



            $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:aj1';
            $header = 'a2:aj2';

            $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'
            ));
            $limit = $objPHPExcel->getActiveSheet()->getHighestDataColumn();
            echo $limit;
            for ($col = 'A'; $col != $limit; $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', 'Partner Name', 'Product Name', 'Test URL', 'Issue');
            $customTitle2 = array('Circle Name', 'Mobile Number', 'Date', 'Time', 'Screen Shot Name', 'Comment');
            while ($rowissue = mysqli_fetch_array($resultforissue)) {
                array_push($arrayissue, $rowissue['Issue_name']);
                $countforissue++;
            }


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


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

            if ($startdate != '' && $enddate != '') {
                $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`,testcase_master.`Precondition`,testcase_master.`Activation_mode`,test_result_url.`Test_id`,test_result_url.`Assign_to`,test_result_url.`Circle_name`,test_result_url.`Performed_date`,test_result_url.`Status`,test_result_url.`Comment`,test_result_url.`Screen_shot_name`,test_result_url.`Device_used`,test_result_url.`Simcard_no`,test_result_url.`Time`,test_result_url.`Updated_date`,test_result_url.`Updated_time`,test_result_url.`Updated_by`  FROM tmtool.test_result_url LEFT JOIN tmtool.testcase_master  ON test_result_url.`Testcase_id`=testcase_master.`Testcase_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";
            } else {
                $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`,testcase_master.`Precondition`,testcase_master.`Activation_mode`,test_result_url.`Test_id`,test_result_url.`Assign_to`,test_result_url.`Circle_name`,test_result_url.`Performed_date`,test_result_url.`Status`,test_result_url.`Comment`,test_result_url.`Screen_shot_name`,test_result_url.`Device_used`,test_result_url.`Simcard_no`,test_result_url.`Time`,test_result_url.`Updated_date`,test_result_url.`Updated_time`,test_result_url.`Updated_by`  FROM tmtool.test_result_url LEFT JOIN tmtool.testcase_master  ON test_result_url.`Testcase_id`=testcase_master.`Testcase_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')) order by num";
            }



            $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['Precondition']);
                $objPHPExcel->getActiveSheet()->SetCellValue('D' . $rowCount, $row['Activation_mode']);
                $objPHPExcel->getActiveSheet()->SetCellValue('E' . $rowCount, $row['Testcase_title']);
                $objPHPExcel->getActiveSheet()->SetCellValue('F' . $rowCount, $row['Status']);


                for ($i = 0, $alpha2 = 'G'; $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);

                    $objPHPExcel->getActiveSheet()->SetCellValue($alpha2 . $rowCount, $rowResult[0]);
                }

                $objPHPExcel->getActiveSheet()->SetCellValue($alpha2 . $rowCount, $row['Circle_name']);
                $alpha2++;
                $objPHPExcel->getActiveSheet()->SetCellValue($alpha2 . $rowCount, $row['Simcard_no']);
                $alpha2++;
                $objPHPExcel->getActiveSheet()->SetCellValue($alpha2 . $rowCount, $row['Performed_date']);
                $alpha2++;
                $objPHPExcel->getActiveSheet()->SetCellValue($alpha2 . $rowCount, $row['Time']);
                $alpha2++;
                $objPHPExcel->getActiveSheet()->SetCellValue($alpha2 . $rowCount, $row['Screen_shot_name']);
                $alpha2++;
                $objPHPExcel->getActiveSheet()->SetCellValue($alpha2 . $rowCount, $row['Comment']);


                $rowCount++;
            }

            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 :(得分:1)

找到自己的答案。

1)有一个echo语句,它将'A'发送到header.so删除echo
2)ob_end_clean()正在创建问题,当我删除该代码时它的工作正常。

感谢所有试图帮助我的人。