而不是保存CSV文件如何下载文件

时间:2015-06-19 07:45:26

标签: php http csv

我使用CSV扩展程序从数据库导出数据,我想将结果下载为CSV文件。我怎么能这样做?

这是我的代码:

public function actionExportexcel() { 
    $con = mysql_connect("localhost", "root", "") or die();
    mysql_select_db("fiducial", $con);
    $filename = 'uploads/'.strtotime("now").".csv";
    $query = mysql_query("SELECT * FROM
                        (
                            SELECT employeecode,name,dob,age,sex,employee_relation,company_id
                            FROM employeedetails
                            UNION
                            SELECT employeecode,father_name,father_dob,father_age,father_gender,father_relation,company_id
                            FROM employeedetails
                            WHERE father_name IS NOT NULL AND company_id IS NOT NULL
                            UNION
                            SELECT employeecode,mother_name,mother_dob,mother_age,mother_gender,mother_relation,company_id
                            FROM employeedetails
                            WHERE mother_name IS NOT NULL AND mother_dob IS NOT NULL
                        )t WHERE t.company_id = '56' ORDER by t.employeecode ") or die(mysql_error());

    $num_rows = mysql_num_rows($query);
    if($num_rows >= 1)
    {
        $rows = mysql_fetch_assoc($query);
        $seperator = "";
        $comma = "";
        foreach ($rows as $name => $value) 
        {
            $seperator .= $comma . '' .str_replace('', '""', $name);
            $comma = ",";
        }
        $seperator .= "\n";
        $fp = fopen($filename, "w");
        fputs($fp, $seperator);
        mysql_data_seek($query, 0);
        while($rows = mysql_fetch_assoc($query))
        {
            $seperator = "";
            $comma = "";
            foreach ($rows as $name => $value) 
            {
                $seperator .= $comma . '' .str_replace('', '""', $value);
                $comma = ",";
            }
            $seperator .= "\n";
            fputs($fp, $seperator);
        }
        echo "Data successfully exported";
        fclose($fp);
    } else {
        echo "No data is Available";
    }

}

如何将其下载为CSV文件?

2 个答案:

答案 0 :(得分:2)

您需要为CSV文件下载正确设置HTTP标头,然后将搜索结果写入本地服务器上的CSV文件,而不是将其写入PHP输出缓冲区(php://output) :

完整的工作示例:

public function actionExportexcel() { 

    $con = mysql_connect("localhost", "root", "") or die();
    mysql_select_db("fiducial", $con);
    $filename = 'uploads/'.strtotime("now").".csv";
    $query = mysql_query("SELECT * FROM
                        (
                            SELECT employeecode,name,dob,age,sex,employee_relation,company_id
                            FROM employeedetails
                            UNION
                            SELECT employeecode,father_name,father_dob,father_age,father_gender,father_relation,company_id
                            FROM employeedetails
                            WHERE father_name IS NOT NULL AND company_id IS NOT NULL
                            UNION
                            SELECT employeecode,mother_name,mother_dob,mother_age,mother_gender,mother_relation,company_id
                            FROM employeedetails
                            WHERE mother_name IS NOT NULL AND mother_dob IS NOT NULL
                        )t WHERE t.company_id = '56' ORDER by t.employeecode ") or die(mysql_error());



    $num_rows = mysql_num_rows($query);
    if($num_rows >= 1) {
        header('Content-Description: Your Download Name ');
        header('Content-Type: application/csv');
        header('Content-Disposition: attachment; filename=yourfilename.csv');

        $rows = mysql_fetch_assoc($query);
        $seperator = "";
        $comma = "";
        foreach ($rows as $name => $value) 
        {
            $seperator .= $comma . '' .str_replace('', '""', $name);
            $comma = ",";
        }
        $seperator .= "\n";
        $fp = fopen('php://output', 'w');
        fputs($fp, $seperator);
        mysql_data_seek($query, 0);
        while($rows = mysql_fetch_assoc($query))
        {
            $seperator = "";
            $comma = "";
            foreach ($rows as $name => $value) 
            {
                $seperator .= $comma . '' .str_replace('', '""', $value);
                $comma = ",";
            }
            $seperator .= "\n";
            fputs($fp, $seperator);
        }

        fclose($fp);
    } else {
        echo "No data is Available";
    }

}

答案 1 :(得分:0)

使用

header('Content-Type: application/excel');
header('Content-Disposition: attachment; filename=<filename>.csv');