0

I'm using CodeIgniter and PHPExcel to make a system. Here I have the loop and return function in ExcelModel to loop in columns and rows I want:

$lastColumn = $objWorksheet->getHighestColumn();
    $lastColumn++;
    $cell = array();
    for ($row = 2; $row <= 7; $row++) {
        for($column = 'A'; $column != $lastColumn; $column++) {
            $cell[] = $objWorksheet->getCell($column.$row)->getValue();
        }
    }

    return $cell;

My view is receiving the data, everything is fine. But, I want to build a HTML table with the data, but the problem is: that array didn't have a key writed by me, so in my table when I output the variable inside tags, it comes the same thing. Look:

foreach ($excelData as $dataArray) {
            echo '<tr>';
            echo '<td>' . $dataArray . '</td>';
            echo '<td>' . $dataArray . '</td>';
            echo '<td>' . $dataArray . '</td>';
            echo '<td>' . $dataArray . '</td>';
            echo '<td>' . $dataArray . '</td>';
            echo '</tr>';
        }

I know that if I have something like this:

foreach ($excelData as $dataArray) {
            echo '<tr>';
            echo '<td>' . $dataArray['example1'] . '</td>';
            echo '<td>' . $dataArray['example2'] . '</td>';
            echo '<td>' . $dataArray['example3'] . '</td>';
            echo '<td>' . $dataArray['example4'] . '</td>';
            echo '<td>' . $dataArray['example5'] . '</td>';
            echo '</tr>';
        }

It'd work. But, as my array is empty and is filled after the loop in ExcelModel, how can I make this?

Rodrigo BRF
  • 133
  • 10

1 Answers1

0

Thanks to @elddenmedio

It worked with CI's table library. Here's the final code:

function readReport() { //Função para retorno dos dados do arquivo Excel
    $this->excel = PHPExcel_IOFactory::load(APPPATH."/exceldata/export.xlsx");
    $this->table->set_heading(
            'Nº Operação',
            'Etapa Atual do WF',
            'Usuário Resp. pela Tarefa',
            'Denominação',
            'Data Envio'
        );

    //Seleciona a planilha ativa
    $objWorksheet = $this->excel->getActiveSheet();

    //Efetua loop de colunas e linhas para buscar os dados
    $lastColumn = $objWorksheet->getHighestColumn();
    $lastColumn++;
    $cell = array();

    //Efetua loop de colunas e linhas para buscar os dados
    for ($row = 2; $row <= 7; $row++) {
        for($column = 'A'; $column != $lastColumn; $column++) {
            $cell[] = $objWorksheet->getCell($column.$row)->getValue();
        }
    }

    //Gera dados da tabela com base no array recebido do loop
    $tableList = $this->table->make_columns($cell, 5);

    //Altera o layout da tabela à ser gerada
    $tmpl = array ( 'table_open'  => '<table class="table">' );
    $this->table->set_template($tmpl);

    //Retorna/gera a tabela na View
    return $this->table->generate($tableList);
  }
Rodrigo BRF
  • 133
  • 10