Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-将数据导出到excel_Php_Export_Phpexcel_Export To Excel - Fatal编程技术网

PHP-将数据导出到excel

PHP-将数据导出到excel,php,export,phpexcel,export-to-excel,Php,Export,Phpexcel,Export To Excel,我有一些数据需要导出到excel。现在它导出到其他类型的文件,我可以用记事本打开和阅读,但不能用excel 代码如下: <?php header("Content-Disposition: attachment; filename=\"ISBN\""); header("Content-Type: application/vnd.xls"); // filename for download function output($data){

我有一些数据需要导出到excel。现在它导出到其他类型的文件,我可以用记事本打开和阅读,但不能用excel

代码如下:

<?php
    header("Content-Disposition: attachment; filename=\"ISBN\"");
    header("Content-Type: application/vnd.xls");
        // filename for download    
function output($data){ 

    //print_r($data);
    $str = preg_replace("/\t/", "\\t", $data);
    $str = preg_replace("/\r?\n/", "\\n", $str);
    if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';


    $filename = "nonprofitdata" . date_default_timezone_set('Ymd') . ".xls";

        $flag = false;
        foreach($data as $row) {
            if(!$flag) {
                // display field/column names as first row
                echo implode("\t", array_keys($row)) . "\r\n";
                $flag = true;
            }
        array_walk($row, 'cleanData');
        echo implode("\t", array_values($row)) . "\r\n";
        }
        exit;
}
?>

试试PHPExcel:这就是我得到这段代码的地方。也许可以用
fputcsv()
编写一个CSV文件。这里有多个问题:1<代码>输出()
从未被调用;2
$data
的内容是什么?三。文件名是“ISBN”?缺少文件扩展名?4我没有看到您使用PHPExcel函数5。最后,MS Excel中显示的错误是什么?实际上,这似乎是一种选项卡分隔格式。如果你聪明的话,Excel会读这些。网上有很多教程-试试看