PHP export Excel Data Instance code

Source: Internet
Author: User

Many sites have exported data for Excel, the first to use JS export but online articles are almost the same and browser compatibility problems, in short, it is not very useful, that had to use PHP exported server-side processing does not exist browser-compatible issues.

The figure above is the data to be exported in the project, the first two lines are THEAD, followed by the tbody (the number of TR is variable inside). The first is to get the data sent to the PHP side processing.


The code is as follows Copy Code

Require_once '.. /classes/phpexcel.php ';
Require_once '.. /classes/phpexcel/iofactory.php ';
$objPHPExcel = new Phpexcel ();
$objPHPExcel->getproperties ()->setcreator ("Maarten Balliauw")->setlastmodifiedby ("Maarten Balliauw")- >settitle ("Office 2007 XLSX Test Document")->setsubject ("Office 2007 XLSX Test Document")->setdescription (" Document for Office 2007 XLSX, generated using PHP classes. ") ->setkeywords ("Office 2007 OPENXML PHP")->setcategory ("Test result file");

$objPHPExcel->setactivesheetindex (0);
$objRichText = new Phpexcel_richtext ();
$objRichText->createtext (");
$objPayable = $objRichText->createtextrun (' immediate monitoring data ');
$objPayable->getfont ()->setcolor (new Phpexcel_style_color (phpexcel_style_color::color_red));
$objPayable->getfont ()->setbold (true);
$objPayable->getfont ()->setsize (24);
$objPHPExcel->getactivesheet ()->getcell (' B1 ')->setvalue ($objRichText);
$objPHPExcel->getactivesheet ()->getstyle (' B1 ')->getfont ()->setbold (true); Bold
$objPHPExcel->getactivesheet ()->getstyle (' B1 ')->getfont ()->setsize (24); Font size
$objPHPExcel->getactivesheet ()->getstyle (' B1 ')->getfont ()->getcolor () ()->setargb (phpexcel_style_ color::color_red); Text color
$objPHPExcel->getactivesheet ()->getstyle (' A1:g1 ')->getfill ()->setfilltype (Phpexcel_style_fill:: Fill_solid);
$objPHPExcel->getactivesheet ()->getstyle (' A1:g1 ')->getfill ()->getstartcolor () (" 00ffffe3 ');/shading

Width
$objPHPExcel->getactivesheet ()->getcolumndimension (' A ')->setautosize (true);
$objPHPExcel->getactivesheet ()->getcolumndimension (' A ')->setwidth (12);
$objPHPExcel->getactivesheet ()->getcolumndimension (' B ')->setwidth (18);
$objPHPExcel->getactivesheet ()->getcolumndimension (' C ')->setwidth (12);
$objPHPExcel->getactivesheet ()->getcolumndimension (' D ')->setwidth (18);
$objPHPExcel->getactivesheet ()->getcolumndimension (' E ')->setwidth (12);
$objPHPExcel->getactivesheet ()->getcolumndimension (' F ')->setwidth (18);
$objPHPExcel->getactivesheet ()->getcolumndimension (' G ')->setwidth (20);

Row height
for ($i = 2; $i <= $i + +) {
$objPHPExcel->getactivesheet ()->getrowdimension ($i)->setrowheight (22);
}

$objPHPExcel->getactivesheet ()->setcellvalue (' A2 ', ' name ');
$objPHPExcel->getactivesheet ()->setcellvalue (' A3 ', ' PHP ');
$objPHPExcel->getactivesheet ()->setcellvalue (' A4 ', ' EXCEL ');
$objPHPExcel->getactivesheet ()->setcellvalue (' A5 ', ');
$objPHPExcel->getactivesheet ()->setcellvalue (' A6 ', ' XXX ');
$objPHPExcel->getactivesheet ()->setcellvalue (' A7 ', ' V ');
$objPHPExcel->getactivesheet ()->setcellvalue (' A8 ', ' d ');
$objPHPExcel->getactivesheet ()->setcellvalue (' A9 ', ' name ');
$objPHPExcel->getactivesheet ()->setcellvalue (' A10 ', ' description ');
for ($i = 2; $i <= $i + +) {
  $objPHPExcel->getactivesheet ()->getstyle (' A '. $i)->getfont ()-&G T;setbold (true);   //Bold
}

$objPHPExcel->getactivesheet ()->setcellvalue (' B2 ', ' [name] ');
$objPHPExcel->getactivesheet ()->setcellvalue (' B3 ', ' exporting Excel with pictures ');
$objPHPExcel->getactivesheet ()->setcellvalue (' B4 ', ' XXXXX ');

Add a picture to Excel
$objDrawing = new phpexcel_worksheet_drawing ();
$objDrawing->setname (' Photo ');
$objDrawing->setdescription (' Photo ');
$objDrawing->setpath ('.. /images/touxiang.jpg ');
$objDrawing->setheight (170);
$objDrawing->setwidth (120);
$objDrawing->setcoordinates (' G2 ');
$objDrawing->setworksheet ($objPHPExcel->getactivesheet ());

$objPHPExcel->getactivesheet ()->setcellvalue (' A17 ', ' other requirements ');
$objPHPExcel->getactivesheet ()->setcellvalue (' B17 ', ' [other requirements] ');

$objPHPExcel->getactivesheet ()->getstyle (' A17 ')->getfont ()->setbold (true); Bold

$objPHPExcel->setactivesheetindex (0);
$objPHPExcel->getactivesheet ()->settitle (' immediate monitoring data ');

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

$objWriter->save (' Phpexcel.xls ');

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.