TP5 Encapsulation Phpexecl introduces and invokes the encapsulation of classes and calls

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.