目錄
回复讨论(解决方案)
首頁 後端開發 php教程 关于PHPEXCEL输出报表有上万条记录的时候貌似挂了?

关于PHPEXCEL输出报表有上万条记录的时候貌似挂了?

May 12, 2018 am 10:22 AM

这是一个完整可用的phpexcel例子  
 

getActiveSheet()->freezePane('A2');
$sharedStyle1 = new PHPExcel_Style();
$sharedStyle1->applyFromArray(    
array('fill' => array(                
'type'	=> PHPExcel_Style_Fill::FILL_SOLID,                
'color'	=> array('argb' => 'FFCCFFCC')            
),          
'borders' => array(                
'bottom'=> array('style' => PHPExcel_Style_Border::BORDER_THIN),                
'right'	=> array('style' => PHPExcel_Style_Border::BORDER_MEDIUM)                
)         
));
$sharedStyle2 = new PHPExcel_Style();
$sharedStyle2->applyFromArray(    
array('fill' => array(                
'type'	=> PHPExcel_Style_Fill::FILL_SOLID,                
'color'	=> array('argb' => 'fff4f4f4')            
)         
));
$objPHPExcel->getActiveSheet()->setSharedStyle($sharedStyle1, "A1:O1");   
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(50);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(50);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(50);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(50);
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(50);
$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(50);
$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(50);
$objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(50);
$objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(50);
$objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth(50);
$objPHPExcel->getActiveSheet()->getColumnDimension('K')->setWidth(50);
$objPHPExcel->getActiveSheet()->getColumnDimension('L')->setWidth(50);
$objPHPExcel->getActiveSheet()->getColumnDimension('M')->setWidth(50);
$objPHPExcel->getActiveSheet()->getColumnDimension('N')->setWidth(50);
$objPHPExcel->getActiveSheet()->getColumnDimension('O')->setWidth(50);
$objPHPExcel->getActiveSheet()->setTitle('Student Export');//?置?前工作表的名?
for($i=0;$igetActiveSheet()->getStyleByColumnAndRow($i, 1)->getFont()->setBold(true);//?置第一行?容加粗
}
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0, 1,'First Name');
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1, 1,'Last Name');
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(2, 1,'Date of Birth');
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(3, 1,'Attendance Year');
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(4, 1,'Course studied');
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(5, 1,'Agency Booking / Direct Booking');
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(6, 1,'Student Country');
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(7, 1,'Student City');
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(8, 1,'Student Home phone number');
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(9, 1,'Student Mobile phone number');
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(10, 1,'Student Email address');
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(11, 1,'Emergency contact Name');
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(12, 1,'Emergency contact phone number 1');
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(13, 1,'Emergency contact phone number 2');
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(14, 1,'Order Date');$number=1;$orderdate='';
$firstname='';
$lastname='';
$dateodbirth='';
$attenddance='';
$studied='';
$booking='';
$country='';
$city='';
$home_phone='';
$mobile_phone='';
$email='';
$emer_name='';
$emer_phone1='';
$emer_phone2='';
$sql="select orders_id,date_purchased from orders order by orders_id desc";
$query=mysql_query($sql);
while($arr=mysql_fetch_array($query)){	
$number++;	
$orderdate=$arr['date_purchased'];	
$sql2="select * from orders_extra_info where orders_id='".$arr['orders_id']."'";	
$query2=mysql_query($sql2);	
$num2=mysql_num_rows($query2);	
if($num2>0)
{		
$arr2=mysql_fetch_array($query2);		
$firstname=$arr2['first_name'];		
$lastname=$arr2['last_name'];		
$dateodbirth=$arr2['date_of_birth'];		
$attenddance=$arr2['o_attendaceyear'];		
$booking=$arr2['o_agency'];		
if($booking==1)
{
$booking='Direct Sale';
}		
if($booking==2)
{
$booking='Agent Sale';
}		
$country_id=$arr2['country'];		
$city=$arr2['city'];		
$home_phone=$arr2['phone1'];		
$mobile_phone=$arr2['phone2'];		
$email=$arr2['email'];		
$emer_name=$arr2['emc_name'];		
$emer_phone1=$arr2['emc_phone1'];		
$emer_phone2=$arr2['emc_phone2'];		
$cou_sql="select countries_id,countries_name from countries where countries_id='".$country_id."'";		
$con_query=mysql_query($cou_sql);		
$con_arr=mysql_fetch_array($con_query);		
$country=$con_arr['countries_name'];	
}	
$sql3="select products_name,orders_id from orders_products where orders_id='".$arr['orders_id']."'";	
$query3=mysql_query($sql3);	
$num3=mysql_num_rows($query3);	
if($num3>0)
{		
$studied='';		
while($arr3=mysql_fetch_array($query3))
{			
$studied.=$arr3['products_name'].",";		
}		
$studied=substr($studied,0,strlen($studied)-1);	
}	
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0, $number,$firstname);	
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1, $number,$lastname);	
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(2, $number,$dateodbirth);	
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(3, $number,$attenddance);	
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(4, $number,$studied);	
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(5, $number,$booking);	
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(6, $number,$country);	
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(7, $number,$city);	
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(8, $number,$home_phone);	
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(9, $number,$mobile_phone);	
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(10, $number,$email);	
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(11, $number,$emer_name);	
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(12, $number,$emer_phone1);	
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(13, $number,$emer_phone2);	
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(14, $number,$orderdate);
}
$objPHPExcel->setActiveSheetIndex(0);//?置打?excel??示哪?工作表
$excelName = 'Student_Export'.date("YmdHis").'.xls';//?置?出excel的文件名
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment; 
filename=".urlencode($excelName));
header("Content-Transfer-Encoding: binary");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: no-cache");
$objWriter->save('php://output');
require(DIR_WS_INCLUDES . 'application_bottom.php');
?>
登入後複製

 输出来的报表,当有上万条记录时,不能导出了,浏览器一直再转,只有小数据量的时候才有用,这时怎么办?  

