導出方法一 使用table輸出導出
1 //導出代碼 2 public function export(){ 3 set_time_limit(0); 4 ini_set('memory_limit','1024M');//設置導出最大內存 5 $ranking = self::getResult();//獲得需要導出的數據 6 //輸出的表頭 7 $_pre = array( 8 "listing" => "ListingID", 9 "salesbefore" => "前十天銷量", 10 "salesafter" => "后十天銷量", 11 "saleschange" => "漲跌銷量", 12 "percentchange" => "漲跌幅度(%)", 13 ); 14 $date = date("YmdHis");//日期作為輸出文件后綴 15 $content = getXLSFromList($_pre,$ranking);//獲得輸出的表格內容 16 header("Content-type:application/vnd.ms-execl;charset=gb2312");//設置導出格式 17 header("Content-Disposition:attactment;filename=銷量變化排行榜".$date.".xls");//設置導出文件名 18 header("Pragma: no-cache"); 19 header("Expires: 0"); 20 echo $content; 21 exit; 22 }
1 //此方法建議寫入公共方法 通過數組遍歷得出導出報表類型結構 2 function getXLSFromList($pres,$lists){ 3 // 內容太大建議搜索少量再導出 4 // if(count($lists)>=20000) 5 // { 6 // header("Content-Type:text/html;charset=utf-8"); 7 // echo "<br/><h1 style='color:red'>Export data is too large, please narrow your search!</h1><br/>"; 8 // exit; 9 // } 10 $keys=array_keys($pres);//獲取表頭的鍵名 11 $content=""; 12 $content.="<table border='1'><tr>"; 13 //輸出表頭鍵值 14 foreach($pres as $_pre){ 15 $content.="<td>$_pre</td>"; 16 } 17 $content.="</tr>"; 18 foreach($lists as $_list){ 19 $content.= "<tr>"; 20 foreach($keys as $key){ 21 $content.= "<td style='vnd.ms-excel.numberformat:@'>".$_list[$key]."</td>"; //style樣式將導出的內容都設置為文本格式 輸出對應鍵名的鍵值 即內容 22 } 23 $content.="</tr>"; 24 } 25 $content.="</table>"; 26 return $content; 27 }
導出方法二 使用PHPExcel導出
1 public function export(){ 2 set_time_limit(0); 3 ini_set('memory_limit','1024M'); 4 $ranking = self::getResult(); //獲取需要導出的內容 5 include_once(EXTEND_PATH . 'Library/ORG/PHPExcel/PHPExcel.class.php'); //引入PHPExcel文件 6 $objPHPExcel = new PHPExcel(); 7 //設置文件的一些屬性,在xls文件——>屬性——>詳細信息里可以看到這些值 8 $objPHPExcel->getProperties()->setCreator("ctos") 9 ->setLastModifiedBy("ctos") 10 ->setTitle("Office 2007 XLSX Test Document") 11 ->setSubject("Office 2007 XLSX Test Document") 12 ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.") 13 ->setKeywords("office 2007 openxml php") 14 ->setCategory("Test result file"); 15 //設置列寬 16 $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(15); 17 $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15); 18 $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20); 19 $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20); 20 $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20); 21 //設置水平居中顯示 22 $objPHPExcel->getActiveSheet()->getStyle('A')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); 23 $objPHPExcel->getActiveSheet()->getStyle('B')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); 24 $objPHPExcel->getActiveSheet()->getStyle('C')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); 25 $objPHPExcel->getActiveSheet()->getStyle('D')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); 26 $objPHPExcel->getActiveSheet()->getStyle('E')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); 27 $objPHPExcel->setActiveSheetIndex(0) // set table header content 28 ->setCellValue('A1', 'Listing ID') 29 ->setCellValue('B1', '前十天銷量') 30 ->setCellValue('C1', '后十天銷量') 31 ->setCellValue('D1', '漲跌銷量') 32 ->setCellValue('E1', '漲跌幅度(%)') 33 $a = 1; //設置默認值 34 foreach($ranking as $k){ 35 $a++; 36 $objPHPExcel->setActiveSheetIndex(0) 37 ->setCellValueExplicit('A'.$a, $k['listing'],PHPExcel_Cell_DataType::TYPE_STRING)//設置數字的科學計數法顯示為文本 38 ->setCellValue('B'.$a, $k['salesbefore']) 39 ->setCellValue('C'.$a, $k['salesafter']) 40 ->setCellValue('D'.$a, $k['saleschange']) 41 ->setCellValue('E'.$a, $k['percentchange']) 42 //設置自動換行 43 $objPHPExcel->getActiveSheet()->getStyle("A$a")->getAlignment()->setWrapText(true); 44 $objPHPExcel->getActiveSheet()->getStyle("B$a")->getAlignment()->setWrapText(true); 45 } 46 $objPHPExcel->getActiveSheet()->setTitle('銷量變化排行榜'); 47 $objPHPExcel->setActiveSheetIndex(0); 48 // excel頭參數 49 header('Content-Type: application/vnd.ms-excel'); 50 header('Content-Disposition: attachment;filename="銷量變化排行榜('.date('YmdHis').').xls"'); //日期為文件名后綴 51 header('Cache-Control: max-age=0'); 52 $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); //excel5為xls格式,excel2007為xlsx格式 53 $objWriter->save('php://output'); 54 }