简体   繁体   English

CodeIgniter:将数据从数据库导出到excel中并下载

[英]CodeIgniter: Export data from database into excel and download

I'm working on a project, that requires exporting data from MYSQL DB depending on the multiple conditions. 我正在开发一个项目,该项目需要根据多种条件从MYSQL DB导出数据。 I am referring this : 我指的是这个

This is my source code: 这是我的源代码:

public function exportExcelData($records)
{
  $heading = false;
        if (!empty($records))
            foreach ($records as $row) {
                if (!$heading) {
                    // display field/column names as a first row
                    echo implode("\t", array_keys($row)) . "\n";
                    $heading = true;
                }
                echo implode("\t", ($row)) . "\n";
            }
 }

public function fetchDataFromTable()
{
     $query =$this->db->get('one_piece_characters'); // fetch Data from table
     $allData = $query->result_array();  // this will return all data into array
     $dataToExports = [];
     foreach ($allData as $data) {
        $arrangeData['Charater Name'] = $data['name'];
        $arrangeData['Charater Profile'] = $data['profile'];
        $arrangeData['Charater Desc'] = $data['description'];
        $dataToExports[] = $arrangeData;
  }
  // set header
  $filename = "dataToExport.xls";
                header("Content-Type: application/vnd.ms-excel");
                header("Content-Disposition: attachment; filename=\"$filename\"");
  $this->exportExcelData($dataToExports);
 }

If I use it without any where clause, it is giving entire table. 如果我不带任何where子句使用它,那么它将提供整个表。

If i use only single where clause, it works good. 如果我仅使用单个where子句,则效果很好。

Bur, if I use multiple where conditions. Bur,如果我使用多个where条件。 it gives me a blank excel sheet. 它给了我一张空白的Excel工作表。

Does anyone have any idea regarding how to make it work with multiple where conditions? 有人对如何使其在多个条件下工作有任何想法吗?

Try using this code on your model file: 尝试在模型文件上使用以下代码:

function createcsv(){
        $this->load->dbutil();
        $this->load->helper('file');
        $this->load->helper('download');
        $delimiter = ",";
        $newline = "\r\n";
        $filename = "filename.csv";
        $query = "SELECT * FROM YourTable"; //USE HERE YOUR QUERY
        $result = $this->db->query($query);
        $data = $this->dbutil->csv_from_result($result, $delimiter, $newline);
        force_download($filename, $data);

    }

// ecport contect list in csv , //在csv中将接触列表列表化,

public function cnt_explode(){

$csvData[] =array(  "Name", "Email Id","Mobile No.","Event", "City","location","No of guest","Event Date","Budget",
                    "Venue Type","Food","Drink","Description","Date");
$data = $this->contact_model->get_contact(NULL);

foreach($data as $cnt){
$csvData[]=array(
      $cnt->name ,$cnt->email, $cnt->mobile_no, $cnt->event, $cnt->city,  $cnt->location,  $cnt->no_guest,$cnt->event_date,$cnt->budget, $cnt->venue_type,
      $cnt->food, $cnt->drink, $cnt->description,$cnt->created_on,
      );
}

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment;filename=venuexpo_".time().".csv");
header("Content-Transfer-Encoding: binary");
$df = fopen("php://output", 'w');
array_walk($csvData, function($row) use ($df) {
  fputcsv($df, $row);
});
fclose($df);
}

///------------ downlode csv done --------------

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM