PHP Export Excel

Source: Internet
Author: User

In the development of a mall website, the customer needs a export report function, I use phpexcel to achieve this function.

Download Phpexcel_1.8.0_doc.zip, the extracted folder classes upload to the root directory of the site, the contents of the classes directory are as follows:

Export File excel.php

require_once dirname (__file__). ' /classes/phpexcel.php ';//introduced phpexcel ..... The process of getting data from a database is omitted here, $a as an array to export ... $numArr = array (' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' I ', ' J ', ' K ', ' L ', ' M ', ' N ', ' O ', ' P ', ' Q ', ' R ', ' S ', ' T ', ' U ', ' V ', ' W ', ' X ', ' Y ', ' Z ', ' AA ', ' AB ', ' AC ', ' AD ', ' AE ', ' AF ', ' AG ', ' AH ', ' AI ', ' AJ ', ' AK ', ' AL ', ' AM ', ' an ', ' AO ', ' AP ', ' AQ ', ' AR ', ' as ', ' at ', ' AU ', ' AV ', ' AW ', ' AX ', ' AY ', ' AZ ');//Create new Phpexcel object$objphpexcel = new Phpexcel (); $arr = Array (' Order number ', ' Place Time ', ' City ', ' region ', ' Customer name ', ' consignee ', ' Contact phone ', ' delivery address ', ' ERP customer name ', ' Logistics system Customer name ', ' Activity item ', ' brand ', ' model ', ' color ', ' object Flow system model ', ' order quantity ', ' Unit price ', ' collect money ', ' red envelopes ', ' Price guarantee rebate ', ' freight ', ' paid amount ', ' payment method ', ' Order origin ', ' upstream vendor ', ' Whether in warehouse ', ' express face number ', ' Order status ', ' Confirm time ', ' last state confirmation ' Between ', ' description ', ' corresponding business ', ' corresponding customer service ', ' Business message ', ' Order summary ', ' salesman ', ' contact method '); 
Output header echo date (' H:i:s '), "Add some data", eol;//set newline $objphpexcel->getactivesheet ()->getstyle (' H ') Getalignment ()->setwraptext (true), $objPHPExcel->getactivesheet ()->getstyle (' Y ')->getalignment ()- >setwraptext (TRUE);//Set the width of the corresponding column $objphpexcel->setactivesheetindex (0)->getcolumndimension (' A ') SetWidth, $objPHPExcel->setactivesheetindex (0)->getcolumndimension (' B ')->setwidth (20); $ Objphpexcel->setactivesheetindex (0)->getcolumndimension (' G ')->setwidth, $objPHPExcel Setactivesheetindex (0)->getcolumndimension (' H ')->setwidth (+), $objPHPExcel->setactivesheetindex (0)- >getcolumndimension (' I ')->setwidth, $objPHPExcel->setactivesheetindex (0)->getcolumndimension (' J ')->setwidth (15);//Output First line $objphpexcel->setactivesheetindex (0)->setcellvalue (' A1 ', $arr [0])->se            Tcellvalue (' B1 ', $arr [1])->setcellvalue (' C1 ', $arr [2])->setcellvalue (' D1 ', $arr [3]) ->setcelLValue (' E1 ', $arr [4])->setcellvalue (' F1 ', $arr [5])->setcellvalue (' G1 ', $arr [6])-    >setcellvalue (' H1 ', $arr [7])->setcellvalue (' I1 ', $arr [8])->setcellvalue (' J1 ', $arr [9]) ->setcellvalue (' K1 ', $arr [ten])->setcellvalue (' L1 ', $arr [11]);//output content for ($i =0; $i <count ($a); $i + +) {$ Objphpexcel->setactivesheetindex (0)->setcellvalue ($numArr [0]. ( $i +2), $a [$i] [' ORDER_SN '])->setcellvalue ($NUMARR [1]. ( $i +2), $a [$i] [' add_time '])->setcellvalue ($NUMARR [2]. ( $i +2), $a [$i] [' City '])->setcellvalue ($NUMARR [3]. ( $i +2), $a [$i] [' region_name '])->setcellvalue ($NUMARR [4]. ( $i +2), $a [$i] [company])->setcellvalue ($NUMARR [5]. ( $i +2), $a [$i] [' consignee '])->setcellvalue ($NUMARR [6]. ( $i +2), $a [$i] [' Mobile '])->setcellvalue ($NUMARR [7]. ( $i +2), $a [$i] [' address '])->setcellvalue ($NUMARR [8]. ( $i +2), ")->setcellvalue ($NUMARR [9]. ( $i +2), ")->setcellvalue ($NUMARR [10]. ( $i +2), ")->setcellvalue ($NUMARR [11]. ( $i +2), $a [$i[' Brand_Name ']);} Rename Worksheetecho Date (' h:i:s '), "Rename Worksheet", EOL; $dirName = Date ("YMD");//directory name $filename = Date ("Ymdhis"); /filename $objphpexcel->getactivesheet ()->settitle ($fileName);//Set Active sheet Index to the first sheet, so Excel open S this as the first sheet$objphpexcel->setactivesheetindex (0);//Save Excel Fileecho date (' h:i:s '), "Write to Ex cel2007 format ", EOL; $callStartTime = Microtime (True); if (!opendir ('. /excel/'. $dirName)} {mkdir ('.. /excel/'. $dirName);} $objWriter = Phpexcel_iofactory::createwriter ($objPHPExcel, ' Excel2007 '); $objWriter->save ('.. /excel/'. $dirName. ' /'. $fileName. '. Xlsx '); $end = GetCurrentTime (); $spend = $end-$begin; if ($spend >) {echo ' <script>if (Confirm ("Execution timed out! ")) {Window.history.back (-1);} </script> '; exit;} Header (' location:http://'. $_server[' Http_host '). ' /excel/'. $dirName. ' /'. $fileName. '. Xlsx ');


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.