Codeigniter - 上传xls或xlsx并将数据发送到当前视图

时间:2017-04-01 06:31:29

标签: excel codeigniter upload

我长期坚持这个问题。我有一个包含学生和学生成绩列表的页面。我想在当前页面添加一个按钮来上传xls / xlsx文件,在我上传文件后,列数据填写学生的分数输入。我怎样才能做到这一点?我需要帮助。

查看

<form action="<?php echo base_url()?>index.php/nilai/ubahNilaiItem/<?php echo $idKelas ?>/<?php echo $id ?>" method="post" class="form-horizontal">
            <button name="tombol" type="submit" value="import" class="btn pull-right btn-primary">Import</button>
            <input type="file" name="inputfile" size="20" class="pull-right"/>
            </div>
            <div class="col-lg-12">
                 <table class="table">
                    <thead>
                        <tr>
                            <th class="col-md-1 active">No</th>
                            <th class="col-md-2 active">NIM</th>
                            <th class="col-md-5 active">Nama</th>
                            <th class="col-md-1 active">Nilai</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php 
                            $no = 0;
                            foreach($input as $i){ 
                                $no++
                        ?>
                        <tr>
                            <td><?php echo $no ?></td>
                            <td><?php echo $i->nim ?></td>
                            <td><?php echo $i->nama ?></td>      
                            <td>
                                <div class="input-group">
                                <input hidden value="<?php echo $i->id ?>" name="id[]">
                                <input class="text-center form-control" value="<?php echo $i->angka_nilai ?>" name="angka[]"> //this is for inputing student score 
                                <span class="input-group-addon"><i class="glyphicon glyphicon-question-sign" data-toggle="tooltip" title="Gunakan koma (,) untuk nilai desimal"></i></span>
                                </div>
                            </td>
                        </tr>
                        <?php } ?>
                    </tbody>
                </table>
          <button name="tombol" value="simpan" type="submit" class="btn pull-right btn btn-primary">Simpan</button>
 </form>

视图的控制器

public function itemView($idKelas,$id){
    //load model
    $this->load->model('nilais');

    //take data from database
    $data2['input'] = $this->nilais->input_item($idKelas,$id)->result();
    $data3['detail'] = $this->nilais->tampil_detail($idKelas)->result();

    //merge array
    $data4 = array_merge($data2, $data3);
    $data4['item'] = $this->nilais->item_detail($id)->result();
    $data4['idKelas'] = $idKelas;
    $data4['id'] = $id;

    //take data for header and notification
    $data6['notifikasi'] = $this->nilais->notifikasi($this->session->userdata('id_user'))->result();
    $data6['count'] = count($data6['notifikasi']);
    $data6['matakuliah'] = $idKelas;
    $data6['active'] = 'nilai';

    //load view nilai and header
    $this->load->view('nilai/item_view',$data4);
    $this->load->view('header',$data6);
}

更新 现在我使用PHPExcel,但我不能得到空单元格。即使它是null,我如何获得所有细胞?

excel的控制器

public function importAction($idKelas,$id){
    //load model
    $this->load->model('nilais');
    $this->load->library('excel');

    $config['upload_path']          = './file/excel/';
    $config['allowed_types']        = 'xls|xlsx';
    $config['max_size']             = 0;

    $this->load->library('upload', $config);
    if ($this->upload->do_upload('inputfile')) {
        $upload_data = $this->upload->data();
        $objPHPExcel = PHPExcel_IOFactory::load($upload_data['full_path']);
        $cell_collection = $objPHPExcel->getActiveSheet()->getCellCollection();

        foreach ($cell_collection as $cell) {
            $column = $objPHPExcel->getActiveSheet()->getCell($cell)->getColumn();
            $row = $objPHPExcel->getActiveSheet()->getCell($cell)->getRow();
            $data_value = $objPHPExcel->getActiveSheet()->getCell($cell)->getValue();
            //header will/should be in row 1 only. of course this can be modified to suit your need.
            if ($row == 1) {
                $header[$row][$column] = $data_value;
            } else {
                $arr_data[$row][$column] = $data_value;
            }
        }
        //send the data in an array format
        $data['header'] = $header;
        $data['values'] = $arr_data;

     }
}

0 个答案:

没有答案