Rodrigo BRF
Rodrigo BRF

Reputation: 133

PHPExcel print array result as HTML table in view

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?

Upvotes: 0

Views: 839

Answers (2)

Rodrigo BRF
Rodrigo BRF

Reputation: 133

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

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

Upvotes: 0

Mark Baker
Mark Baker

Reputation: 212522

$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

Upvotes: 1

Related Questions