Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 关于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');
?>
Copy after login

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

回复讨论(解决方案)

有以下几种缓存方式可以使用:  
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 的规模使用内存  

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

自己到网上搜一下就是了  

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

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24
Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

How does PHP type hinting work, including scalar types, return types, union types, and nullable types? How does PHP type hinting work, including scalar types, return types, union types, and nullable types? Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

See all articles