2015-06-19 57 views
1

我使用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

您需要正確設置HTTP標頭的CSV文件下載,然後,而不是寫你的查詢結果到本地服務器上的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"; 
    } 

} 
+0

文件正在下載,但該文件中沒有數據,它只是顯示我的回聲聲明回聲「數據成功導出」 – Nodemon

+0

@ExCrin我編輯了我的答案,包括您的原始代碼。 –

+0

謝謝你的工作 – Nodemon

0

使用

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

文件中獲取下載,但沒有數據我n該文件,它只是顯示我的回聲語句回聲「數據成功導出」; – Nodemon