PHPExcel 1.8.0 and CakePHP 2.7.5 cannot list MySQL data.

Asked 2 years ago, Updated 2 years ago, 47 views

Download from http://phpexcel.codeplex.com/ --- > (1.8.0) As of November 23, 2015
(*As of May 2021, you can download it from https://github.com/PHPOffice/PHPExcel.)

■ Unzip the downloaded file and rename the Classes folder to phpexcel.
Then, place the phpexcel folder under app/Vendor/

■Preparation and placement of template files
Create a phpexcel folder under app/tmp/
Place empty [template.xlsx] in 2007 format.

■ Controller Settings
app/Controller/Users.php

public function excel(){
    $this->layout=';
    $data = $this->User->findById(15);
    $this->set('data',$data);
}

■View/Users/excel.ctp

<?php
App::import('Vendor','PHPExcel_Writer_IWriter', array('file'=>'phpexcel/PHPExcel/Writer/IWriter.php'));
App::import('Vendor','PHPExcel_Writer_Abstract', array('file'=>'phpexcel/PHPExcel/Writer/Abstract.php'));
App::import('Vendor','PHPExcel_Writer_Excel2007', array('file'=>'phpexcel/PHPExcel/Writer/Excel2007.php'));
App::import('Vendor','PHPExcel_Reader_Excel2007', array('file'=>'phpexcel/PHPExcel/Reader/Excel2007.php'));

$objReader=PHPExcel_IOFactory::createReader("Excel 2007");

$template=TMP.'phpexcel/';
$template_path=$template.'template.xlsx';
$PHPExcel=$objReader->load($template_path);

$PHPExcel->setActiveSheetIndex(0);
$sheet=$PHPExcel->getActiveSheet();

$sheet->fromArray($data, null, 'A1');

$filename = 'output.xlsx';
$filename =mb_convert_encoding($filename, 'sjis', 'utf-8');

header('Content-Disposition: attachment; filename="'.$filename.'";
$objWriter=new PHPExcel_Writer_Excel 2007 ($PHPExcel);
$objWriter->save('php://output');

This way, only one line of user record information with ID number 15 is
Output as an Excel file.

I would like to list all MySQL data, so I will use the controller.

$data=$this->User->find('all');

This results in an invalid extension file and error.

If <php debug($data);?> on the View side, the array is properly listed.

$sheet->fromArray($data,null,'A1'); how to write
I don't think so.

Could you please tell me who you know?
Thank you for your cooperation.

cakephp

2022-09-30 14:17

1 Answers

This blog:PHPExcel is convenient to use the fromArray method for writing.
Especially the foreach part.

foreach($data as$value){
    $sheet->setCellValueByColumnAndRow($col++,$row,$value);
}


2022-09-30 14:17

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.