Composer Installation Phpexecl
Composer require Phpoffice/phpexcel
<?php
namespace potting;
Use Phpexcel;
Use Phpexcel_iofactory;
Class Excel
{
static public function export ($HEADARR, $data) {
$path = ' execl/';
$fileName =time ();
if (!file_exists ($path)) {
mkdir ($path, 0777,true);
}
$objPHPExcel = new Phpexcel ();
$objPHPExcel->getproperties ();
$key = Ord ("A"); Set up the table header
foreach ($headArr as $v) {
$colum = Chr ($key);
$objPHPExcel->setactivesheetindex (0)->setcellvalue ($colum. ' 1 ', $v);
$objPHPExcel->setactivesheetindex (0)->setcellvalue ($colum. ' 1 ', $v);
$key + = 1;
}
$column = ' 2 ';
$objActSheet = $objPHPExcel->getactivesheet ();
foreach ($data as $key = + $rows) {//Line write
$span = Ord ("A");
foreach ($rows as $keyName = + $value) {//Column write
$objActSheet->setcellvalue (Chr ($span). $column, $value);
$span + +;
}
$column + +;
}
$fileName = $fileName. ". XLS ";
$objActSheet = Phpexcel_iofactory::createwriter ($objPHPExcel, ' Excel2007 ');
Save XLS
$objActSheet->save ($path. $fileName);
return path
return $path. $fileName;
}
}
<?php
namespace potting;
Use Phpexcel;
Use Phpexcel_iofactory;
Class Excel
{
static public function export ($fileName, $HEADARR, $data) {
$path = ' execl/';
$fileName =time ();
if (!file_exists ($path)) {
mkdir ($path, 0777,true);
// }
$objPHPExcel = new Phpexcel ();
$objPHPExcel->getproperties ();
$key = Ord ("A"); Set up the table header
foreach ($headArr as $v) {
$colum = Chr ($key);
$objPHPExcel->setactivesheetindex (0)->setcellvalue ($colum. ' 1 ', $v);
$objPHPExcel->setactivesheetindex (0)->setcellvalue ($colum. ' 1 ', $v);
$key + = 1;
}
$column = ' 2 ';
$objActSheet = $objPHPExcel->getactivesheet ();
foreach ($data as $key = + $rows) {//Line write
$span = Ord ("A");
foreach ($rows as $keyName = + $value) {//Column write
$objActSheet->setcellvalue (Chr ($span). $column, $value);
$span + +;
}
$column + +;
}
$fileName = $fileName. ". XLS ";
$fileName = Iconv ("Utf-8", "gb2312", $fileName); Renaming a table
$objPHPExcel->setactivesheetindex (0); Set the activity Order index to the first table, so Excel opens this is the first table
Header (' Content-type:application/vnd.ms-excel ');
Header ("Content-disposition:attachment;filename= ' $fileName '");
Header (' cache-control:max-age=0 ');
$objWriter = Phpexcel_iofactory::createwriter ($objPHPExcel, ' Excel5 ');
$objWriter->save (' php://output '); Files are downloaded through the browser
Exit ();
$fileName = $fileName. ". XLS ";
$objActSheet = Phpexcel_iofactory::createwriter ($objPHPExcel, ' Excel2007 ');
Save XLS
$objActSheet->save ($path. $fileName);
return path
return $path. $fileName;
}
}
Public Function Index ()
{
$fileName = ' Test ';
$header =[' table head a ', ' table Head b ', ' table header C ';
$data =[
[' hehe ', ' Heihei ', ' 2321312 '],
[' hehe ', ' Heihei ', ' 2321312 '],
[' haha ', ' haha ', ' 2321312 ']
];
$url =excel::export ($fileName, $header, $data);
Var_dump ($url);d ie ();
}
This article is from the "Scars" blog, make sure to keep this source http://9934547.blog.51cto.com/9924547/1979387
TP5 Encapsulation Phpexecl introduces and invokes the encapsulation of classes and calls