2014-02-11 147 views
0

任何人都可以幫助我當我的表單中的某些字段爲空時,我點擊提交(發送到excel),這將在我保存的excel文件中彈出。生成報告時phpexcel中的錯誤

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> 
+0

我們可以看到'fromArray'簽名.. 。我的記憶說這不是它想要的行號...... – ficuscr

回答

2

嘗試設置,而不是空空

類似:

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

「null」的FYI綁定到'fromArray()'方法的第二個參數。 – ficuscr