目录
回复讨论(解决方案)
首页 后端开发 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脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前 By 尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
4 周前 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)

11个最佳PHP URL缩短脚本(免费和高级) 11个最佳PHP URL缩短脚本(免费和高级) Mar 03, 2025 am 10:49 AM

长URL(通常用关键字和跟踪参数都混乱)可以阻止访问者。 URL缩短脚本提供了解决方案,创建了简洁的链接,非常适合社交媒体和其他平台。 这些脚本对于单个网站很有价值

Instagram API简介 Instagram API简介 Mar 02, 2025 am 09:32 AM

在Facebook在2012年通过Facebook备受瞩目的收购之后,Instagram采用了两套API供第三方使用。这些是Instagram Graph API和Instagram Basic Display API。作为开发人员建立一个需要信息的应用程序

在Laravel中使用Flash会话数据 在Laravel中使用Flash会话数据 Mar 12, 2025 pm 05:08 PM

Laravel使用其直观的闪存方法简化了处理临时会话数据。这非常适合在您的应用程序中显示简短的消息,警报或通知。 默认情况下,数据仅针对后续请求: $请求 -

构建具有Laravel后端的React应用程序:第2部分,React 构建具有Laravel后端的React应用程序:第2部分,React Mar 04, 2025 am 09:33 AM

这是有关用Laravel后端构建React应用程序的系列的第二个也是最后一部分。在该系列的第一部分中,我们使用Laravel为基本的产品上市应用程序创建了一个RESTFUL API。在本教程中,我们将成为开发人员

简化的HTTP响应在Laravel测试中模拟了 简化的HTTP响应在Laravel测试中模拟了 Mar 12, 2025 pm 05:09 PM

Laravel 提供简洁的 HTTP 响应模拟语法,简化了 HTTP 交互测试。这种方法显着减少了代码冗余,同时使您的测试模拟更直观。 基本实现提供了多种响应类型快捷方式: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

php中的卷曲:如何在REST API中使用PHP卷曲扩展 php中的卷曲:如何在REST API中使用PHP卷曲扩展 Mar 14, 2025 am 11:42 AM

PHP客户端URL(curl)扩展是开发人员的强大工具,可以与远程服务器和REST API无缝交互。通过利用Libcurl(备受尊敬的多协议文件传输库),PHP curl促进了有效的执行

在Codecanyon上的12个最佳PHP聊天脚本 在Codecanyon上的12个最佳PHP聊天脚本 Mar 13, 2025 pm 12:08 PM

您是否想为客户最紧迫的问题提供实时的即时解决方案? 实时聊天使您可以与客户进行实时对话,并立即解决他们的问题。它允许您为您的自定义提供更快的服务

宣布 2025 年 PHP 形势调查 宣布 2025 年 PHP 形势调查 Mar 03, 2025 pm 04:20 PM

2025年的PHP景观调查调查了当前的PHP发展趋势。 它探讨了框架用法,部署方法和挑战,旨在为开发人员和企业提供见解。 该调查预计现代PHP Versio的增长

See all articles