douhuiyan2772 2017-12-28 08:29
浏览 47

phpexcel的奇怪行为

When trying to get an Excel spreadsheet from php with phpexcel, strange characters are generated in the output if a constant definition (DEFINE (' var',' value') is added. My code

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');      

I don't understand the reason, since config. php is all it contains.

If I delete the config. php line, the excel file is generated correctly Does anyone know what this behavior is due to?

this code runs smoothly

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 content

define('DB_HOST', 'myHost');
define('DB_USER', 'myUser');
define('DB_PASSWORD', 'myPass');
define('DB_DATABASE', 'myDB');
  • 写回答

1条回答 默认 最新

  • duangu9173 2017-12-28 08:57
    关注

    I finally found it. the output to the excel file does not allow ECHO or any other type of text output, in my case the config file had a blank line at the beginning

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

    etc.

    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)