无法在服务器上生成Excel文件

时间:2016-01-22 13:07:20

标签: php excel

我正在尝试使用以下代码生成发票。

它在local-host中运行良好,

当我在朋友服务器中执行代码时,它工作正常,

在我的服务器中无法生成相同的代码 - (使用godaddy)

我是否需要修改任何服务器设置?

 <?php
    include("includes/DbConfig.php");
    $SQL = "SELECT id,user_mobile,user_email FROM users limit 1,2";
    $header = '';
    $result ='';
    $exportData = mysql_query ($SQL ) or die ( "Sql error : " . mysql_error( ) );

    $fields = mysql_num_fields ( $exportData );

    for ( $i = 0; $i < $fields; $i++ )
    {
        $header .= mysql_field_name( $exportData , $i ) . "\t";
    }

    while( $row = mysql_fetch_row( $exportData ) )
    {
        $line = '';
        foreach( $row as $value )
        {                                            
            if ( ( !isset( $value ) ) || ( $value == "" ) )
            {
                $value = "\t";
            }
            else
            {
                $value = str_replace( '"' , '""' , $value );
                $value = '"' . $value . '"' . "\t";
            }
            $line .= $value;
        }
        $result .= trim( $line ) . "\n";
    }
    $result = str_replace( "\r" , "" , $result );

    if ( $result == "" )
    {
        $result = "\nNo Record(s) Found!\n";                        
    }

        header("Content-type: application/octet-stream");
        header("Content-Disposition: attachment; filename=export.xls");
        header("Pragma: no-cache");
        header("Expires: 0");
        print "$header\n$result";
    ?>

1 个答案:

答案 0 :(得分:1)

我刚添加了ob_get_clean();它开始工作了

<?php
ob_start();
session_start();
include("includes/DbConfig.php");
?>

   <?php
    header("Content-type: application/octet-stream");
    header("Content-Disposition: attachment; filename=export.xls");
    ob_get_clean();
    header("Pragma: no-cache");
    header("Expires: 0");
    print "$header\n$result";
    ?>