如何使用导出按钮在codeigniter中将数据库表导出到xl表单中,然后单击视图

时间:2015-05-26 12:05:00

标签: php mysql excel codeigniter

我有关于codeigniter和php的问题,如何导出数据 表进入excel表。

这是我的观看代码。

<div class="tab-pane active fade in" id="all">                              
    <table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered datatable" id="example">
        <thead>
            <tr>
                <td class="hidden">1</td>
                <th>Name of Employee</th>
                <th>State</th>
                <th>Zone</th>
                <th>Quiz</th>
                <th class="hidden-xxs">Assessment</th>
                <th class="hidden-xxs">Score</th>
                <th class="hidden-xs">Date</th>
                <th class="text-center" style=" width: 10%">Action</th>
            </tr>
        </thead>
        <tbody>
        <?php 
        $i = 1;
        foreach ($results as $result) { 
           if (($result->exam_title_user_id == $this->session->userdata('user_id')) OR ($this->session->userdata('user_role_id') <= 3)) { ?>
            <tr class="<?= ($i & 1) ? 'even' : 'odd'; ?>">
                <td class="hidden">1</td>
                <td><?= $result->user_name; ?></td>
                <td><?= $result->state_name; ?></td>
                <td><?= $result->user_zone_name; ?></td>
                <td><?= $result->title_name; ?></td>    
                <td class="hidden-xxs"><?= ($result->result_percent >= $result->pass_mark) ? '<span class="label label-primary">PASS</span>' : '<span class="label label-warning">FAIL</span>' ?></td>
                <td class="hidden-xxs"><?php echo $result->result_percent; ?>%</td>
                <td class="hidden-xs"><?= date("D, d M", strtotime($result->exam_taken_date)); ?></td>
                <td class="text-center" style=" width: 25%">
                    <div class="btn-group">
                        <a class="btn btn-default btn-sm" href = "<?= base_url('exam_control/view_my_result_detail/' . $result->result_id); ?>"><i class="glyphicon glyphicon-eye-open"></i><span class="invisible-on-md">  View</span></a>
                        <a class="btn btn-default btn-sm" href = "<?= base_url('exam_control/view_detailed_result/' . $result->result_id); ?>"><i class="glyphicon glyphicon-eye-open"></i><span class="invisible-on-md">  View Details</span></a>
                        <?php if ($this->session->userdata['user_role_id'] <= 2) { ?>
                            <a onclick="return delete_confirmation()" href = "<?= base_url('exam_control/delete_results/' . $result->result_id); ?>" class="btn btn-sm btn-default" ><i class="glyphicon glyphicon-trash"></i><span class="invisible-on-md">  Delete </span></a>
                        <?php } ?>
                    </div>
                </td>
            </tr>
            <?php $i++;
            }
        }
        ?>
        </tbody>
    </table>
    </div>

1 个答案:

答案 0 :(得分:0)

我使用简单的fputcsv函数将数据导出为CSV格式。

这是我的控制器代码

 public function all_emp_result_export($message = '')
{       
            header('Content-Type: text/csv; charset=utf-8');
            header('Content-Disposition: attachment; filename=employeeResult.csv');

            // Create a file pointer connected to the output stream
            $output = fopen('php://output', 'w');

            // Output the column headings
            fputcsv($output, array(
                'Employee Name',
                'State',
                'Zone',
                'Quiz',
                'Assesssment',
                'Score %',
                'Date'
            ));

                $b = $this->db->select('*')
                        ->select("users.user_id AS user_id")
                        ->select("result.user_id AS result_user_id")
                        ->select("exam_title.user_id AS exam_title_user_id")
                        ->from('result')
                        ->order_by("result.exam_taken_date", "desc")
                        ->join('users', 'users.user_id = result.user_id', 'left')
                        ->join('exam_title', 'exam_title.title_id = result.exam_id', 'left')
                        ->join('states', 'users.state = states.state_id', 'left')
                        ->join('user_zone', 'users.user_zone = user_zone.user_zone_id', 'left')
                        ->get()
                        ->result();

            foreach($b as $a) {
            //while($a = mysql_fetch_assoc($b)) {
            //foreach($b->result_array() as $a){
                $items_array = array();

                $items_array[] = $a->user_name;
                $items_array[] = $a->state_name;
                $items_array[] = $a->user_zone_name;
                $items_array[] = $a->title_name;
                $items_array[] = ($a->result_percent >= $a->pass_mark) ? PASS : FAIL ;
                $items_array[] = $a->result_percent;
                $items_array[] = date("D, d M", strtotime($a->exam_taken_date));

                // Output the Data to the CSV
                fputcsv($output, $items_array);
            }
}

我们需要从视图表中调用此控制器以将数据导出到excel