回复讨论(解决方案)

有以下几种缓存方式可以使用:  
1). PHPExcel_CachedObjectStorageFactory::cache_in_memory;  
      默认情况下,如果你不初始化任何缓存方式,PHPExcel将使用内存缓存的方式。  
2).PHPExcel_CachedObjectStorageFactory::cache_in_memory_serialized;   
     使用这种缓存方式,单元格会以序列化的方式保存在内存中,这是降低内存使用率性能比较高的一种方案。  
3). PHPExcel_CachedObjectStorageFactory::cache_in_memory_gzip;   
     与序列化的方式类似,这种方法在序列化之后,又进行gzip压缩之后再放入内存中,这回跟进一步降低内存的使用,但是读取和写入时会有一些慢。  
4). PHPExcel_CachedObjectStorageFactory::cache_to_discISAM;  
      当使用cache_to_discISAM这种方式时,所有的单元格将会保存在一个临时的磁盘文件中,只把他们的在文件中的位置保存在PHP的内存中,这 会比任何一种缓存在内存中的方式都慢,但是能显著的降低内存的使用。临时磁盘文件在脚本运行结束是会自动删除。  
5). PHPExcel_CachedObjectStorageFactory::cache_to_phpTemp;  
      类似cache_to_discISAM这种方式,使用cache_to_phpTemp时,所有的单元格会还存在php://temp I/O流中,只把他们的位置保存在PHP的内存中。PHP的php://memory包裹器将数据保存在内存中,php://temp的行为类似,但是当 存储的数据大小超过内存限制时,会将数据保存在临时文件中,默认的大小是1MB,但是你可以在初始化时修改它:应该最多值能导出2万条。有以下几种缓存方式可以使用:  

1). PHPExcel_CachedObjectStorageFactory::cache_in_memory;  

      默认情况下,如果你不初始化任何缓存方式,PHPExcel将使用内存缓存的方式。  
2).PHPExcel_CachedObjectStorageFactory::cache_in_memory_seria……  
内存问题,标记~

补充一下  
PHPExcel 官网说,PHPExcel 以每个单元格 1K 的规模使用内存  

据说可以什么分批导入?有没有例子怎么去写

自己到网上搜一下就是了  

