在codeigniter中使用PHPExel导入Excel文件

r3i60tvu  于 2023-02-27  发布在  PHP
关注(0)|答案(1)|浏览(118)

好的,我尝试使用此代码插入数据,如您所见,一切正常,但仍会遇到问题,例如,每当我插入一行时,它会将我重定向回“拒绝访问”,并且无法插入数据,而当我在Excel文件中插入两行时,它会立即插入数据
如果(isset($_文件[雇员文件])){ $文件= $_文件[雇员文件][临时工名称];

$this->load->library('PHPExcel');
  
 $excel = PHPExcel_IOFactory::load($file);
 // $sheet = $excel->getSheet(0);
 foreach($excel->getWorksheetIterator() as $worksheet)
 {
 $highestRow = $worksheet->getHighestRow();
 $highestColumn = $worksheet->getHighestColumn();
 for($row=2; $row<=$highestRow; $row++)
 {
 $data[] = array(
 'first_name'  => ($cellValue = $worksheet->getCellByColumnAndRow(0, $row)->getValue()) == "" ? null : $cellValue,
 'last_name'  =>$worksheet->getCellByColumnAndRow(1, $row)->getValue(),
 'enroll_id'  =>$worksheet->getCellByColumnAndRow(2, $row)->getValue(),
 'nic'  =>$worksheet->getCellByColumnAndRow(3, $row)->getValue(),
 'job_title_id'  =>$worksheet->getCellByColumnAndRow(4, $row)->getValue(),
 'join_date'  =>$worksheet->getCellByColumnAndRow(5, $row)->getValue(),
 'loc_id'  =>$worksheet->getCellByColumnAndRow(6, $row)->getValue(),
 'supervisor_id'  =>$worksheet->getCellByColumnAndRow(7, $row)->getValue(),
 'dob'  =>$worksheet->getCellByColumnAndRow(8, $row)->getValue(),
 'dept_id'  =>$worksheet->getCellByColumnAndRow(9, $row)->getValue(),
 'email'  =>$worksheet->getCellByColumnAndRow(10, $row)->getValue(),
 'official_email'  =>$worksheet->getCellByColumnAndRow(11, $row)->getValue(),
 'phone'  =>$worksheet->getCellByColumnAndRow(12, $row)->getValue(),
 'address'  =>$worksheet->getCellByColumnAndRow(13, $row)->getValue(),
 'marital_status'  =>$worksheet->getCellByColumnAndRow(14, $row)->getValue(),
 'gender'  =>$worksheet->getCellByColumnAndRow(15, $row)->getValue(),
 'rel_name'  =>$worksheet->getCellByColumnAndRow(16, $row)->getValue(),
 'rel_relation'  =>$worksheet->getCellByColumnAndRow(17, $row)->getValue(),
 'rel_contact'  =>$worksheet->getCellByColumnAndRow(18, $row)->getValue(),
 'employment_status'  =>$worksheet->getCellByColumnAndRow(19, $row)->getValue(),
 'date_of_confirmation'  =>$worksheet->getCellByColumnAndRow(20, $row)->getValue(),
 'gross_salary'  =>$worksheet->getCellByColumnAndRow(21, $row)->getValue(),
 'dept_role'  =>$worksheet->getCellByColumnAndRow(22, $row)->getValue(),
 );
 }
 }
        
// array_shift($data);
$this->SqlModel->insert($data); 
}

 if ($this->db->affected_rows() > 0) {
 redirect('employees/index/success');
 } else {
 redirect(base_url().'accessdenied'); 
 }
wvt8vs2t

wvt8vs2t1#

我解决了这个问题。实际上问题是我没有使我的列在数据库中为空,这是它不是插入单行的excel文件,但当我使所有列为空,它开始完美地工作。

相关问题