PHP export generates Excel file

Source: Internet
Author: User

PHP Export build Excel first need to download install Office extensions

You can download it yourself, or you can download and install it via composer

Personal comparison suggests the latter

Code is simple no nonsense directly on the code


/**
* @param $file _name String file name
* @param $title String title
* @param $order a comma-concatenated form of a String in Chinese
* @param $data Array Data set
* @param $data _order String data collection key is concatenated as a comma
*/
function Export ($file _name, $title, $order, $data, $data _order) {

$objPHPExcel = new Phpexcel ();
$letter = Array ();
for ($j = n, $j <; $j + +) {
$letter [] = Chr ($j);
}
/**
* Setcreator Author
* Settitle Title
* Setsubject Theme
* SetDescription Notes
* Setkeywords Keywords
* Setcategory Category
*/
$objPHPExcel->getproperties ()->setcreator ("alading")
->setlastmodifiedby ("alading")
->settitle ("Office" XLSX)
->setsubject ("Office" XLSX)
->setdescription ("Office" XLSX)
->setkeywords ("Office 2007")
->setcategory ("alading file");

$order _arr = Explode (', ', $order);

foreach ($order _arr as $key = + $value) {
Set width
$objPHPExcel->getactivesheet ()->getcolumndimension ($letter [$key])->setwidth (20);
Set Horizontal Center
$objPHPExcel->getactivesheet ()->getstyle (' A1 ')->getalignment ()->sethorizontal (phpexcel_style_ Alignment::horizontal_center);
$objPHPExcel->getactivesheet ()->getstyle ($letter [$key])->getalignment ()->sethorizontal (phpexcel_ Style_alignment::horizontal_center);
}

Set row height
$objPHPExcel->getactivesheet ()->getrowdimension (' 1 ')->setrowheight (22);
$objPHPExcel->getactivesheet ()->getrowdimension (' 2 ')->setrowheight (20);
$data _order = Explode (', ', $data _order);
Set the final style
End ($data _order);
$end _key = ': '. $letter [Key ($data _order)];

Fonts and Styles
$objPHPExcel->getactivesheet ()->getdefaultstyle ()->getfont ()->setsize (10);
$objPHPExcel->getactivesheet ()->getstyle (' A2 '. $end _key. ' 2 ')->getfont ()->setbold (true);
$objPHPExcel->getactivesheet ()->getstyle (' A1 ')->getfont ()->setbold (true);
$objPHPExcel->getactivesheet ()->getstyle (' A2 '. $end _key. ' 2 ')->getalignment ()->sethorizontal (Phpexcel_style_alignment::horizontal_center);
$objPHPExcel->getactivesheet ()->getstyle (' A2 '. $end _key. ' 2 ')->getborders ()->getallborders ()->setborderstyle (Phpexcel_style_border::border_thin);

Merge
$objPHPExcel->getactivesheet ()->mergecells (' A1 '. $end _key. ' 1 ');

Table header
$objPHPExcel->setactivesheetindex (0)->setcellvalue (' A1 ', $title);
foreach ($order _arr as $k = + $v) {
$objPHPExcel->setactivesheetindex (0)
->setcellvalue ($letter [$k]. ' 2 ', $v);
}

for ($i = 0, $len = count ($data), $i < $len; $i + +) {
foreach ($data _order as $da + = $vo) {
$objPHPExcel- >getactivesheet (0)->setcellvalue ($letter [$da]. ($i + 3), $data [$i] [$vo]);
}
$objPHPExcel->getactivesheet ()->getstyle (' A '. ($i + 3). $end _key. ($i + 3)) ->getalignment ()->setvertical (Phpexcel_style_alignment::vertical_center);
$objPHPExcel->getactivesheet ()->getstyle (' A '. ($i + 3). $end _key. ($i + 3)) ->getborders ()->getallborders ()->setborderstyle (Phpexcel_style_border::border_thin);
$objPHPExcel->getactivesheet ()->getrowdimension ($i + 3)->setrowheight (16);
}
//rename table name
$objPHPExcel->getactivesheet ()->settitle ($title);
//Table index set to first table
$objPHPExcel- >setactivesheetindex (0);
//Output
Header (' Content-type:application/vnd.ms-excel ');
header (' content-disposition:attachment; Filename= "'. $file _name. '. xls ');
Header (' cache-control:max-age=0 ');

$objWriter = Phpexcel_iofactory::createwriter ($objPHPExcel, ' Excel5 ');

$objWriter->save (' php://output ');
}

PHP export generates Excel file

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.