PHPExcel打印数组结果作为HTML表格在视图中

时间:2016-06-08 14:47:38

标签: php codeigniter

我正在使用PHPExcel和CodeIgniter来创建应用程序。我有我的Model for Excel处理:

class modelExcel extends CI_Model
{
  protected $excel;

  function __construct() {
    parent::__construct();
    $this->load->library('Excel');
  }

  function readReport() { //Função para retorno dos dados do arquivo Excel
    $this->excel = PHPExcel_IOFactory::load(APPPATH."/exceldata/wf.xlsx");

    //Seleciona a planilha ativa
    $objWorksheet = $this->excel->getActiveSheet()->rangeToArray('D7:J7');

    return $objWorksheet;
  }
}

我的主页控制器,我获取所有数据并发送到视图:

public function index() {
        if($this->session->userdata('logged')) {
            $this->load->model('ModelExcel');
            $data['excel'] = $this->ModelExcel->readReport();
            $sessionData = $this->session->userdata('logged');
            $data['username'] = $sessionData['userName'];
            $this->load->view('template/header', $data);
            $this->load->view('home');
            $this->load->view('template/footer');   
        }

最后我的View(有更多代码,但这里正是我要打印表格的地方):

<div class="row"> 
   <div class="col-md-6">
        <?=$excel?>
    </div>
</div>

完全按照这样做,我得到了数组到字符串转换的错误。有另一种方法来打印excel特定的列和行?

2 个答案:

答案 0 :(得分:1)

$objWorksheet将是一个二维数组,行和列,包含1行和7列,因此您需要迭代该行以获取列数组,然后迭代这些列获取要在视图中显示的单个单元格值

答案 1 :(得分:0)

用&#34; foreach&#34;解决。这是最终的代码:

foreach ($excelData as $dataArray) {
            foreach ($dataArray as $dataValue) {
                echo $dataValue;
            }
        }