生成报告时phpexcel中的错误

时间:2014-02-11 17:48:07

标签: php html mysql phpexcel

任何人都可以帮助我,当我的表单中的某些字段为空时我正在处理错误,我点击提交(发送到excel),这将弹出我已保存的Excel文件。

enter image description here

但如果我填写表单中的所有输入,则表示没有错误...错误仅显示某些输入为空时我点击按钮。

enter image description here

当前代码:

<?php
if(isset($_POST['send'])){
    require_once 'Classes\PHPExcel\IOFactory.php';
    $filename = 'worksheet.xlsx';
    $title = $_POST['title'];
    mysql_connect("localhost","root","") or die ("cant connect!");
    mysql_select_db("test") or die ("cant find database!");

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

    $objPHPExcel = $objReader->load($filename);
    $objWorksheet = $objPHPExcel->getActiveSheet();
    $objWorksheet = $objPHPExcel->setActiveSheetIndex(0);
    $name1 = $_POST['name1'];
    $optA1 = $_POST['optA1'];
    $optB1 = $_POST['optB1'];
    $optC1 = $_POST['optC1'];
    $optD1 = $_POST['optD1'];
    $total1 = $_POST['total1'];
    $remarks1 = $_POST['remarks1'];

    $name2 = $_POST['name2'];
    $optA2 = $_POST['optA2'];
    $optB2 = $_POST['optB2'];
    $optC2 = $_POST['optC2'];
    $optD2 = $_POST['optD2'];
    $total2 = $_POST['total2'];
    $remarks2 = $_POST['remarks2'];

    $result = array(
    array(
        $name1,
        $optA1,
        $optB1,
        $optC1,
        $optD1,
        $total1,
        $remarks1
    ),
    array(
        $name2,
        $optA2,
        $optB2,
        $optC2,
        $optD2,
        $total2,
        $remarks2
    )
);


        $headings = array( 
            'NAME',
            'Gen Info. & Technical Knowledge',
            'Communication Ability',
            'Attitude Towards Profession',
            'Appearance',
            'TOTAL',
            'Remarks'
        );
        $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()
             ->getStyle('A1:G14')
             ->getAlignment()
             ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('A1:G1')->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($points, null, 'A2');
        $row = 3;
        foreach($result as $rows){
            $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;
}?>
<!DOCTYPE html>
<html>
<head>
<title>test</title>
</head>
<body>
    <form id="form1" name="form1" method="post" action="" >
    FILE TITLE: <input name="title" type="text" id="title" />
    <table>
    <td><input name="name1" type="text"  value="" required size="41" id="query1" /></td>
            <td>
            <input id="textboxid" type="radio" name="optA1" value="1" />1
            <input id="textboxid" type="radio" name="optA1" value="2" />2
            <input id="textboxid" type="radio" name="optA1" value="3" />3
            </td>
            <td>
            <input id="textboxid" type="radio" name="optB1" value="1" />1
            <input id="textboxid" type="radio" name="optB1" value="2" />2
            <input id="textboxid" type="radio" name="optB1" value="3" />3
            </td>
            <td>
            <input id="textboxid" type="radio" name="optC1" value="1" />1
            <input id="textboxid" type="radio" name="optC1" value="2" />2
            </td>
            <td>
            <input id="textboxid" type="radio" name="optD1" value="1" />1
            <input id="textboxid" type="radio" name="optD1" value="2" />2
            </td>
            <td><input type="text" name="total1" size="3" /></td>
            <td><input name="remarks1" type="text"  value="" required size="8"  /></td>
         </tr>
         <tr>
            <td><input name="name2" type="text"  value="" required size="41" id="query2"  /></td>
            <td>
            <input id="textboxid" type="radio" name="optA2" value="1" />1
            <input id="textboxid" type="radio" name="optA2" value="2" />2
            <input id="textboxid" type="radio" name="optA2" value="3" />3
            </td>
            <td>
            <input id="textboxid" type="radio" name="optB2" value="1" />1
            <input id="textboxid" type="radio" name="optB2" value="2" />2
            <input id="textboxid" type="radio" name="optB2" value="3" />3
            </td>
            <td>
            <input id="textboxid" type="radio" name="optC2" value="1" />1
            <input id="textboxid" type="radio" name="optC2" value="2" />2
            </td>
            <td>
            <input id="textboxid" type="radio" name="optD2" value="1" />1
            <input id="textboxid" type="radio" name="optD2" value="2" />2
            </td>
            <td><input type="text" name="total2" id="total2" size="3" /></td>
            <td><input name="remarks2" type="text"  value="" required size="8" /></td>
        </tr>
    </table>
    <input type="submit" name="send" value="send to excel" id="send" formnovalidate />
    </form>
    </body>
    </html>

1 个答案:

答案 0 :(得分:2)

尝试设置为空而不是空

类似的东西:

$name1 = isset($_POST['name1']) ? $_POST['name1'] : "";