PHPEXCEL在CSV中包含html

PHPEXCEL在CSV中包含html,php,csv,phpexcel,Php,Csv,Phpexcel,我是PHPEXCEL的新手。我的问题是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>

我是PHPEXCEL的新手。我的问题是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>

无标题文件
敏捷的棕色狐狸跳过了懒狗。

当代码中包含HTML时,Web服务器会将HTML发送给用户。此时,Web服务器告诉访问它的浏览器它正在发送的内容的格式。(在您的例子中,是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>

然后输出PHPExcel文件,但现在已经太迟了,因为用户的浏览器已经被告知它正在接收MIME类型“text/html”的内容。没有合适的机制来告诉浏览器,在请求的中间,内容类型已经改变,并且开始对它进行不同的处理。

< P>当你的代码中有HTML时,你的Web服务器将HTML发送给你的用户。此时,Web服务器告诉访问它的浏览器它正在发送的内容的格式。(在您的例子中,是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>

然后输出PHPExcel文件,但现在已经太迟了,因为用户的浏览器已经被告知它正在接收MIME类型“text/html”的内容。没有一种机制可以在请求的中间告诉浏览器内容类型已经改变,并且开始对它进行不同的处理。

< P>我创建了一个SpApple PHP文件,称为它,它的工作方式很有魅力。我想让Mark Baker回答这个问题,不过这是一个注释:)

我创建了一个单独的php文件,名为它,它的工作方式很有魅力。我想让Mark Baker回答这个问题,不过这是一个注释:)

如果你使用PHPExcel发送csv文件,那么也不要发送标记,它真的很简单,我创建了一个单独的php文件,调用它,它就可以工作:)如果你使用PHPExcel发送csv文件,那么也不要发送标记,它真的很简单,我创建了一个分离的php文件,调用它,它就工作了:)
 <!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>