2014-02-11 4 views
0

Может ли кто-нибудь помочь мне исправить эту ошибку после открытия файла сохранения с помощью phpexcel?ошибка в excel с использованием PHPexcel

Я открыл сохраненный файл и этот всплывающий всплывающий всплывающий файл после открытия сохраненного файла excel ... как я могу это исправить?

enter image description here

текущий код:

<!DOCTYPE html> 
<html> 
<head> 
<title>test</title> 
</head> 
<body> 
<?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'); 
}?> 

    <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

''? В Excel? И вы обвиняете JavaScript? –

ответ

1

Вы должны переместить выход за пределы HTML обработки POST и выхода после вывода XLSX. Ваш код прямо сейчас окружает XLSX кучей HTML:

<?php // delete all the HTML at the beginning and start with <?php 
if(isset($_POST['send'])){ 
    require_once 'Classes\PHPExcel\IOFactory.php'; 
    // keep all the code in between this 
    $objWriter->save('php://output'); 
    exit; // add the exit here 
}?> 

<!DOCTYPE html> 
<html> 
<head> 
<title>test</title> 
</head> 
<body> 
    <form id="form1" name="form1" method="post" action="" > 
    [...] 
+0

Отлично работает как магия ... спасибо: D – xplody

Смежные вопросы