read from the first page of an excel

0

I'm doing a project that takes the data of an excel with several tabs (tab per year) and I implemented it in the table, but it turns out that when I send it read from the excel just read me the Last page, I do not know how to make you read all of them.

This is the complete PHP code:

<?php

class Conversion {

    private $_objPHPExcel;
    private $_sheet;
    private $_rows;
    private $_accountingCodes;
    private $_accountingEntries;

    public function __construct($file_type, $local_file){

        $objReader = PHPExcel_IOFactory::createReader($file_type);
        //lista de folios del archivo excel
        $worksheetList = $objReader->listWorksheetNames($local_file);
        $lastSheet = count($worksheetList) - 1;
        $this->_sheet = $worksheetList[$lastSheet];

        $objReader->setLoadSheetsOnly($this->_sheet);
        $objReader->setReadDataOnly(true);
        $this->_objPHPExcel = $objReader->load($local_file);

    }

    public function filterAccountingCodes() {

        $highestRow = $this->_objPHPExcel->getActiveSheet()->getHighestRow();

        $codes = $this->_objPHPExcel->getActiveSheet()->rangeToArray('A1:' . 'A' . $highestRow, 0, FALSE, TRUE, TRUE);

        foreach ($codes as $row => $column) {

            if (substr($column['A'], 0, 10) != '          ') {
                unset($codes[$row]);
            }else{
                $column_filtered[] = explode('    ' , substr($column['A'],10));
                $this->_accountingCodes = $column_filtered;
            }
        }

        $this->_rows = array_keys($codes);

    }


    public function filterAccountingEntries(){

        /*Construye array de entradas contables a partir de un rango de celdas*/

        foreach ( $this->_rows as $rows => $row ) {
            $accounts[] = $this->_objPHPExcel->getActiveSheet()->rangeToArray('C' . $row . ':N' .$row, 0, FALSE, TRUE, FALSE);
        }

        foreach($accounts as $key => $value) {
            $accounts_filtered[] = $value[0];
        }

        /*Filtrado del array de códigos eliminando concepto*/

        foreach ($this->_accountingCodes as $key => $value) {
            $codes_filtered[] = $value[0]; 
        }

        /*Unificación de ambos arrays incluyendo fecha de cada entrada contable*/

        $arrayLenght = count($codes_filtered);

        for ($i = 0 ; $i < $arrayLenght ; $i++){

            for ($j = 0 ; $j < 12 ; $j++){

                $year = '20'.substr($this->_sheet, -2);
                $monthNum = $j + 1;
                $dateObj   = DateTime::createFromFormat('!m', $monthNum);
                $month = $dateObj->format('m'); 

                $account[] = $codes_filtered[$i] .  ' ' . $year .'-' . $month . '-01' . ' ' . $accounts_filtered[$i][$j];
            }
        }


        foreach ($account as $row) {

            $accounting_entries[] = explode(' ' , $row);

        }

        $this->_accountingEntries = $accounting_entries;

    }

    public function accountingCodesCSV($file){

        $csv = fopen($file, 'w');

        foreach ($this->_accountingCodes as $fields) {
            fputcsv($csv, $fields, $delimiter = "\t", $enclosure = '"');
        }

        fclose($csv);
    }

    public function accountingEntriesCSV($file){

        $csv = fopen($file, 'w');

        foreach ($this->_accountingEntries as $fields) {
            fputcsv($csv, $fields, $delimiter = "\t", $enclosure = '"');
        }

        fclose($csv);
    }  

}

?>
    
asked by racxo 21.11.2017 в 18:40
source

1 answer

1
  

NOTE : This is just a pseudo-code that does not guarantee operation   of the program .

As I mentioned in the comment, in your code you have this line that tells the method that reads the excel only the last sheet:

$this->_sheet = $worksheetList[$lastSheet];

My suggestion is that you change the code so that you can cycle through the excel sheets:

public function __construct($file_type, $local_file){

        $objReader = PHPExcel_IOFactory::createReader($file_type);

        // lista de folios del archivo excel.
        $worksheetList = $objReader->listWorksheetNames($local_file);

        // Aquí miras cuántas hojas tiene tu archivo Excel.
        $cantidadHojas = count($worksheetList);

        // Aquí deberías haces un ciclo para que (por cada ciclo)
        // llame al evento que procesa la hoja excel.
        for ($hojaExcel = 0; $hojaExcel <= $cantidadHojas; $hojaExcel++) {

            // Aquí le estoy pasando el valor de "$hojaExcel".
            $this->_sheet = $worksheetList[$hojaExcel];    
            $objReader->setLoadSheetsOnly($this->_sheet);
            $objReader->setReadDataOnly(true);
            $this->_objPHPExcel = $objReader->load($local_file);
        }

    }
    
answered by 21.11.2017 в 20:19