Laravel Excel Structure (Importing File)

1

I am using the Laravel Excel library and I have been reading the documentation but it does not say anything to read the excel from a certain row number, that is, I have the first 6 lines of the excel with information and the "title" of each column is not until row 7, as I indicated to the function that the "indexes" are there?

public function excel($id){
  $bankFiles = FicheroBanco::find($id);
  $url = $bankFiles->url;

    Excel::selectSheetsByIndex(0)->load($url, function ($sheet){
        //$sheet->noHeading();
        $sheet->each(function ($row){
            $mov = new Transaccion();
            $mov->cuenta = $row->cuenta;
            $mov->divisa = $row->divisa;
            $mov->titular = $row->titular;
            $mov->fecha = $row->fecha;
            $mov->concepto = $row->concepto;
            $mov->valor = $row->valor;
            $mov->importe = $row->importe;
            $mov->saldo = $row->saldo;
            $mov->observaciones = $row->observaciones;
            $mov->extra = $row->extra;
            $mov->save();
        });
    });
return view('bank.excel');
}

I've seen that I can use the - > take (); to catch the first x lines of a document or the - > skip (); to skip the first x lines. The problem is that the first line of the excel file is a merged cell of 7 columns I can not use the titles of line 6, since it mounts the unstructured array.

EDIT: By default it can be configured from within the excel.php configuration file in the 'startRow' = > 1 but the thing is that I can not take any kind of information from the fields before that line either. The thing is that I need the info of some fields of the first 6 lines, but I need to indicate that the headings start on line 6. Any ideas?

I have read in the documentation that you can use the - > byConfig (); parameter but as indicated only if you do not use headings.

Would someone know how to do it?

I need to insert into the database the fields before the "headings", for each row of subsequent data.

EDITO: I've been doing tests and although I did two different functions one without using headings and the other one, having changed the startRow parameter of the configuration, both one and another load me from the same row. I do not know how to indicate one to load one row and the other to load from the beginning. Can you?

The above code works perfectly for me if I put 'startRow' = > 6 , on the other hand if I use noheadings (with startrow 6) I can not access previous rows of the excel file.

I can not modify the structure of the excel since I have to stick to it.

I leave the documentation - > link

    
asked by RuralGalaxy 17.05.2016 в 10:48
source

1 answer

0

In the end the solution has been, leave the configuration excel.php as it was, that is, 'startRow' = > 1 to be able to access all the tuples from the beginning and I have done the following:

public function excel($id)
    {
        $bankFiles = FicheroBanco::find($id);

        $url = $bankFiles->url;
        $code = $bankFiles->id_banco;

        Excel::selectSheetsByIndex(0)->load($url, function ($sheet) use($code){
            $sheet->noHeading();

            $results = $sheet->toArray();

            $account = $results[1][1];
            $currency = $results[2][1];
            $owner = $results[3][1];
            $range = $results[4][1];

            $sheet->skip(6);

            $sheet->each(function ($row) use($code, $account, $currency, $owner, $range){

                $fechaOperacion = str_replace('/','-', $row[0]);
                $fechaOperacion = Carbon::parse($fechaOperacion)->format('Y-m-d');

                $fechaValor = str_replace('/','-', $row[2]);
                $fechaValor = Carbon::parse($fechaValor)->format('Y-m-d');

                $mov = new Transaction();
                $mov->id_banco = $code;
                $mov->cuenta = $account;
                $mov->divisa = $currency;
                $mov->titular = $owner;
                $mov->rango = $range;
                $mov->fechaOperacion = $fechaOperacion;
                $mov->concepto = $row[1];                    
                $mov->fechaValor = $fechaValor;
                $mov->importe = $row[3];
                $mov->saldo = $row[4];

                $mov->save();
            });
        });

    }

This way I have been able to get what I wanted. The problem was that I did not know how to navigate $ sheet but afterwards in the documentation I saw that there is - > toArray () and - > toObject () and that way you return the object as you play without strange positions and for each of your whole life or whatever it takes.

I hope it's useful for someone.

    
answered by 20.05.2016 / 16:01
source