首页 > 解决方案 > 除当前单元格值外,导入 phpexcel codeigniter

问题描述

我想使用我的codeigniter中的php excel从excel导入,当前单元格包含当前文本

单元格1 单元格2
一个 1
2
C 3

我只想在cell1中除了B之外的单元格插入数据库

我的控制器

public function import_excel(){
    if (isset($_FILES["fileExcel"]["name"])) {
        $path = $_FILES["fileExcel"]["tmp_name"];
        $object = IOFactory::load($path);
        foreach($object->getWorksheetIterator() as $worksheet)
        {
            $highestRow = $worksheet->getHighestRow();
            $highestColumn = $worksheet->getHighestColumn();    
            for($row=2; $row<=$highestRow; $row++)
            {
                $cell1= $worksheet->getCellByColumnAndRow(0, $row)->getValue();
                $cell2= $worksheet->getCellByColumnAndRow(1, $row)->getValue();
                
                $temp_data[] = array(
                    'cell1'  => $cell1,
                    'cell2'   => $cell2,
                    
                );
                

            }
        }
        
        if($cell1 == 'B'){

        }else{
            $this->load->model('M_Admin');
            $insert = $this->M_Admin->insert($temp_data);
            
        }

但这失败了,数据仍然插入。请帮助我,非常感谢你的帮助。

标签: codeigniterphpexcel

解决方案


我以前从未使用过 phpexcel,但如果它是数组并认为 $cell1 是字符串/文本,我想你可以尝试使用它:

        for($row=2; $row<=$highestRow; $row++)
        {
            $cell1= $worksheet->getCellByColumnAndRow(0, $row)->getValue();
            $cell2= $worksheet->getCellByColumnAndRow(1, $row)->getValue();

            //cell value you want to hide
            if ($cell1 == 'B') {
                $cell1 = 'xxx'; //overwrite that value into something
            }

            $temp_data[] = array(
                'cell1'  => $cell1,
                'cell2'   => $cell2,
                
            );
            

        }

注意:我正在使用 codeigniter 3


推荐阅读