【问题标题】:PHP create xls from arrayPHP从数组创建xls
【发布时间】:2014-04-28 18:19:09
【问题描述】:

我尝试从数组创建 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');

问题是我得到一个空文件。知道可能是什么问题吗?

【问题讨论】:

  • 您似乎缺少了一行代码:您正在创建一个对象$writer,但实际上并没有对它做任何事情。
  • 您永远不会打印任何信息。

标签: php phpexcel xls


【解决方案1】:

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

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

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

require_once('PHPExcel.php');

$sheet = array(
    array(
      'a1 data',
      'b1 data',
      'c1 data',
      'd1 data',
    )
  );

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

【讨论】:

  • 大家好,这个例子拯救了我,但它在 Firefox 中不起作用,你知道我能做些什么吗?
  • Excel5 的 mimetype 错误,它应该是 application/vnd.ms-excelapplication/vnd.openxmlformats-officedocument.spreadsheetml.sheet 用于 Excel2007 扩展名为 xlsx 的文件。
【解决方案2】:

有时我们不能使用像PHPExcel这样的外部库/类,在这种情况下,我建议您使用HTML表格并将标题更改为xls,您可以轻松地从数组中创建它,非常简单的示例:

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

$sheet = array(
    array(
      'a1 data',
      'b1 data',
      'c1 data',
      'd1 data',
    )
);

$table = '<table><tbody><tr><td>A1</td><td>B1</td><td>C1</td><td>D1</td></tr>';
foreach ($sheet as $row) {
    $table.= '<tr><td>'.  implode('</td><td>', $row) . '</td></tr>';
}
$table.= '</tbody></table>';

header('Content-Encoding: UTF-8');
header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header ("Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT");
header ("Cache-Control: no-cache, must-revalidate");
header ("Pragma: no-cache");
header ("Content-type: application/x-msexcel;charset=UTF-8");
header ("Content-Disposition: attachment; filename=productsExport.xls" );

echo $table;
?>

【讨论】:

  • 这很酷,我从不知道excel会这样打开表格标记
  • 非常简单实用
【解决方案3】:
        $header = ['c1.', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7', 'c8', 'c9', 'c10', 'c11']; 
        header('Content-Disposition: attachment; filename="data'.date('d-m-y').'.xls"');
        header("Content-Type: application/vnd.ms-excel");

        $output = fopen('php://output', 'w');
        fputcsv($output,  $header  , "\t");
        foreach ($Data as $value) { 
            $tmp_data = [];  
            array_push($tmp_data, $value['c1']);
            array_push($tmp_data, $value['c2']);
            array_push($tmp_data, $value['c3']);
            array_push($tmp_data, $value['c4']);
            array_push($tmp_data, $value['c5'] );
            array_push($tmp_data, $value['c6']);
            array_push($tmp_data, $value['c7']);
            array_push($tmp_data, $value['c8']);
            array_push($tmp_data,  $value['c9']);
            array_push($tmp_data, $value['c10']); 
            array_push($tmp_data, $value['c11']);
            fputcsv($output,  $tmp_data ,  "\t" );  
        }
        fclose($output);

【讨论】:

    【解决方案4】:
            $list=array();
            $list['tableheading'] =array('No','Name','Age','Sex');
            $fichier = 'sample.xls';
            header( "Content-Type: text/csv;charset=utf-8" );
            header( "Content-Disposition: attachment;filename=\"$fichier\"" );
            header("Pragma: no-cache");
            header("Expires: 0");
            $fp= fopen('php://output', 'w');
            fputcsv($fp,$list['tableheading'] );
            $slno=1;
            foreach($databasefetch AS $tabledata) {              
                $data['list']=array($slno,$tabledata['name'],$tabledata['Age'],$tabledata['sex']);
                fputcsv($fp, $data['list']);
                $slno++;
            }
            fclose($fp);
            exit();
    

    【讨论】:

    • 不鼓励仅使用代码的答案。请考虑添加一些信息。
    • 这也是生成一个 csv 文件,而不是真正的 xls 格式。因此,当查看者期待真正的 xls 格式时,此方法将失败。
    猜你喜欢
    • 2013-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-05
    • 1970-01-01
    • 1970-01-01
    • 2018-10-04
    • 2012-11-23
    相关资源
    最近更新 更多