duanhan7001
2017-04-06 06:36
浏览 37
已采纳

无法从下载帮助程序代码igniter 3.0创建.csv文件

I am trying to export a list page to csv file using the code below.

 public function export()
{
    $customers = \CI::Customers()->get_customer_export();

    \CI::load()->helper('download_helper');
    force_download('customers.csv', ($customers));
}

It generates and error that second parameter should be a string not any array.Also if i try to json_encode the $customers variable,it creates a file but outputs json code in the csv file.

Any help is appreciated

图片转代码服务由CSDN问答提供 功能建议

我正在尝试使用下面的代码将列表页面导出到csv文件。

  public function export()
 {
 $ customers = \ CI :: Customers() - > get_customer_export(); 
 
 \ CI :: load() - > helper('  download_helper'); 
 force_download('customers.csv',($ customers)); 
} 
   
 
 

它生成并错误第二个参数应该是 字符串不是任何数组。如果我尝试json_encode $ customers变量,它会创建一个文件,但在csv文件中输出json代码。

感谢任何帮助

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dqvzfp6468 2017-04-06 06:54
    已采纳

    Try to use this function: first of all you have put this code in helper:

    function array_to_csv($array, $download = "")
    {   
        if ($download != "")
        {    
            header('Content-Description: File Transfer');
            header("Content-type: application/vnd.ms-excel");
            header('Content-Disposition: attachement; filename="' . $download . '"');
            header('Content-Transfer-Encoding: binary');
        }        
    
        ob_start();
        $f = fopen('php://output', 'w') 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
        {    
            print "\xEF\xBB\xBF"; // UTF-8 BOM
            print $str;
        }        
    }
    

    Array formation should be like:

    Array
    (
        [0] => Array
            (
                [0] => 1
                [1] => 0
                [2] => Test 1
            )
    
        [1] => Array
            (
                [0] => 2
                [1] => 1
                [2] => Test 1.1
            )
    
        [2] => Array
            (
                [0] => 3
                [1] => 1
                [2] => Test 1.2
            )
    
    )
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • donglan9517 2017-04-06 07:30

    You may also use a 'CI database utility class' for simplicity as follows:

    $this->load->dbutil(); 
    $query = $this->db->query("SELECT * FROM mytable"); 
    echo $this->dbutil->csv_from_result($query);
    

    Check the user guide below for more information https://www.codeigniter.com/userguide3/database/utilities.html

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题