显示已更新的行数并从Excel文件中插入

时间:2017-08-29 13:14:28

标签: php phpexcel

我想显示单独插入和更新的行的值。我正在使用PHPExcel上传我的服务器上的文件并读取行以插入或更新数据库中的值。我目前有一个4500行的文件,但变量总是最终为0.(Codeigniter)

模型

//获取值

function fi_upload($file_name){
$file = './uploads/'.$file_name;


$this->load->library('excel');

$objPHPExcel = PHPExcel_IOFactory::load($file);

$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();
        if ($row == 1) {
            $header[$row][$column] = $data_value;
        }else{
            $arr_data[$row][$column] = $data_value;
        }
    }

    $data['header'] = $header;
    $data['values'] = $arr_data;
    $insert =0;
$update =0;
$crinsert =0;
$crupdate=0;

foreach ($arr_data as $q){

        if($q['A']==NULL){
        continue;
        }else{

            $data = array(
                //insert columns and values
                );

            if($q['G']=="CR"){

                $this->db->select('*');
                $this->db->where('ledger_cust',$q['A']);
                $this->db->where('ledger_date',$insertLedgerDate);
                $this->db->where('ledger_assign',$q['D']);
                $this->db->where('ledger_cr_post',$q['O']);
                $query = $this->db->get('tbl_ledger');
                if($query->num_rows() > 0){
                                    $this->db->where('ledger_cust',$q['A']);
                                    $this->db->where('ledger_date',$insertLedgerDate);
                                    $this->db->where('ledger_assign',$q['D']);
                                    $this->db->where('ledger_cr_post',$q['O']);
                                    $this->db->update('tbl_ledger',$data);
                                    $crupdate++;

                                    }else{
                                     $this->db->insert('tbl_ledger',$data);
                                     $crinsert++;

                                    }

              }else{
                $this->db->select('*');
                $this->db->where('ledger_cust',$q['A']);
                $this->db->where('ledger_doc_no',$q['H']);
                $this->db->where('ledger_balance',$q['P']);

                $query2 = $this->db->get('tbl_ledger');
                            if($query2->num_rows() > 0){
                                    $this->db->where('ledger_cust',$q['A']);
                                    $this->db->where('ledger_doc_no',$q['H']);
                                    $this->db->where('ledger_balance',$q['P']);
                                    $this->db->where('ledger_db_post',$q['N']);
                                    $this->db->update('tbl_ledger',$data);
                                    $update++;
                                  }else{
                                 $this->db->insert('tbl_ledger',$data);
                                 $insert++;
                                 }
                    }
                }
}
$insert = $insert + $crinsert;

$update = $update + $crupdate;

echo $insert;
echo "<br>";
echo $update;
$msg = "Ledger Successfully Uploaded. <br> New Entries  ".$insert." <br>Updated Entries ".$update;
return $msg;
}

如果行数较少,则会成功显示该值。 提前谢谢!

0 个答案:

没有答案