Phpexcel用空字符串替换空值

时间:2017-08-04 21:28:19

标签: php excel codeigniter loops phpexcel

我注意到如果一个单元格为空,phpexcel会给它一个空值。有没有办法可以在循环数组并存储到数据库之前用空字符串替换空值。下面是我的代码,我正在使用codeigniter框架。

public function import(){
        $id = $this->session->userdata('company_id');

          $directoryname = get_company_full_name($id);
            if (!is_dir(FCPATH.'assets/customer_documents/Databases/'.$directoryname)) {
               mkdir(FCPATH.'assets/customer_documents/Databases/'.$directoryname, 0777, TRUE);
              }


        $database_name = $this->input->post('database_name');

        // $data['addressbook'] = $this->csv_model->get_addressbook($id);
        $data['errors'] = '';    //initialize  upload error array to empty

        $config['upload_path'] = FCPATH.'assets/customer_documents/Databases/'.$directoryname;
        $config['allowed_types'] = '*';
        $config['max_size'] = '';

        $this->upload->initialize($config);

        if (!$this->upload->do_upload()) {
            $data['errors'] = $this->upload->display_errors();

            $data['databases'] = $this->database->get_databases($id);

            $this->session->set_flashdata($data);
            redirect(base_url().'Databases');
        } else {
          $file_data = $this->upload->data();

          $file_ext = $file_data['file_ext'];

          // use custom function to determine if filetype is allowed
          if (allow_csv_type($file_ext))
          {

            $file_path =  FCPATH.'assets/customer_documents/Databases/'.$directoryname. '/' .$file_data['file_name'];


          //read file from path
          $objPHPExcel = PHPExcel_IOFactory::load($file_path);



          $date_uploaded = date("Y-m-d H:i:s");

            if ($objPHPExcel) {
                  //get only the Cell Collection
                  $cell_collection = $objPHPExcel->getActiveSheet()->getCellCollection();


                  //extract to a PHP readable array format
                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;
                    }
                }

                // array_shift($arr_data); // removes the 1st/header element

                //store the database name and details
                $database_data  = array(
                  'db_name'=>$database_name,
                  'company_id' => $id,
                  'deleted' => 0,
                  'date_uploaded'=> $date_uploaded
                );

                $this->database->insert_db($database_data);

                $database_id = $this->db->insert_id();


                foreach ($arr_data as $row) {

                      $insert_data = array(
                          'company_id' => $this->session->userdata('company_id'),
                          'number'=>$row['A'],
                          'province'=>$row['B'],
                          'district'=>$row['C'],
                          'ward'=>$row['D'],
                          'farming_type'=>$row['E'],
                          'commodity'=>$row['F'],
                          'database_name'=>$database_name,
                          'db_id' =>$database_id,
                          'deleted' => 0,
                          'date_uploaded'=>$date_uploaded
                      );
                    $this->database->insert_csv($insert_data, $id);
                  }

                 log_message('info', '*****************************Customer with the ID: '.$this->session->userdata('company_id').' and name: '.get_company_full_name($this->session->userdata('company_id')).' uploaded a csv database. The uploder phone number: '.$this->session->userdata('phone').' The database ID: '.$database_id.' The database name: '.$database_name.'*****************************');


                 $this->session->set_flashdata('success', 'Data Imported Succesfully');
                 redirect(base_url().'Databases');

        } else{
            $data['errors'] = "Error occured";
            $data['page_class'] = 'Databases-page';
            $data['title'] = 'Databases';
            $data['content_view'] = 'Databases/index';
            $this->template->admin_template($data);
          }
          }
           else {

              $this->session->set_flashdata('errors','File type is not allowed!');
               redirect('Databases');
            }

    }

  }

}

我正在寻找一种方法,我可以在空值上添加空值sting值。

1 个答案:

答案 0 :(得分:1)

在到达PHPExcel之前,只需使用一个简单的数组修饰符即可。

如果性能是一个问题,则可以考虑不要在array_map()中创建多个函数。请注意,isset()非常快,该解决方案根本不会调用任何其他函数。

$replacements = array(
    'search1' => 'replace1',
    'search2' => 'replace2',
    'search3' => 'replace3'
);
foreach ($a as $key => $value) {
    if (isset($replacements[$value])) {
        $a[$key] = $replacements[$value];
    }
}

请参阅:https://stackoverflow.com/a/32321702/4272537