When developing a mall website, the customer needs a report export function. I use PHPExcel to implement this function.
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 and upload the Classes in the decompressed folder to the root directory of the website. The content in the Classes directory is as follows:
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 :'. $ _ SERVER ['HTTP _ host']. '/excel /'. $ dirName. '/'.w.filename.'.xlsx ');