PHPExcel突然不起作用

时间:2014-02-14 16:25:03

标签: php html mysql phpexcel phpexcelreader

我的代码之前正在工作,但突然今天它在我更改了一些输入之后无法工作......最糟糕的是它在我点击提交按钮执行phpexcel代码之后不再下载...而且它只会直接打开to dreamweaver并发布错误....有人可以帮我这个。

错误:当我点击提交按钮生成一个excel报告时,它将直接打开Dreamweaver并显示以下内容。

<br />
<b>Warning</b>:  Header may not contain more than a single header, new line detected in <b>C:\xampp\htdocs\eloginko\interview.php</b> on line <b>342</b><br />
<!-- XLS binary data -->

当前代码:

<?php
    if(isset($_POST['send'])){
        include('connection.php');
        require_once 'Classes\PHPExcel\IOFactory.php';
        $filename = 'worksheet.xlsx';
        $position = $_POST['position'];
        $repcode = $_POST['repcode'];
        $town = $_POST['town'];
        $title = $repcode .'_'. $town .'_'. $position;

        $objReader = PHPExcel_IOFactory::createReader('Excel2007');
        $objReader->setReadDataOnly(true);


        $objPHPExcel = $objReader->load($filename);
        $objWorksheet = $objPHPExcel->getActiveSheet();
        $objWorksheet = $objPHPExcel->setActiveSheetIndex(0);


        $repcode1 = isset($_POST['repcode']) ? $_POST['repcode'] : "";
        $aic1 = isset($_POST['aic1']) ? $_POST['aic1'] : "";
        $name1 = isset($_POST['name1']) ? $_POST['name1'] : "";
        $optA1 = isset($_POST['optA1']) ? $_POST['optA1'] : "";
        $optB1 = isset($_POST['optB1']) ? $_POST['optB1'] : "";
        $optC1 = isset($_POST['optC1']) ? $_POST['optC1'] : "";
        $optD1 = isset($_POST['optD1']) ? $_POST['optD1'] : "";
        $total1 = isset($_POST['total1']) ? $_POST['total1'] : "";
        $remarks1 = 'N/A';

        $repcode2 = isset($_POST['repcode']) ? $_POST['repcode'] : "";
        $aic2 = isset($_POST['aic2']) ? $_POST['aic2'] : "";
        $name2 = isset($_POST['name2']) ? $_POST['name2'] : "";
        $optA2 = isset($_POST['optA2']) ? $_POST['optA2'] : "";
        $optB2 = isset($_POST['optB2']) ? $_POST['optB2'] : "";
        $optC2 = isset($_POST['optC2']) ? $_POST['optC2'] : "";
        $optD2 = isset($_POST['optD2']) ? $_POST['optD2'] : "";
        $total2 = isset($_POST['total2']) ? $_POST['total2'] : "";
        $remarks2 = 'N/A';

        $repcode3 = isset($_POST['repcode']) ? $_POST['repcode'] : "";
        $aic3 = isset($_POST['aic3']) ? $_POST['aic3'] : "";
        $name3 = isset($_POST['name3']) ? $_POST['name3'] : "";
        $optA3 = isset($_POST['optA3']) ? $_POST['optA3'] : "";
        $optB3 = isset($_POST['optB3']) ? $_POST['optB3'] : "";
        $optC3 = isset($_POST['optC3']) ? $_POST['optC3'] : "";
        $optD3 = isset($_POST['optD3']) ? $_POST['optD3'] : "";
        $total3 = isset($_POST['total3']) ? $_POST['total3'] : "";
        $remarks3 = 'N/A';

        $repcode4 = isset($_POST['repcode']) ? $_POST['repcode'] : "";
        $aic4 = isset($_POST['aic4']) ? $_POST['aic4'] : "";
        $name4 = isset($_POST['name4']) ? $_POST['name4'] : "";
        $optA4 = isset($_POST['optA4']) ? $_POST['optA4'] : "";
        $optB4 = isset($_POST['optB4']) ? $_POST['optB4'] : "";
        $optC4 = isset($_POST['optC4']) ? $_POST['optC4'] : "";
        $optD4 = isset($_POST['optD4']) ? $_POST['optD4'] : "";
        $total4 = isset($_POST['total4']) ? $_POST['total4'] : "";
        $remarks4 = 'N/A';

        $repcode5 = isset($_POST['repcode']) ? $_POST['repcode'] : "";
        $aic5 = isset($_POST['aic5']) ? $_POST['aic5'] : "";
        $name5 = isset($_POST['name5']) ? $_POST['name5'] : "";
        $optA5 = isset($_POST['optA5']) ? $_POST['optA5'] : "";
        $optB5 = isset($_POST['optB5']) ? $_POST['optB5'] : "";
        $optC5 = isset($_POST['optC5']) ? $_POST['optC5'] : "";
        $optD5 = isset($_POST['optD5']) ? $_POST['optD5'] : "";
        $total5 = isset($_POST['total5']) ? $_POST['total5'] : "";
        $remarks5 = 'N/A';

        $repcode6 = isset($_POST['repcode']) ? $_POST['repcode'] : "";
        $aic6 = isset($_POST['aic6']) ? $_POST['aic6'] : "";
        $name6 = isset($_POST['name6']) ? $_POST['name6'] : "";
        $optA6 = isset($_POST['optA6']) ? $_POST['optA6'] : "";
        $optB6 = isset($_POST['optB6']) ? $_POST['optB6'] : "";
        $optC6 = isset($_POST['optC6']) ? $_POST['optC6'] : "";
        $optD6 = isset($_POST['optD6']) ? $_POST['optD6'] : "";
        $total6 = isset($_POST['total6']) ? $_POST['total6'] : "";
        $remarks6 = 'N/A';

        $repcode7 = isset($_POST['repcode']) ? $_POST['repcode'] : "";
        $aic7 = isset($_POST['aic7']) ? $_POST['aic7'] : "";
        $name7 = isset($_POST['name7']) ? $_POST['name7'] : "";
        $optA7 = isset($_POST['optA7']) ? $_POST['optA7'] : "";
        $optB7 = isset($_POST['optB7']) ? $_POST['optB7'] : "";
        $optC7 = isset($_POST['optC7']) ? $_POST['optC7'] : "";
        $optD7 = isset($_POST['optD7']) ? $_POST['optD7'] : "";
        $total7 = isset($_POST['total7']) ? $_POST['total7'] : "";
        $remarks7 = 'N/A';

        $repcode8 = isset($_POST['repcode']) ? $_POST['repcode'] : "";
        $aic8 = isset($_POST['aic8']) ? $_POST['aic8'] : "";
        $name8 = isset($_POST['name8']) ? $_POST['name8'] : "";
        $optA8 = isset($_POST['optA8']) ? $_POST['optA8'] : "";
        $optB8 = isset($_POST['optB8']) ? $_POST['optB8'] : "";
        $optC8 = isset($_POST['optC8']) ? $_POST['optC8'] : "";
        $optD8 = isset($_POST['optD8']) ? $_POST['optD8'] : "";
        $total8 = isset($_POST['total8']) ? $_POST['total8'] : "";
        $remarks8 = 'N/A';

        $repcode9 = isset($_POST['repcode']) ? $_POST['repcode'] : "";
        $aic9 = isset($_POST['aic9']) ? $_POST['aic9'] : "";
        $name9 = isset($_POST['name9']) ? $_POST['name9'] : "";
        $optA9 = isset($_POST['optA9']) ? $_POST['optA9'] : "";
        $optB9 = isset($_POST['optB9']) ? $_POST['optB9'] : "";
        $optC9 = isset($_POST['optC9']) ? $_POST['optC9'] : "";
        $optD9 = isset($_POST['optD9']) ? $_POST['optD9'] : "";
        $total9 = isset($_POST['total9']) ? $_POST['total9'] : "";
        $remarks9 = 'N/A';

        $repcode10 = isset($_POST['repcode']) ? $_POST['repcode'] : "";
        $aic10 = isset($_POST['aic10']) ? $_POST['aic10'] : "";
        $name10 = isset($_POST['name10']) ? $_POST['name10'] : "";
        $optA10 = isset($_POST['optA10']) ? $_POST['optA10'] : "";
        $optB10 = isset($_POST['optB10']) ? $_POST['optB10'] : "";
        $optC10 = isset($_POST['optC10']) ? $_POST['optC10'] : "";
        $optD10 = isset($_POST['optD10']) ? $_POST['optD10'] : "";
        $total10 = isset($_POST['total10']) ? $_POST['total10'] : "";
        $remarks10 = 'N/A';

        $result = array(
        array(
            $aic1,
            $repcode1,
            $name1,
            $optA1,
            $optB1,
            $optC1,
            $optD1,
            $total1,
            $remarks1
        ),
        array(
            $aic2,
            $repcode2,
            $name2,
            $optA2,
            $optB2,
            $optC2,
            $optD2,
            $total2,
            $remarks2
        ),
        array(
            $aic3,
            $repcode3,
            $name3,
            $optA3,
            $optB3,
            $optC3,
            $optD3,
            $total3,
            $remarks3
        ),
        array(
            $aic4,
            $repcode4,
            $name4,
            $optA4,
            $optB4,
            $optC4,
            $optD4,
            $total4,
            $remarks4
        ),
        array(
            $aic5,
            $repcode5,
            $name5,
            $optA5,
            $optB5,
            $optC5,
            $optD5,
            $total5,
            $remarks5
        ),
        array(
            $aic6,
            $repcode6,
            $name6,
            $optA6,
            $optB6,
            $optC6,
            $optD6,
            $total6,
            $remarks6
        ),
        array(
            $aic7,
            $repcode7,
            $name7,
            $optA7,
            $optB7,
            $optC7,
            $optD7,
            $total7,
            $remarks7
        ),
        array(
            $aic8,
            $repcode8,
            $name8,
            $optA8,
            $optB8,
            $optC8,
            $optD8,
            $total8,
            $remarks8
        ),
        array(
            $aic9,
            $repcode9,
            $name9,
            $optA9,
            $optB9,
            $optC9,
            $optD9,
            $total9,
            $remarks9
        ),
        array(
            $aic10,
            $repcode10,
            $name10,
            $optA10,
            $optB10,
            $optC10,
            $optD10,
            $total10,
            $remarks10
        )
    );


            $headings = array(array('INTERVIEW WORKSHEET'),array(
                NULL,
                NULL,
                NULL,
                NULL,
                NULL,
                NULL,
                NULL,
                NULL,
                NULL),array(
                'AIC',
                'RepCode',
                'Name of Candidates',
                'Gen Info. & Technical Knowledge',
                'Communication Ability',
                'Attitude Towards Profession',
                'Appearance',
                'TOTAL',
                'Remarks')
            );
            $fone = "___________________________";
            $ftwo = "Signature of Rater";
            $footer = array(array($fone),array($ftwo));

            $points = array(
                ' ',
                ' ',
                ' ',
                '(3 pts)',
                '(3 pts)',
                '(2 pts)',
                '(2 pts)',
                '(10 pts)',
                ' '
            );
            $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
            $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
            $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
            $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
            $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);
            $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setAutoSize(true);
            $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setAutoSize(true);
            $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setAutoSize(true);
            $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setAutoSize(true);
            $objPHPExcel->getActiveSheet()
                 ->getStyle('A1:I19')
                 ->getAlignment()
                 ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getDefaultStyle()
              ->getBorders()
              ->getTop()
              ->setBorderStyle(PHPExcel_Style_Border::BORDER_THICK);
            $objPHPExcel->getDefaultStyle()
              ->getBorders()
              ->getBottom()
              ->setBorderStyle(PHPExcel_Style_Border::BORDER_THICK);
            $objPHPExcel->getDefaultStyle()
              ->getBorders()
              ->getLeft()
              ->setBorderStyle(PHPExcel_Style_Border::BORDER_THICK);
            $objPHPExcel->getDefaultStyle()
              ->getBorders()
              ->getRight()
              ->setBorderStyle(PHPExcel_Style_Border::BORDER_THICK);
            $objPHPExcel->getActiveSheet()->getStyle('A1:I1')->getFont()->setSize(22);
            $objPHPExcel->getActiveSheet()->mergeCells('A1:I2');
            $objPHPExcel->getActiveSheet()->getStyle('A16:I16')->getFont()->setSize(17);
            $objPHPExcel->getActiveSheet()->getStyle('A17:I17')->getFont()->setSize(13);
            $objPHPExcel->getActiveSheet()->mergeCells('A16:I16');
            $objPHPExcel->getActiveSheet()->mergeCells('A17:I17');
            $objPHPExcel->getActiveSheet()->getStyle('A3:I3')->applyFromArray(
                array(
                    'font' => array(
                        'bold' => true
                    ),
                    'alignment' => array(
                        'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
                    ),
                    'borders' => array(
                        'top' => array(
                            'style' => PHPExcel_Style_Border::BORDER_THIN
                        )
                    ),
                    'fill' => array(
                        'type' => PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR,
                        'rotation' => 90,
                        'startcolor' => array(
                            'argb' => 'FFA0A0A0'
                        ),
                        'endcolor' => array(
                            'argb' => 'FFFFFFFF'
                        )
                    )
                )
        );
            $objPHPExcel->getActiveSheet()->fromArray($headings, null, 'A1');
            $objPHPExcel->getActiveSheet()->fromArray($footer, null, 'A16');
            $objPHPExcel->getActiveSheet()->fromArray($points, null, 'A4');
            $row = 5;
            foreach($result as $rows){
            if (!empty($rows[4])) {
                $objPHPExcel->getActiveSheet()->fromArray($rows, null, 'A' . $row);
                $row++;
            }
        }


        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="'.$title.'.xlsx"');
        header('Cache-Control: max-age=0');

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

1 个答案:

答案 0 :(得分:3)

您的$title变量包含换行符。您可以使用以下简单的解决方法将其删除:

header(sprintf(
    'Content-Disposition: attachment;filename="%s.xlsx"',
    str_replace("\n", "", $title)
));

如果你想确定Unix,Mac或Windows风格的换行符:

header(sprintf(
    'Content-Disposition: attachment;filename="%s.xlsx"',
    preg_replace('/\\r\\n?|\\n/', '', $title)
));