如何在CodeIgniter中用PHPExcel实现Excel导入后的数据更新?
Excel数据导入MySQL后的增量更新实现
我已经通过PHPExcel成功将Excel数据导入MySQL数据库,现在需要实现导入后的数据更新功能:数据库中现有32条数据,再次导入Excel时总数保持32条,若Excel里的数据有变更,则同步更新数据库对应记录。
原控制器代码
function import() { if(isset($_FILES["file"]["name"])) { $path = $_FILES["file"]["tmp_name"]; $object = PHPExcel_IOFactory::load($path); foreach($object->getWorksheetIterator() as $worksheet) { $highestRow = $worksheet->getHighestRow(); $highestColumn = $worksheet->getHighestColumn(); for($row=16; $row<=$highestRow; $row++) { $studentID = $worksheet->getCellByColumnAndRow(1, $row)->getValue(); $name = $worksheet->getCellByColumnAndRow(2, $row)->getValue(); $grade = $worksheet->getCellByColumnAndRow(5, $row)->getValue(); $subject = $worksheet->getCellByColumnAndRow(2, 8)->getValue(); if(isset($studentID)){ if($name != null){ $data[$row] = array( 'studentID' => $studentID, 'name' => $name, 'grade' => $grade, 'subject' => $subject ); } }else{ if($name != null){ $dataUpdate[$row] = array( 'studentID' => $studentID, 'grade' => $grade, 'subject' => $subject ); } } } } if(isset($data)){ $this->loading_model->insert($data); } if(isset($dataUpdate)){ echo 'Something'; } echo 'Data Imported successfully'; } }
原模型代码
function insert($data) { $this->db->insert_batch('tbl_college_grades', $data); } function update($data) { $this->db->update_batch('tbl_college_grades', $data); }
解决方案
核心思路
以studentID作为唯一标识,判断数据库中是否存在对应记录:
- 存在则更新该记录的
name、grade、subject字段 - 不存在则新增(如果需要兼容新增场景,若严格要求总数不变,可跳过新增逻辑)
修改后的控制器代码
function import() { if(isset($_FILES["file"]["name"])) { $path = $_FILES["file"]["tmp_name"]; $object = PHPExcel_IOFactory::load($path); $insertData = []; $updateData = []; foreach($object->getWorksheetIterator() as $worksheet) { $highestRow = $worksheet->getHighestRow(); // 提前读取科目,避免循环内重复IO操作 $subject = $worksheet->getCellByColumnAndRow(2, 8)->getValue(); for($row=16; $row<=$highestRow; $row++) { $studentID = $worksheet->getCellByColumnAndRow(1, $row)->getValue(); $name = $worksheet->getCellByColumnAndRow(2, $row)->getValue(); $grade = $worksheet->getCellByColumnAndRow(5, $row)->getValue(); // 过滤无效记录 if(empty($studentID) || empty($name)){ continue; } // 检查当前学生是否已在数据库中 $isExists = $this->loading_model->checkStudentExists($studentID); $record = [ 'studentID' => $studentID, 'name' => $name, 'grade' => $grade, 'subject' => $subject ]; if($isExists){ $updateData[] = $record; } else { $insertData[] = $record; } } } // 执行批量更新 if(!empty($updateData)){ $this->loading_model->update($updateData); echo count($updateData) . '条数据已更新'; } // 执行批量插入(若不需要新增可注释此段) if(!empty($insertData)){ $this->loading_model->insert($insertData); echo '<br>' . count($insertData) . '条新数据已插入'; } echo '<br>数据处理完成'; } }
修改后的模型代码
function insert($data) { $this->db->insert_batch('tbl_college_grades', $data); } function update($data) { // 指定studentID作为匹配字段,确保批量更新时能找到对应记录 $this->db->update_batch('tbl_college_grades', $data, 'studentID'); } // 新增方法:检查学生是否存在 function checkStudentExists($studentID) { $this->db->where('studentID', $studentID); $query = $this->db->get('tbl_college_grades'); return $query->num_rows() > 0; }
关键改动说明
- 唯一匹配字段:
update_batch必须指定第三个参数(匹配字段studentID),否则无法正确匹配数据库记录进行更新。 - 性能优化:将
subject的读取移到循环外,避免重复读取同一个单元格。 - 数据过滤:跳过空的
studentID或name记录,防止无效数据进入数据库。 - 结果反馈:增加更新/插入数量的提示,方便验证操作结果。
内容的提问来源于stack exchange,提问作者Coder Codes
相关产品推荐
相关产品推荐

