Codeigniter - 获取视图中的表数据,发送表数据并在控制器上检索表数据

时间:2015-11-02 10:49:29

标签: php codeigniter phpexcel

我是Codeigniter的新手。我想知道如何在视图中获取表数据,通过ajax(可能)发送它,并在控制器中检索表数据(我想将表数据导出到excel文件中)。提前谢谢。

我的观点:

<table id="mytable">
                    <?php 
                        $i=1;
                        foreach ($placeout as $placeout_item){
                    ?>
                            <tr class="gradeX">
                                <td class="center"><?php echo $i; $i++; ?></td>
                                <td><?php echo $placeout_item['pjname']; ?></td>
                                <td><?php echo $placeout_item['entries_date']; ?></td>
                                <td><?php echo $placeout_item['lg_id']; ?></td>
                                <td class="right"><?php echo $placeout_item['entries_amt']; ?></td>
                                <td class="right"><?php echo $placeout_item['comm']; ?></td>
                                <td class="center"><?php echo $placeout_item['curr_id']; ?></td>
                            </tr>
                    <?php 
                        }
                    ?>
</table>
<button type="reset" class="btn btn-icon btn-default" onclick="exportExcel('Placeout')"><i></i>Export to Excel</button>

我的jquery:

<script>
var data_table = [];
$('#mytable td').each(function() {
    data_table.push($(this).html());   //make an array from table data
});

function exportExcel(name) {
    $.ajax(
    {
        url: "<?php echo site_url('export/excel/"+name+"'); ?>",
        type:'POST', //data type
        dataType : "json",         //this line should be erased???
        data : {tes:data_table}
    });
}
</script>

我的控制器:

function excel($name="")
{
    $data_table=$this->input->post('tes');

    $objPHPExcel = new PHPExcel();

    if($data_table!=null)
    {
        foreach ($data_table as $data)
        {
            $exceldata[] = $data;
        }
        $objPHPExcel->getActiveSheet()->fromArray($exceldata, null, 'A2');
    }
    header('Content-Type: application/vnd.ms-excel');
    header('Content-Disposition: attachment;filename="'.$name.'.xls"');
    header('Cache-Control: max-age=0');
    $objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5');                
    $objWriter->save('php://output');
}

2 个答案:

答案 0 :(得分:0)

我在控制器文件中有一些修改excel功能。

function excel($name="")
{
     $filename =  "excel_report.csv";   
     $fp = fopen('php://output', 'w');            
     header('Content-Type: text/csv; charset=utf-8');       
     header('Content-type: application/csv');
     header('Content-Disposition: attachment; filename='.$filename );

    $data_table=$this->input->post('tes');

    if($data_table!=null)
    {
        foreach ($data_table as $data)
        {
            $exceldata = array(
                    $data['column_name_1'],   // dummy column name here 
                    $data['column_name_2'],
                    $data['column_name_3'],
                    $data['column_name_4'],
                    );
            fputcsv($fp, $exceldata );
        }

    }
 die(); 

}

答案 1 :(得分:0)

你的jquery应该是这样的:

 <script>
 var data_table = [];
 $('#mytable tr').each(function() {
    var tr = [];
    $(this).find('td').each(function(){
       tr.push($(this).html())
    });
    data_table.push(tr);   //make an array from table data
 });

 function exportExcel(name) {
     $.ajax(
     {
         url: "<?php echo site_url('export/excel/"+name+"'); ?>",
         type:'POST', //data type
         dataType : "json",         //this line should be erased???
         data : {tes:data_table}
     });
 }
 </script>