2016-12-14 72 views
0

我是PHPEXCEL的新用戶。我的問題是PHPEXCEL(下面的代碼)包含CSV輸出中的HTML。PHPEXCEL在CSV中包含html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <title>Untitled Document</title> 
    </head> 

    <body> 
    The quick brown fox jumps over the lazy dog. 
    <?php 
    /** Include PHPExcel */ 
    require_once dirname(__FILE__) . '/PHPExcel_1.8.0_doc/Classes/PHPExcel.php'; 


    // Create new PHPExcel object 
    $objPHPExcel = new PHPExcel(); 

    // Add some data 
    $objPHPExcel->setActiveSheetIndex(0) 
       ->setCellValue('A1', 'ID') 
       ->setCellValue('B1', 'Name') 
       ->setCellValue('C1', 'Description') 
       ->setCellValue('D1', 'Type'); 

       //ID 
       for($i=2; $i<=6; $i++){ 
    $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A'.$i, $i-1); 
       } 

       //Name 
       for($i=2; $i<=6; $i++){ 
    $objPHPExcel->setActiveSheetIndex(0)->setCellValue('B'.$i, "Game" . ($i-1)); 
       } 


    // Miscellaneous glyphs, UTF-8 
    /*$objPHPExcel->setActiveSheetIndex(0) 
       ->setCellValue('A4', 'Miscellaneous glyphs') 
       ->setCellValue('A5', 'éàèùâêîôûëïüÿäöüç'); 
    */ 

    // Rename worksheet 
    $objPHPExcel->getActiveSheet()->setTitle('List of Games'); 


    // Set active sheet index to the first sheet, so Excel opens this as the first sheet 
    $objPHPExcel->setActiveSheetIndex(0); 


    // Redirect output to a client’s web browser (Excel5) 
    header('Content-Type: application/vnd.ms-excel'); 
    header('Content-Disposition: attachment;filename="gcg_list.xls"'); 
    header('Cache-Control: max-age=0'); 
    // If you're serving to IE 9, then the following may be needed 
    header('Cache-Control: max-age=1'); 

    // If you're serving to IE over SSL, then the following may be needed 
    header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past 
    header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified 
    header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1 
    header ('Pragma: public'); // HTTP/1.0 

    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); 
    $objWriter->save('php://output'); 
    exit; 
    ?> 
    </body> 
    </html> 

輸出:
敏捷的棕色狐狸跳過了懶狗。 Џࡱက; ş+'@ Hh䄀䄀䄀Sp Sp Sp Sp ‸3f̿fff̙̙̌̿̿̿̿3f3̌fff3f3f33333f33333「遊戲列表AA恀IDNameDescriptionTypeGame1Game2Game3Game4Game5̇р* + Dffffff濧ffffff濨邇迡」dXX333333ӿ333333ӿU} $} $} $} $ @ @ @ @ @ddggՍ՜。+,0 HPX`hp x䄀工作表計算器的計算器進入F7Uҁ7UҁSummaryInformation(F耀練習冊FDžDocumentSummaryInformation8˚F쀀

如果我讓我的代碼純PHP(不包含HTML標籤),PHPEXCEL將輸出正確的CSV文件

輸出:
ID名稱說明類型 1的Game1
2 GAME2
3 Game3
4 Game4
5 Game5

你能幫助我知道什麼是錯的代碼?謝謝

+1

如果你使用PHPExcel發送一個csv文件,那麼不要發送標記,這很簡單 –

+0

謝謝,我創建了一個sepate php文件,稱它,它的工作:) – aabejero

回答

0

我創建了一個sepate php文件,叫它,它的作用就像魅力。我想讓馬克貝克的答案,但它的評論:)

2

當您的代碼中有HTML時,您的網絡服務器會將HTML發送給您的用戶。此時,網絡服務器會通知瀏覽器訪問它正在發送的內容的格式。 (在你的情況下,HTML,因爲這是你的頁面的頂部)

然後你輸出PHPExcel文件,但在這個階段已經太遲了,因爲用戶的瀏覽器已經被告知它正在接收的內容MIME類型「text/html」。沒有任何機制可以在請求中告訴瀏覽器內容類型已經改變,並開始以不同的方式處理它。