phpexcel没有输出任何东西

时间:2018-01-28 20:46:45

标签: php phpexcel

下面是我的简单测试php。当我跑这个什么都没发生。

require_once('/var/www/html/Classes/PHPExcel.php');

$excel = new PHPExcel();

$excel->setActiveSheetIndex(0)
    ->setCellValue('A1','Hello')
    ->setCellValue('B1','World');

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="/var/www/html/Examples/test.xlsx"');
header('Cache-Control: max-age=0');


$objWriter = PHPExcel_IOFactory::createWriter($excel, 'Excel2007');
$objWriter->save('php://output');
exit;

为什么这不起作用的任何想法。根本没有重定向,也没有归档。

1 个答案:

答案 0 :(得分:0)

通过略读PHPEXCEL documentation示例,可以使用以下内容:

<?php
date_default_timezone_set('America/Los_Angeles');
require_once('/var/www/html/Classes/PHPExcel.php');

$sheet = array(
    array(
      'a1 Hello',
      'b1 World',
      'c1 here',
      'd1 test',
    )
  );

  $doc = new PHPExcel();
  $doc->setActiveSheetIndex(0);

  $doc->getActiveSheet()->fromArray($sheet, null, 'A1');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="your_name.xls"');
header('Cache-Control: max-age=0');

  // Do your stuff here
  $writer = PHPExcel_IOFactory::createWriter($doc, 'Excel5');

$writer->save('php://output');
?>

重要提示请勿通过exit();

终止该进程