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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?