drfb52000 2016-06-08 20:26
浏览 45
已采纳

使用PHPExcel数组返回构建表(列和行)

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?

  • 写回答

1条回答 默认 最新

  • dongzhuange2625 2016-06-09 13:56
    关注

    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);
      }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器