使用带内容连接3表的codeigniter自动下载MS Excel文件

By simon at 2018-02-28 • 0人收藏 • 60人看过

[![enter image description 这里](https://i.stack.imgur.com/2IXSw.png)(https://i.stack.imgur.com/2IXSw.png) 我怎样才能做到这一点自动下载MS Excel的源代码,但是 MS excel结果的内容是从数据库中的3个表中连接起来的? 调节器

public function laporanex()
{
    $this->load->model('cetak_model');
    $this->load->dbutil();
    $this->load->helper('file');
    /* get the object   */
    $report = $this->cetak_model->ex();
    /*  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('simokja.csv',$new_report);
    /*  Done    */
}
模型
public function ex(){

    $this->db->select('c.nama, a.*, b.*')
            ->from('user c')
            ->join('pekerjaan a', 'c.id = a.id')
            ->join('progress b', 'a.no_spk = b.no_spk');

    $query = $this->db->get();
    return $query->result();
} 

4 个回复 | 最后更新于 2018-02-28
2018-02-28   #1

尝试这个, 控制器:

public function report(){
        $result['report_data'] = $this->Homemodel->get_report(); 
         $this->load->view('admin/reports',$result);

    }
型号:
public function get_report(){
    $this->db->select('A.*,B.*,C.*')
             ->from('A')
             ->join('B','B.bid=A.id')
             ->join('c','c.cid=A.id')
             ->order_by('A.created_on', 'Desc');
                $query = $this->db->get();
                return $query->result_array();
}
视图:
<?php 
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=exceldata.xls");
header("Pragma: no-cache");
header("Expires: 0");

?>

<table border='1'>
  <tr>
<th>Id</th>
<th>Name</th>
<th>Email</th>
<th>Phone Number</th>
 </tr>

 <?php
$i =1;
 foreach($report_data as $data) { 
    ?>
    <tr>


<td><?php echo $i; ?></td>
<td><?php echo $data["name"]; ?></td>
<td><?php echo $data["email"]; ?></td>
<td><?php echo $data["mobile"]; ?></td>

</tr>
<?php   
$i++;
}?>
</table>

2018-02-28   #2

    $this->load->model('cetak_model');
    $data= $this->cetak_model->ex();
    header('Content-Type: text/csv; charset=utf-8');
    header('Content-Disposition: attachment; filename=data.csv');
    $output = fopen('php://output', 'w');
    fputcsv($output, array('Column1',
                           'Column2',
                           'Column3',
                           'Column4',
                           'Column5'
                        ));
    for($i=0;$i<count($data);$i++)
    {
        $row[0] = $data[$i]['Column1'];
        $row[1] = $data[$i]['Column2'];
        $row[2] = $data[$i]['Column3'];
        $row[3] = $data[$i]['Column4'];
        $row[4] = $data[$i]['Column5'];
        fputcsv($output, $row);
    }

2018-02-28   #3

这是我的控制器

public function laporanex()
{
    $this->load->model('cetak_model');
    header('Content-Type: text/csv; charset=utf-8');
    header('Content-Disposition: attachment; filename= simokja.csv');
    $output = fopen('php://output', 'w');
    fputcsv($output, array('Column1',
                           'Column2',
                           'Column3',
                           'Column4',
                           'Column5'
                        ));
    for($i=0;$i<count($report);$i++)
    {
        $row[0] = "Sold";
        $row[1] = $data[$i]['Column1'];
        $row[2] = $data[$i]['Column2'];
        $row[3] = $data[$i]['Column3'];
        $row[4] = $data[$i]['Column4'];
        $row[4] = $data[$i]['Column5'];
        fputcsv($output, $row);
    }
}
模型
public function ex(){

    $this->db->select('c.nama, a.*, b.*')
            ->from('user c')
            ->join('pekerjaan a', 'c.id = a.id')
            ->join('progress b', 'a.no_spk = b.no_spk');

    $query = $this->db->get();
    return $query->result();
} 
并在图像的结果![enter image description 这里](https://i.stack.imgur.com/SXmBW.png)

2018-02-28   #4

尝试这个, 控制器:

public function report(){
        $result['report_data'] = $this->Homemodel->get_report(); 
         $this->load->view('admin/reports',$result);

    }
型号:
public function get_report(){
    $this->db->select('A.*,B.*,C.*')
             ->from('A')
             ->join('B','B.bid=A.id')
             ->join('c','c.cid=A.id')
             ->order_by('A.created_on', 'Desc');
                $query = $this->db->get();
                return $query->result_array();
}
视图:
<?php 
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=exceldata.xls");
header("Pragma: no-cache");
header("Expires: 0");

?>

<table border='1'>
  <tr>
<th>Id</th>
<th>Name</th>
<th>Email</th>
<th>Phone Number</th>
 </tr>

 <?php
$i =1;
 foreach($report_data as $data) { 
    ?>
    <tr>


<td><?php echo $i; ?></td>
<td><?php echo $data["name"]; ?></td>
<td><?php echo $data["email"]; ?></td>
<td><?php echo $data["mobile"]; ?></td>

</tr>
<?php   
$i++;
}?>
</table>

登录后方可回帖

Loading...