[英]PHPEXCEL includes html in CSV
我是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>
输出:
敏捷的棕色狐狸跳过了懒狗。 Џࡱက; ? ? ????ş?+'ٰ @ Hh?䄀无标题的电子表格?未知的创建者?未知的创建者@7Uҁ@7Uҁ? ̇рB=%r8X“ 1܀Calibri 3f̀̿fff̙̙̀̌̿̿̿̀̿̀3f3̌̀̀fff3f3f33f33333f33333”游戏列表AAgName IDName?DescriptionTypeGame1Game2Game3Game4Game5? *р* + Dffffff濧ffffff濨迩迡“dXX333333ӿ333333ӿU} $?} $?} $?} $ ????????????????????????????????? @ ??? @?> @d?d?ggՍ՜。?+,0 HP?X?`?hp x䄀?????工作表?? Feuilles de calculRoot条目F7Uҁ7UҁSummaryInformation(F耀工作簿FDžDocumentSummaryInformation8F쀀
如果我将代码设为纯PHP(没有HTML标记),PHPEXCEL将输出正确的CSV文件。
输出:
ID名称描述类型1 Game1
2游戏2
3游戏3
4游戏4
5游戏5
您能帮我知道代码有什么问题吗? 谢谢
当代码中包含HTML时,网络服务器会将HTML发送给用户。 此时,网络服务器告诉浏览器访问它的内容是什么格式。 (对于您来说,是HTML,因为它位于页面顶部)
然后,您随后输出PHPExcel文件,但是到此阶段为时已晚,因为已经告知用户浏览器它正在接收MIME类型“ text / html”的内容。 没有机制可以在请求过程中告知浏览器内容类型已更改,并开始以不同的方式进行处理。
我已经创建了一个单独的php文件,名为php文件,它的工作方式就像魅力。 我想让马克·贝克回答这个问题,但是它是一个评论:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.