没哟这样的需求,谁会去测试?

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系統,解釋
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆樹的耳語 - 如何解鎖抓鉤
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1666
14
CakePHP 教程
1425
52
Laravel 教程
1325
25
PHP教程
1273
29
C# 教程
1252
24
說明PHP中的安全密碼散列(例如,password_hash,password_verify)。為什麼不使用MD5或SHA1? 說明PHP中的安全密碼散列(例如,password_hash,password_verify)。為什麼不使用MD5或SHA1? Apr 17, 2025 am 12:06 AM

在PHP中,應使用password_hash和password_verify函數實現安全的密碼哈希處理,不應使用MD5或SHA1。1)password_hash生成包含鹽值的哈希,增強安全性。 2)password_verify驗證密碼,通過比較哈希值確保安全。 3)MD5和SHA1易受攻擊且缺乏鹽值,不適合現代密碼安全。

PHP和Python:比較兩種流行的編程語言 PHP和Python:比較兩種流行的編程語言 Apr 14, 2025 am 12:13 AM

PHP和Python各有優勢,選擇依據項目需求。 1.PHP適合web開發,尤其快速開發和維護網站。 2.Python適用於數據科學、機器學習和人工智能,語法簡潔,適合初學者。

PHP行動:現實世界中的示例和應用程序 PHP行動:現實世界中的示例和應用程序 Apr 14, 2025 am 12:19 AM

PHP在電子商務、內容管理系統和API開發中廣泛應用。 1)電子商務:用於購物車功能和支付處理。 2)內容管理系統:用於動態內容生成和用戶管理。 3)API開發:用於RESTfulAPI開發和API安全性。通過性能優化和最佳實踐,PHP應用的效率和可維護性得以提升。

PHP:網絡開發的關鍵語言 PHP:網絡開發的關鍵語言 Apr 13, 2025 am 12:08 AM

PHP是一種廣泛應用於服務器端的腳本語言,特別適合web開發。 1.PHP可以嵌入HTML,處理HTTP請求和響應,支持多種數據庫。 2.PHP用於生成動態網頁內容,處理表單數據,訪問數據庫等,具有強大的社區支持和開源資源。 3.PHP是解釋型語言,執行過程包括詞法分析、語法分析、編譯和執行。 4.PHP可以與MySQL結合用於用戶註冊系統等高級應用。 5.調試PHP時,可使用error_reporting()和var_dump()等函數。 6.優化PHP代碼可通過緩存機制、優化數據庫查詢和使用內置函數。 7

PHP類型提示如何起作用,包括標量類型,返回類型,聯合類型和無效類型? PHP類型提示如何起作用,包括標量類型,返回類型,聯合類型和無效類型? Apr 17, 2025 am 12:25 AM

PHP類型提示提升代碼質量和可讀性。 1)標量類型提示:自PHP7.0起,允許在函數參數中指定基本數據類型,如int、float等。 2)返回類型提示:確保函數返回值類型的一致性。 3)聯合類型提示:自PHP8.0起,允許在函數參數或返回值中指定多個類型。 4)可空類型提示:允許包含null值,處理可能返回空值的函數。

PHP的持久相關性:它還活著嗎? PHP的持久相關性:它還活著嗎? Apr 14, 2025 am 12:12 AM

PHP仍然具有活力,其在現代編程領域中依然佔據重要地位。 1)PHP的簡單易學和強大社區支持使其在Web開發中廣泛應用;2)其靈活性和穩定性使其在處理Web表單、數據庫操作和文件處理等方面表現出色;3)PHP不斷進化和優化,適用於初學者和經驗豐富的開發者。

PHP和Python:代碼示例和比較 PHP和Python:代碼示例和比較 Apr 15, 2025 am 12:07 AM

PHP和Python各有優劣,選擇取決於項目需求和個人偏好。 1.PHP適合快速開發和維護大型Web應用。 2.Python在數據科學和機器學習領域佔據主導地位。

PHP與其他語言:比較 PHP與其他語言:比較 Apr 13, 2025 am 12:19 AM

PHP適合web開發,特別是在快速開發和處理動態內容方面表現出色,但不擅長數據科學和企業級應用。與Python相比,PHP在web開發中更具優勢,但在數據科學領域不如Python;與Java相比,PHP在企業級應用中表現較差,但在web開發中更靈活;與JavaScript相比,PHP在後端開發中更簡潔,但在前端開發中不如JavaScript。

See all articles