|
下載phpExcel_1.8.0_doc.zip http://phpexcel.codeplex.com/,將解壓后的文件夾里的Classes上傳到網(wǎng)站的根目錄下,Classes目錄內(nèi)的內(nèi)容如下:
導(dǎo)出文件Excel.php
require_once dirname(__FILE__).'/Classes/phpExcel.php';//引入phpExcel.....此處略去從數(shù)據(jù)庫獲取數(shù)據(jù)的過程,$a為需要導(dǎo)出的數(shù)組......$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('訂單號(hào)','下單時(shí)間','城市','地區(qū)', '客戶名稱', '收貨人', '聯(lián)系電話', '收貨地址', 'ERP客戶名稱', '物流系統(tǒng)客戶名稱', '活動(dòng)項(xiàng)目', '品牌', '型號(hào)', '顏色', '物流系統(tǒng)型號(hào)', '訂貨量', '單價(jià)', '代收貨款', '紅包', '價(jià)保返利', '運(yùn)費(fèi)', '實(shí)收金額', '付款方式', '訂單來源', '上游廠商', '是否在倉', '快遞面單號(hào)', '訂單狀態(tài)', '確認(rèn)時(shí)間', '末次狀態(tài)確認(rèn)時(shí)間', '描述', '對(duì)應(yīng)業(yè)務(wù)', '對(duì)應(yīng)客服', '商家留言', '下單摘要', '業(yè)務(wù)員', '聯(lián)系方式');// 輸出標(biāo)題echo date('H:i:s') , " Add some data" , EOL;//設(shè)置換行$objphpExcel->getActiveSheet()->getStyle('H')->getAlignment()->setWrapText(true);$objphpExcel->getActiveSheet()->getStyle('Y')->getAlignment()->setWrapText(true);//設(shè)置相應(yīng)列的寬度$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);//輸出第一行$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]);//輸出內(nèi)容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");//目錄名$fileName = date("YmdHis");//文件名$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.'/'.$fileName.'.xlsx');$end = getCurrentTime();$spend = $end-$begin;if ($spend > 30){ echo '<script>if(confirm("執(zhí)行超時(shí)!")){ window.history.back(-1);}</script>'; exit;}header('Location:http://'.$_SERVER['HTTP_HOST'].'/excel/'.$dirName.'/'.$fileName.'.xlsx');
php技術(shù):PHP 導(dǎo)出Excel示例分享,轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。