phpexcel的奇怪行为

问题描述 投票:1回答:1

当试图从php使用phpexcel获取Excel电子表格时,如果添加了常量定义(DEFINE('var','value'),则会在输出中生成奇怪的字符。我的代码

header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="theFile.xls"');
header('Cache-Control: max-age=0');

require_once '../config.php';
include 'PHPExcel.php';
$excel = new PHPExcel();

$writer = PHPExcel_IOFactory::createWriter($excel, 'Excel5');

$excel->setActiveSheetIndex(0);
$excel->getActiveSheet()->SetCellValue('A1', 'Hello');
$excel->getActiveSheet()->SetCellValue('B2', 'world!');
$writer->save('php://output');      

我不明白原因,因为配置。 php就是它的全部内容。

如果我删除配置。 php行,正确生成excel文件有谁知道这个行为是由于什么?

这段代码运行顺利

header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="theFile.xls"');
header('Cache-Control: max-age=0');

include 'PHPExcel.php';
$excel = new PHPExcel();

$writer = PHPExcel_IOFactory::createWriter($excel, 'Excel5');

$excel->setActiveSheetIndex(0);
$excel->getActiveSheet()->SetCellValue('A1', 'Hello');
$excel->getActiveSheet()->SetCellValue('B2', 'world!');
$writer->save('php://output');      

config.php内容

define('DB_HOST', 'myHost');
define('DB_USER', 'myUser');
define('DB_PASSWORD', 'myPass');
define('DB_DATABASE', 'myDB');
php excel phpexcel
1个回答
0
投票

我终于找到了。输出到excel文件不允许ECHO或任何其他类型的文本输出,在我的情况下配置文件在开头有一个空行

--->here
<?php 
define('DB_HOST', 'myHost');
define('DB_USER', 'myUser');

等等

© www.soinside.com 2019 - 2024. All rights reserved.