Browse an excel by columns PHPEXCEL

32 views Asked by At

I need to read an excel file and upload the information to my DB, the structure of the information is the next one.

enter image description here

The idea is to take A1, then B2 to B8, N amoung of times, i make the test with a txt file and almost works, but I can't make it work in PhpExcel file.

    while(! feof($file)) {
 $i=0;
    $data = array();
    while($i<8){
        $line = fgets($file);
        echo $line. "<br>";
        array_push($data, $line);
        //echo $data[$i];
        $i=$i+1;
    }
    $medida_data=array(            
        'id_solicitud'  => 133,
        'programa'  => 1,
        'orden' => $data[0],  
        'ges' => $data[1],                   
        'dimension' =>$data[2],                   
        'actividades' => $data[3],
        'ejecucion' =>$data[4],
        'fecha_comprometida' => date('Y-m-d',strtotime($data[5])),
        'fecha_creacion' => date('Y-m-d'),
        'plazo' => $data[6],                   
        'estado' => 1
    );     
    $this->propuestas_model->register_medida_plan($medida_data);
    echo "<br>";
    $i=0;

At the moment I have a simple formula to run the Excel file, that's goes from row to row, but I don't know how to jump between columns.

    $reader->open($inputFileName);
    $count = 1;
    $worksheet = $spreadsheet->getActiveSheet();
    //Numero de Hojas en el Archivo 
        foreach ($worksheet->getRowIterator() as $row) {
            if($row[0] != ''){
             if($count > 1) {
                    $data = array(
                        'fecha_edicion' => date("Y-m-d", strtotime($row[0])),
                        'orden' => $row[1][1],
                        'ges' => $row[2],
                        'dimension' => $row[3],
                        'ejecucion' => $row[4],
                        'actividades' => $row[5],
                        'medios_verificacion' => $row[6]
                       
                    );  
                    //$this->db->insert('medidas',$data);                       
                }  
                $count++;                           
            }
        }
1

There are 1 answers

0
hanif zekri On

If I get your point right, you can add 2 counter in your code

$row_ounter for counting rows

$group_counter for counting each 8 rows

For example, rows counter start with 1 for first Folio, so next Folio number should be 9, and next 17, and next 25, and...

And when you convert this idea to code:

$row_ounter = 0;
$group_counter = 0;

foreach ($worksheet->getRowIterator() as $row) {
    
    $row_ounter++;
    
    if ($group_counter == 0) {
        //It's Folio
        $group_counter++;
    
    } elseif ($row_ounter % ($group_counter * 8) == 1) {
        //It's Folio
        $group_counter++;
    
    } else {
        //It's NOT Folio
    }
}

also notice that:

PHPExcel - DEAD

The project has not be maintained for years and must not be used anymore. All users must migrate to its direct successor PhpSpreadsheet, or another alternative.