PHPExcel print array result as HTML table in view

柔情痞子 提交于 2021-02-11 14:00:33

问题


I'm using PHPExcel and CodeIgniter to make an application. I have my Model for Excel treatment:

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;
  }
}

My Main Page Controller where I get all the data and send to a view:

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');   
        }

And finally my View (have more code, but here's exactly where I want to print the table):

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

Doing exactly as this are, I got an error of array to string conversion. Have some another way to print excel specific columns and rows?


回答1:


$objWorksheet will be a 2-dimensional array, rows and columns, with 1 row and 7 columns, so you'll need to iterate over that row to get the array of columns, and then iterate over those columns to get the individual cell values for display in your view




回答2:


Solved with "foreach". Here's the final code:

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


来源:https://stackoverflow.com/questions/37705860/phpexcel-print-array-result-as-html-table-in-view

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!