PHP从数组创建xls

前端之家收集整理的这篇文章主要介绍了PHP从数组创建xls前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我尝试从数组创建xls文件,并使用以下代码通过浏览器下载它:
$sheet = array(
    array(
      'a1 data','b1 data','c1 data','d1 data',)
  );

  $doc = new PHPExcel();
  $doc->getActiveSheet()->fromArray($sheet,null,'A1');

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

  // Do your stuff here

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

问题是我得到一个空文件.任何想法可能是什么问题?

请试试 :
As per official documentation,您首先需要使用对象编写器保存文件

如果这是您想要的,请告诉我

<?PHP
date_default_timezone_set('America/Los_Angeles');

require_once('PHPExcel.PHP');

$sheet = array(
    array(
      'a1 data',)
  );

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

  $doc->getActiveSheet()->fromArray($sheet,'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');
?>

猜你在找的PHP相关文章