phpexcel codeigniter验证检查一行中的单元格是否为空

时间:2017-06-14 07:50:43

标签: php codeigniter phpexcel codeigniter-3

我使用phpexcel在我的codeigniter 3中“从excel导入”功能,但是我想创建一个验证系统来检查某些单元格是否为空,并且可以在导入数据之前检测哪个列和行是进入我的数据库。如果检测到,导入功能应该停止。

这是控制器:

public function do_upload(){


    $config['upload_path'] = './tempDat/';
    $config['allowed_types'] = 'xlsx|xls';

    $this->load->library('upload', $config);

    if ( ! $this->upload->do_upload()){
        $error = array('error' => $this->upload->display_errors());
    }
    else{
        $data = array('upload_data' => $this->upload->data());
        $upload_data = $this->upload->data(); 
        $filename = $upload_data['file_name'];
        $this->client->upload_data($filename);
      //  unlink('./tempDat/'.$filename);
        redirect('client/upload','refresh');
    }
}

这是我已经尝试的模型:

public function upload_data($filename){
    ini_set('memory_limit', '-1');
    $inputFileName = './tempDat/'.$filename;
    try {
    $objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
    } catch(Exception $e) {
    die('Error loading file :' . $e->getMessage());
    }

    $worksheet = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);

    $numRows = count($worksheet);

    $objWorksheet = $objPHPExcel->getActiveSheet();
    $highestRow = $objWorksheet->getHighestRow(); 
    $highestColumn = $objWorksheet->getHighestColumn(); 


    for ($i=2; $i < ($numRows+1) ; $i++) { 

       $test = $objPHPExcel->getActiveSheet()->rangeToArray('A1:A'.$i);
 if( $test != NULL ) {  

        $ins = array(
                "eng_code"          => $worksheet[$i]["B"],
                "name_1"          => $worksheet[$i]["C"],
                "name_2"          => $worksheet[$i]["D"],
                "divisi"          => $worksheet[$i]["E"],
                "title"          => $worksheet[$i]["F"],
                "sex"                       => $worksheet[$i]["G"],
                "date_of_birth"          => $worksheet[$i]["H"],
                "date_of_commencement"          => $worksheet[$i]["I"],
                "salary_min_1"          => $worksheet[$i]["J"],
                "allowance_min_1"          => $worksheet[$i]["K"],
                "salary"          => $worksheet[$i]["L"],
                "allowance"          => $worksheet[$i]["M"],
                "termination_date"          => $worksheet[$i]["N"],
                "type_of_termination"          => $worksheet[$i]["O"],
                "manfaat_termination"          => $worksheet[$i]["P"],
                "basic_salary_at"          => $worksheet[$i]["Q"],
                "basic_allowance_at"          => $worksheet[$i]["R"],
                "status_employee_min_1"          => $worksheet[$i]["S"],
                "status_employee"          => $worksheet[$i]["T"],


               );


         $this->db->insert('tb_eec', $ins);
    } else {

        if( $test == NULL || $test == '') {

         $this->session->set_flashdata('pesan', '<div style="color:blue;"><b>Kolom '.$worksheet[1]["A"].' Baris '.$i.' Belum diisi !</b></div>');

         }
    }
    }
}

我尝试了很多不同的方法,但结果总是一样的,即使某些单元格为空,我的函数也会继续导入数据。

请帮助我:)

1 个答案:

答案 0 :(得分:0)

我是否正确地认为这就是你所追求的目标?

$inserts = array();

for ($i=2; $i < ($numRows+1) ; $i++) { 

    $test = $objPHPExcel->getActiveSheet()->rangeToArray('A1:A'.$i);

    $do_insert = true;

    foreach (range('B','T') as $let){
        if (empty($worksheet[$i][$let])){
            $do_insert = false;
        }
    }

    if ($do_insert){
        $inserts[] = array(
            "eng_code"          => $worksheet[$i]["B"],
            "name_1"          => $worksheet[$i]["C"],
            "name_2"          => $worksheet[$i]["D"],
            "divisi"          => $worksheet[$i]["E"],
            "title"          => $worksheet[$i]["F"],
            "sex"                       => $worksheet[$i]["G"],
            "date_of_birth"          => $worksheet[$i]["H"],
            "date_of_commencement"          => $worksheet[$i]["I"],
            "salary_min_1"          => $worksheet[$i]["J"],
            "allowance_min_1"          => $worksheet[$i]["K"],
            "salary"          => $worksheet[$i]["L"],
            "allowance"          => $worksheet[$i]["M"],
            "termination_date"          => $worksheet[$i]["N"],
            "type_of_termination"          => $worksheet[$i]["O"],
            "manfaat_termination"          => $worksheet[$i]["P"],
            "basic_salary_at"          => $worksheet[$i]["Q"],
            "basic_allowance_at"          => $worksheet[$i]["R"],
            "status_employee_min_1"          => $worksheet[$i]["S"],
            "status_employee"          => $worksheet[$i]["T"],
        );
    }
}

if (count($inserts)){
    foreach ($inserts as $ins){
        $this->db->insert('tb_eec', $ins);
    }
} else {
    $this->session->set_flashdata('pesan', '<div style="color:blue;"><b>Kolom '.$worksheet[1]["A"].' Baris '.$i.' Belum diisi !</b></div>');
}