问题
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