PHP Excel export example sharing

Source: Internet
Author: User
Tags eol php excel zip extension
When developing a Mall website, the customer needs a report export function. I use PHPExcel to implement this function. Download phpexcel_1.8.0_doc.zip extension:

Export file Excel. php

Require_once dirname (_ FILE __). '/Classes/PHPExcel. php '; // introduce PHPExcel ..... the process of retrieving data from the database is omitted here. $ a is the array to be exported ...... $ 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', 'A',' AB ', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'Ai', 'AJ ', 'AK', 'Al ', 'AM', 'Any', 'ao', 'AP ', 'Aq', 'ar ', 'as', 'at', 'Au', 'Av ', 'aw', 'ax', 'ay', 'az'); // Create new PHPExcel object $ objPHPExcel = new PHPExcel (); $ arr = array ('Order No ', 'order time ', 'city', 'region', 'customer name', 'consignee', 'Contact number', 'shipping address', 'erp customer name ', 'logistics system customer name', 'activity project', 'brand', 'model', 'Color', 'logistics system model', 'Order quantity ', 'unit price ', 'pay-as-you-go users', 'hongba', 'Price rebates ', 'Shipping', 'amount received', 'payment method ', 'Order source', 'Upstream Manufacturers ', 'In warehouse? ', 'waybill no.', 'Order status', 'confirmation time', 'Last status confirmation time', 'Description', 'corresponding service ', 'customer service', 'Merchant message', 'Order summary', 'Salesman', 'Contact information'); // output the title echo date ('H: I: s '), "Add some data", EOL; // set the line feed $ objPHPExcel-> getActiveSheet ()-> getStyle ('h')-> getAlignment () -> setWrapText (true); $ objPHPExcel-> getActiveSheet ()-> getStyle ('Y')-> getAlignment ()-> setWrapText (true ); // Set the column width $ objPHPExcel-> setActiveSheetIndex (0)-> getColumnDimension ('A')-> setWidth (15); $ objPHPExcel-> setActiveSheetIndex (0) -> getColumnDimension ('B')-> setWidth (20); $ objPHPExcel-> setActiveSheetIndex (0)-> getColumnDimension ('g')-> setWidth (15 ); $ objPHPExcel-> setActiveSheetIndex (0)-> getColumnDimension ('h')-> setWidth (40); $ objPHPExcel-> setActiveSheetIndex (0)-> getColumnDimension ('I ') -> setWidth (15); $ objPHPExcel-> setActiveSheetIndex (0)-> getColumnDimension ('J')-> setWidth (15 ); // output the first line $ objPHPExcel-> setActiveSheetIndex (0)-> setCellValue ('A1', $ arr [0])-> setCellValue ('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 [10])-> setCellValue ('l1 ', $ arr [11]); // Output Content for ($ I = 0; $ I
 
  
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'])-> s EtCellValue ($ 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 "); // file name $ objPHPExcel-> getActiveSheet ()-> setTitle ($ fileName); // Set active sheet index to the first sheet, so Excel opens this as the first sheet $ objPHPExcel-> setActiveSheetIndex (0); // Save Excel 2007 fileecho date ('H: i: s'), "Write to Excel2007 format", EOL; $ callStartTime = microtime (true); if (! Opendir ('.. /excel /'. $ dirName) {mkdir ('.. /excel /'. $ dirName) ;}$ objWriter = PHPExcel_IOFactory: createWriter ($ objPHPExcel, 'excel2007 '); $ objWriter-> save ('.. /excel /'. $ dirName. '/'.w.filename.'.xlsx'); $ end = getCurrentTime (); $ spend = $ end-$ begin; if ($ spend> 30) {echo 'script if (confirm ("execution timeout! ") {Window. history. back (-1);} script '; exit;} header ('Location: http ://'. $ _ SERVER ['http _ host']. '/excel /'. $ dirName. '/'.w.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.