【问题标题】:CodeIgniter: Export data from database into excel and downloadCodeIgniter:将数据从数据库导出到 excel 并下载
【发布时间】:2017-02-09 22:13:09
【问题描述】:

我正在做一个项目,该项目需要根据多个条件从 MYSQL DB 导出数据。我指的是this:

这是我的源代码:

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);
 }

如果我在没有任何 where 子句的情况下使用它,它会给出整个表。

如果我只使用一个 where 子句,效果很好。

Bur,如果我使用多个 where 条件。它给了我一张空白的 Excel 表格。

有没有人知道如何让它在多个 where 条件下工作?

【问题讨论】:

  • 您确定您的数据库中有任何适合您的multiple conditions 的数据吗?
  • 显示你的 where 条件?
  • 尝试 echo $this->db->last_query() 与您的多个条件并在您的 databasephpmyadmin 中执行相同的查询并检查您是否得到任何结果
  • @Zeeshan 查询看起来像这样 select * from dept where deptid=5 and deptname="motor" 并且它不返回任何数据。它给出了一个空白的 Excel 表
  • 您是否尝试过直接在数据库中运行相同的查询,如果可以得到任何结果

标签: php mysql excel codeigniter export-to-excel


【解决方案1】:

尝试在您的模型文件中使用此代码:

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);

    }

【讨论】:

    【解决方案2】:

    // csv中的ecport联系人列表,

    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 --------------
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-15
      • 1970-01-01
      • 1970-01-01
      • 2017-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多