我正在使用CodeIgniter和PHPExcel来创建一个系统。这里我在ExcelModel中有循环和返回函数来循环我想要的列和行:
$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;
我的观点是收到数据,一切都很好。但是,我想用数据构建一个HTML表,但问题是:那个数组没有我写的密钥,所以在我的表中输出变量里面的变量时,它就是同样的东西。看:
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>';
}
我知道如果我有这样的事情:
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>';
}
它有效。但是,由于我的数组是空的并且在ExcelModel中的循环之后填充,我该怎么做呢?
答案 0 :(得分:0)
感谢@elddenmedio
它与CI的表库一起使用。这是最终的代码:
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);
}