dongxiao1591
2014-02-11 15:31
浏览 51
已采纳

使用PHPexcel在Excel中出错

can anyone help me fix this kind of error after opening a save file using phpexcel?

i opened the saved file and this kind of error pop-up after opening the saved excel file...how do i fix this?

enter image description here

current code:

<!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>
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douzhu1188 2014-02-11 15:37
    已采纳

    You need to move the HTML output beyond the POST processing and exit after outputting the XLSX. Your code right now surrounds the XLSX with a bunch of 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="" >
        [...]
    
    打赏 评论

相关推荐 更多相似问题