PHP Export Excel

Source: Internet
Author: User

Download phpexcelFirst, Baidu search into the official website download

Import require (dirname (__file__). '/phpexcel/classes/phpexcel.php ');

Code:

Creating objects
$excel =New Phpexcel ();
Excel table format, 8 columns
$letter =Array' A ',' B ',' C ',' D ',' E ',' F ',' G ',' H ',' I ',' J ');
Table Header Array
$tableheader =Array' Order number ',' Order Time ',' User name ',' Consignee ',' Contact phone ',' Detailed address ',' Amount payable ',' Payment status ',' Delivery status ',' Order goods ');
Populating header information
For$i =0;$i <Count$tableheader);$i + +) {
$excel->getactivesheet ()->setcellvalue ("$letter [$I]1 ","$tableheader [$I]");
}
Table width
$excel->getactivesheet ()->getcolumndimension (' A ')->setwidth (30);
$excel->getactivesheet ()->getcolumndimension (' B ')->setwidth (30);
$excel->getactivesheet ()->getcolumndimension (' C ')->setwidth (30);
$excel->getactivesheet ()->getcolumndimension (' D ')->setwidth (30);
$excel->getactivesheet ()->getcolumndimension (' E ')->setwidth (30);
$excel->getactivesheet ()->getcolumndimension (' F ')->setwidth (30);
$excel->getactivesheet ()->getcolumndimension (' G ')->setwidth (30);
$excel->getactivesheet ()->getcolumndimension (' H ')->setwidth (30);
$excel->getactivesheet ()->getcolumndimension (' I ')->setwidth (30);
$excel->getactivesheet ()->getcolumndimension (' J ')->setwidth (50);
Enquiry value
$start _time=Strtotime ($_request[' Start_time ']);
$end _time=Strtotime ($_request[' End_time ']);
$pay _status=$_request[' Pay_status '];
$shipping _status=$_request[' Shipping_status '];
$sql ="Select O.order_id,o.order_sn,o.add_time,o.user_id,o.consignee,o.tel,o.address,o.order_amount,o.order_status, O.pay_status,o.shipping_status ".
"From".$ecs->table (' Order_info ')."As O".
"WHERE o.add_time > ' {$start _time}' and O.add_time < ' {$end _time}' And pay_status={$pay _status}and shipping_status={$shipping _status}";
$res =$db->getall ($SQL);
$user _list =$res;
Populating table information
$hang =2;
foreach ($user _listAs$key =$value) {
$goods =$db->getall ("Select Goods_name,goods_number from".$ecs->table (' Order_goods ')."Where order_id=".$value [' order_id ']);
$goodsname =" ";
foreach$goodsAs$k 1=>$v 1) {
$goodsname. ="\ n".$v 1[' Goods_name ']."X".$v 1[' Goods_number ']." ";
}
$username =$db->getone ("Select User_name from".$ecs->table (' Users ')."Where user_id=".$value [' user_id ']);
$excel->getactivesheet ()->setcellvalue (' A '. ($hang),$value [' Order_sn ']." ");Add a space to prevent the timestamp from being converted
$excel->getactivesheet ()->setcellvalue (' B '. ($hang),Date' Y-m-d h:i:s ',$value [' Add_time '])." ");
$excel->getactivesheet ()->setcellvalue (' C '. ($hang),$username." ");
$excel->getactivesheet ()->setcellvalue (' D '. ($hang),$value [' Consignee ']." ");
$excel->getactivesheet ()->setcellvalue (' E '. ($hang),$value [' Tel ']." ");
$excel->getactivesheet ()->setcellvalue (' F '. ($hang),$value [' Address '." ");
$excel->getactivesheet ()->setcellvalue (' G '. ($hang),$value [' Order_amount ']." ");
$excel->getactivesheet ()->setcellvalue (' H '. ($hang), $_lang[' ps ' [$value [' Pay_status ']]. "   "); $excel->getactivesheet ()->setcellvalue (' I '). ($hang), $_lang[' SS ' [$value [' Shipping_status ']]. " ");
$excel->getactivesheet ()->setcellvalue (' J '). ($hang), $goodsname);
$excel->getactivesheet ()->getstyle (' A2:t '. $hang)->getalignment ()->setwraptext (true); Automatic line height adjustment
$hang = $hang + 1;
}

Create an Excel input object
$write = new Phpexcel_writer_excel5 ($excel);
Header ("Pragma:public");
Header ("expires:0");
Header ("Cache-control:must-revalidate, Post-check=0, pre-check=0");
Header ("Content-type:application/force-download");
Header ("Content-type:application/vnd.ms-execl");
Header ("Content-type:application/octet-stream");
Header ("Content-type:application/download");
Header (' Content-disposition:attachment;filename= order list '. Date (' y-m-d ', Time ()) '. XLS ');
Header ("Content-transfer-encoding:binary");
$write->save (' php://output ');
Exit

PHP Export Excel

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.