2015-05-29 64 views
2

我們需要在代碼點火器中導出(下載)CSV文件。這隻會導出我的表格數據,我如何更改我的csv文件的格式?請任何人幫助我。如何導出代碼點火器中的CSV文件?

+1

顯示您的代碼將幫助我們幫助您。 – Kishor

+0

可能的重複[從codeigniter mysql下載csv](http://stackoverflow.com/questions/20840274/download-csv-from-codeigniter-mysql) – Saty

回答

4

Codeigniter爲此有一個內置的類。試試這個:

$this->load->dbutil(); 
$this->load->helper('file'); 
$this->load->helper('download'); 
$query = $this->db->query("Your SQL Here"); 
$delimiter = ","; 
$newline = "\r\n"; 
$data = $this->dbutil->csv_from_result($query, $delimiter, $newline); 
force_download('CSV_Report.csv', $data); 
1

你可以看到這裏如何導出數據庫結果到CSV文件

http://www.code2learn.com/2012/03/generating-csv-file-using-codeigniter.html

,或者你可以用這個代碼創建幫手

if (! defined('BASEPATH')) exit('No direct script access allowed'); 

if (! function_exists('array_to_csv')) 
{ 
    function array_to_csv($array, $download = "") 
    { 
     if ($download != "") 
     {  
      header('Content-Type: application/csv'); 
      header('Content-Disposition: attachement; filename="' . $download . '"'); 
     }   

     ob_start(); 
     $f = fopen($download, 'wb') or show_error("Can't open php://output"); 
     $n = 0;   
     foreach ($array as $line) 
     { 
      $n++; 
      if (! fputcsv($f, $line)) 
      { 
       show_error("Can't write line $n: $line"); 
      } 
     } 
     fclose($f) or show_error("Can't close php://output"); 
     $str = ob_get_contents(); 
     ob_end_clean(); 

     if ($download == "") 
     { 
      return $str;  
     } 
     else 
     {  
      echo $str; 
     }   
    } 
} 

if (! function_exists('query_to_csv')) 
{ 
    function query_to_csv($query, $headers = TRUE, $download = "") 
    { 
     if (! is_object($query) OR ! method_exists($query, 'list_fields')) 
     { 
      show_error('invalid query'); 
     } 

     $array = array(); 

     if ($headers) 
     { 
      $line = array(); 
      foreach ($query->list_fields() as $name) 
      { 
       $line[] = $name; 
      } 
      $array[] = $line; 
     } 

     foreach ($query->result_array() as $row) 
     { 
      $line = array(); 
      foreach ($row as $item) 
      { 
       $line[] = $item; 
      } 
      $array[] = $line; 
     } 

     echo array_to_csv($array, $download); 
    } 
} 

/* End of file csv_helper.php */ 
/* Location: ./system/helpers/csv_helper.php */ 

然後你控制器功能看起來像這樣

function create_csv(){ 
$this->load->helper('url'); 
       $this->load->helper('csv'); 

      $query = $this->db->query('SELECT * FROM <tablename>'); 
      $num = $query->num_fields(); 
      $var =array(); 
      $i=1; 
      $fname=""; 
      while($i <= $num){ 
       $test = $i; 
       $value = $this->input->post($test); 

       if($value != ''){ 
         $fname= $fname." ".$value; 
         array_push($var, $value); 

        } 
       $i++; 
      } 

      $fname = trim($fname); 

      $fname=str_replace(' ', ',', $fname); 

      $this->db->select($fname); 
      $quer = $this->db->get('<tablename>'); 

      query_to_csv($quer,TRUE,'Products_'.date('dMy').'.csv'); 

     } 
} 
+0

感謝您的幫助,但在csv文件中,我沒有得到任何查詢數據?文件下載成功,但我沒有在csv文件中找到任何記錄。 – piyush