1
function export_csv(){
        $this->load->helper('csv');
        if(isset($_POST['term_val'])&&$_POST['term_val']<>'0'){

        $term   = $this->Search_model->get_term($_POST['term_val']);
        $term   = json_decode($term);
        }else{
        $term=array();
        }


     $orders    = $this->Order_model->get_orders_export($term,'place_order');

    if(count($orders)>0){
    foreach($orders as $order){
    $sublist['order_id']= $order->order_number;
    $sublist['ship_to']= $order->ship_firstname.' '.$order->ship_lastname;
    $sublist['order_date']= date('d-m-Y H:i:s',strtotime($order->ordered_on));
    $sublist['email_to']= $order->ship_email;
    $sublist['city']= $order->ship_city;
    $sublist['pincode']= $order->ship_zip;
    $sublist['ship_address']= $order->ship_address1.' , '.$order->ship_address2;
    $sublist['phone']= $order->ship_phone;
        $sublist['product_name']= $order->name;
    $sublist['product_id']= $order->product_id;
    $sublist['status']= $order->status;
    $sublist1[]= $sublist;
    }
    $delimiter = ";";
    $newline = "\r\n";
    $heading[]=array('order_id'=>'Order Id','ship_to'=>'Ship To','order_date'=>'Order Date','email_to'=>'Email To',
    'city'=>'City','pincode'=>'Pincode','ship_address'=>'Ship Address','phone'=>'Phone','product_name'=>'Product Name','product_id'=>'Product ID','status'=>'status');

    $get_csv=array_to_csv1($sublist1,$heading,$delimiter, $newline); 
    ob_start();
  $filename = "orders_" . date('Ymd') . ".xls";
  header("Content-Disposition: attachment; filename=\"$filename\"");
  header("Content-Type: application/vnd.ms-excel");


//  header("Content-type: text/x-csv");
    //header("Content-type: text/csv");
//  header("Content-type: application/csv");
//  header("Content-Disposition: attachment; filename=orders".date('d-M-Y').".csv");    
    print_r($get_csv); 
    }else{

    redirect($this->config->item('admin_folder').'/orders');    
    }
    }

在此处输入图像描述

上面的代码是导出 CSV 的控制器功能,图像是导出时发生的动作。

问题:实际上,如果我们使用 codeigniter 导出 csv,它会显示如上所示的图像。如果我们使用 codeigniter 按下 export csv 它不应该显示该图像,它应该直接将 csv 导出到 excel 中。

4

2 回答 2

2

如果您想生成 csv 格式的报告,使用 codeigniter 非常容易。你的模型函数

function index(){
   return $query = $this->db->get('my_table');
   //Here you should note i am returning 
   //the query object instead of 
   //$query->result() or $query->result_array()
}    

现在在控制器中

function get_report(){
  $this->load->model('my_model');
  $this->load->dbutil();
  $this->load->helper('file');
  // get the object
  $report = $this->my_model->index();
  //pass it to db utility function
  $new_report = $this->dbutil->csv_from_result($report);
 //Now use it to write file. write_file helper function will do it
 write_file('csv_file.csv',$new_report);
 //Done
}

Codeigntier 中的所有内容都不需要外部设备。上述方法将强制下载文件而不是打开文件。干杯! 如果你想写 xml 文件也很容易。只需使用 dbutil 的 xml_from_result() 方法并使用 write_file('xml_file.xml,$new_report) 访问这些链接会有所帮助。

数据库实用程序类

文件助手

于 2013-03-16T07:50:27.070 回答
-1
***> if you are trying to generate reports in csv format then it will quite
> easy with codeigniter.***

将此代码放在您的控制器中

function get_report()
    {
        $this->load->model('Main_Model');
        $this->load->dbutil();
        $this->load->helper('file');
        /* get the object   */
        $report = $this->Main_Model->print_report();
        $delimiter = ",";
        $newline = "\r\n";
        $new_report = $this->dbutil->csv_from_result($report, $delimiter, $newline);
        write_file( 'application/third_party/file.csv', $new_report);
        $this->load->view('report_success.php');
   }

将此代码放入模型

public function print_report()
    {
        return $query = $this->db->query("SELECT * FROM Table_name");
    } 

report_success.php 只是成功通知。

您的报告正在导出。谢谢


最后生成您的“file.csv”。它基本上存储在物理存储中。在 CodeIgniter/application/third-party/file.csv


有用。它会帮助你。

于 2015-01-20T10:39:26.290 回答