alabejero
alabejero

Reputation: 113

PHPEXCEL includes html in CSV

I'm new with PHPEXCEL. My problem is PHPEXCEL(the code below) includes HTML in CSV output.

 <!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>

Output:
The quick brown fox jumps over the lazy dog. Џࡱက;  ş򹏨+'ٰ@Hh 䄀Untitled SpreadsheetUnknown CreatorUnknown Creator@7Uҁ@7Uҁ  ̇рB=%r8X"1܀Calibri�� �� �� �� �� �� �� �� �� �� �� �� �� �� �� ‸3f̀̿fff̙̙̀̌̿̿̿̀̿̀3f3̌̀̀fff3f3f33333f33333" List of GamesAAg恀 IDName DescriptionTypeGame1Game2Game3Game4Game5  ̇р*+Dffffff濧ffffff濨迩迡"dXX333333ӿ333333ӿU} $ } $ } $ } $     𿽀 @ @ @ @ >@ddgg Ս՜.+,0HP X`hp x 䄀  Worksheet Feuilles de calculRoot Entry F7Uҁ7UҁSummaryInformation( F耀Workbook FDžDocumentSummaryInformation8 F쀀

If I make my code pure PHP(without HTML tags), PHPEXCEL will output correct CSV file.

Output:
ID Name Description Type 1 Game1
2 Game2
3 Game3
4 Game4
5 Game5

Can you help me know what's wrong with the code? Thanks

Upvotes: 0

Views: 682

Answers (2)

alabejero
alabejero

Reputation: 113

I've created a sepate php file, called it and it works like charm. I want to make Mark Baker the answer however its a comment :)

Upvotes: 0

Karl Buys
Karl Buys

Reputation: 449

When you have HTML in your code, your webserver sends the HTML to your user. At this point, the webserver tells the browser accessing it what the format of the content it is sending is. (in your case, HTML, because that's at the top of your page)

You then output the PHPExcel file afterwards, but it's too late by this stage, because the user's browser has already been told that it's receiving content of the MIME type "text/html". There is no mechanism in place to tell a browser, in the middle of a request, that the content type has changed, and to start processing it differently.

Upvotes: 2

Related Questions