php PHP从数组创建xls

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23348385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:39:47  来源:igfitidea点击:

PHP create xls from array

phpphpexcelxls

提问by MTA

I try to create xls file from array and download it with the browser with this code:

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

The problem is that i get a empty file.Any idea what can be the issue?

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

回答by Paul Bele

Please try : As per official documentation, you first need to save the file with the object writer

请尝试: 根据官方文档,您首先需要使用对象编写器保存文件

Please let me know if this is what you wanted

请让我知道这是否是您想要的

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

回答by Paulo Victor

Sometimes we just can't use an external library/class like PHPExcel, in this case, I recommend you to use HTML table and change headers as xls, you can create it easily from array, very simple example:

有时我们不能使用像 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;
?>

回答by Saurabh Chandra Patel

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

回答by nisanth A.N

        $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();