Home Backend Development PHP Tutorial PHPExcel使用中遇到的几个小问题

PHPExcel使用中遇到的几个小问题

Jun 13, 2016 pm 01:17 PM
cache gt phpexcel

PHPExcel使用中遇到的几个问题

最近做项目,接触了几次PHPExcel,过程中遇到了几个问题,现对PHPExcel的一些常见用法和我遇到的问题及解决方法总结如下。(推荐还是要看一下PHPExcel官网的手册)

?

一、常见用法

??? 这个部分网站上有很多资料了,我这里不再一一罗列,只是将我用到的几个地方写了一下。

?

//首先要导入phpexcel
require_once ($yourpath.'PHPExcel.php');
//创建phpexcel对象
$objPHPExcel = new PHPExcel ();
//设置属性
$objPHPExcel->getProperties ()->setCreator ( "Miss Yang" )
		->setLastModifiedBy ( "Miss Yang" )
		->setTitle ( "Office 2003 XLS Document" )
		->setSubject ( "Office 2003 XLS Document" )
		->setDescription ( "TrunkBow" )
		->setKeywords ( "TrunkBow" )
		->setCategory ( "TrunkBow" );
        
//设置当前的sheet索引,用于后续的内容操作。  
//缺省情况下,PHPExcel会自动创建第一个sheet被设置SheetIndex=0  
$objExcel->setActiveSheetIndex(0); 
$objActSheet = $objExcel->getActiveSheet();  
 
 //设置当前活动sheet的名称  
$objActSheet->setTitle('Sheet1');  

//设置单元格内容
 $objActSheet->setCellValue('A1', '字符串内容');  // 字符串内容  
 $objActSheet->setCellValue('A2', 26);            // 数值  
 $objActSheet->setCellValue('A3', true);          // 布尔值  
 $objActSheet->setCellValue('A4', '=SUM(A2:A2)'); // 公式   

//显式指定内容类型  
 $objActSheet->setCellValueExplicit('A5','8757584',PHPExcel_Cell_DataType::TYPE_STRING);

//设置单元格格式
$objActSheet ->getStyle ('A1' )
             ->getFont ()
             ->getColor()
             ->setARGB(PHPExcel_Style_Color::COLOR_RED);   
$objActSheet ->getStyle ('A1' )
             ->getFont ()
             ->setBold(true); 
$objActSheet ->getStyle ('A')
             ->getNumberFormat()
             ->setFormatCode ( PHPExcel_Style_NumberFormat::FORMAT_TEXT );
$objActSheet ->getColumnDimension ('A')
             ->setWidth(20); 
$objActSheet ->getColumnDimension ('B')
             ->setAutoSize(true); 
$activeSheet ->getColumnDimension('C')
             ->setVisible(false); 

//生成下拉列表框
$list = "item1,item2,item3";
$objValidation1 = $activeSheet->getCell('A1')->getDataValidation();
$objValidation1->setType( PHPExcel_Cell_DataValidation::TYPE_LIST )
               ->setErrorStyle(PHPExcel_Cell_DataValidation::STYLE_INFORMATION )
               ->setAllowBlank(false)
               ->setShowInputMessage(true)
               ->setShowErrorMessage(true)
               ->setShowDropDown(true)
               ->setErrorTitle('输入的值有误')
               ->setError('您输入的值不在下拉框列表内.')
               ->setPromptTitle('下拉选择框')
               ->setPrompt('请从下拉框中选择您需要的值!')
               ->setFormula1('"' . $list . '"');    

//下载输出
$filename = "testphp.xls";
$filename = iconv("utf-8", 'gbk', $filename);
$objWriter = PHPExcel_IOFactory::createWriter ( $objPHPExcel, 'Excel5' );
header ( 'Content-Type: application/vnd.ms-excel' );
header ( "Content-Disposition: attachment;filename=$filename" );
header ( 'Cache-Control: max-age=0' );
$objWriter->save ( 'php://output' );
exit(0);   
Copy after login

?

?

?

二、问题总结

1.单元格内容为长数字的,输出时,总是转成科学计数法。

?

??? 这个问题网上也有不少资料提到的,大多是说要修改几个地方的源码。但是我一直没找到对应的源码在哪里,可能是版本不同的原因。我是这样解决的:

?

//显示指定内容类型
 $objActSheet->setCellValueExplicit('A5','8757584',PHPExcel_Cell_DataType::TYPE_STRING); 
Copy after login

??

2.PHPExcel占用内存过大。

?

PHPExcel是一个很强大的处理ExcelPHP开源类,但是很大的一个问题就是它占用内存太大,从1.7.3开始,它支持设置cell的缓存方式,但是推荐使用目前稳定的版本1.7.6,因为之前的版本都会不同程度的存在bug

官网上是这么说的:PHPExcel平均下来使用1k/单元格的内存,因此大的文档会导致内存消耗的也很快。单元格缓存机制能够允许PHPExcel将内存中的小的单元格对象缓存在磁盘或者APCmemcache或者Wincache中,尽管会在读取数据上消耗一些时间,但是能够帮助你降低内存的消耗。默认情况下,PHPExcel依然将单元格对象保存在内存中,但是你可以自定义。你可以使用PHPExcel_Settings::setCacheStorageMethod()方法,将缓存方式作为参数传递给这个方法来设置缓存的方式。

??

$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_in_memory; 
PHPExcel_Settings::setCacheStorageMethod($cacheMethod); 
Copy after login

?

?

setCacheStorageMethod()方法会返回一个BOOL型变量用于表示是否成功设置(比如,如果APC不能使用的时候,你设置使用APC缓存,将会返回false)。每一个worksheet都会有一个独立的缓存,当一个worksheet实例化时,就会根据设置或配置的缓存方式来自动创建。一旦你开始读取一个文件或者你已经创建了第一个worksheet,就不能在改变缓存的方式了。目前,有以下几种缓存方式可以使用:

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的内存中。PHPphp://memory包裹器将数据保存在内存中,php://temp的行为类似,但是当存储的数据大小超过内存限制时,会将数据保存在临时文件中,默认的大小是1MB,但是你可以在初始化时修改它:

$cacheMethod = PHPExcel_CachedObjectStorageFactory:: cache_to_phpTemp; 
$cacheSettings = array( ' memoryCacheSize ' => '8MB' ); 
PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings); 
Copy after login

?

?

php://temp文件在脚本结束是会自动删除。

?

6). PHPExcel_CachedObjectStorageFactory::cache_to_apc;

当使用cach_to_apc时,单元格保存在APC中,只在内存中保存索引。APC缓存默认超时时间时600秒,对绝大多数应用是足够了,当然你也可以在初始化时进行修改:

?

?

$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_APC; 
$cacheSettings = array( 'cacheTime' => 600 ); 
PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings); ?
Copy after login

?

?

当脚本运行结束时,所有的数据都会从APC中清楚(忽略缓存时间),不能使用此机制作为持久缓存。

?

7).PHPExcel_CachedObjectStorageFactory::cache_to_memcache

??????? 使用cache_to_memory时,单元格对象保存在memcache中,只在内存中保存索引。默认情况下,PHPExcel会在localhost和端口11211寻找memcache服务,超时时间600秒,如果你在其他服务器或其他端口运行memcache服务,可以在初始化时进行修改:

?

$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_memcache; 
$cacheSettings = array( 'memcacheServer'=> 'localhost', 
                                       'memcachePort' => 11211, 
                                       'cacheTime' => 600 );  
PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings); 

Copy after login

??

???????从初始化设置的形式上看,MS还不支持多台memcache服务器轮询的方式,比较遗憾。

?当脚本结束时,所有的数据都会从memcache清空(忽略缓存时间),不能使用该机制进行持久存储。

?

3.下拉列表的数据来源过长

?

?

??? 我们还是先来看手册是怎么说的:

???

It is important to remember that any string participating in an Excel formula is allowed to be maximum 255 characters (not bytes).

?

???

???? 当下拉列表的数据来源过长(more than 255 characters)时,该下拉列表会显示不正确(我遇到的情况是:该下拉列表显示了别的正常的下拉列表的数据来源).

以下是我的解决办法:

//解决下拉框数据来源字串长度过大:将每个来源字串分解到一个空闲的单元格中
$str_list = "item1,item2,item3,......" ;
$str_len = strlen($str_list);
if($str_len>=255){
        $str_list_arr = explode(',', $str_list); 
        if($str_list_arr) 
              foreach($str_list_arr as $i =>$d){
                     $c = "P".($i+1);
                     $activeSheet->setCellValue($c,$d); 
               } 
         $endcell = $c;
         $activeSheet->getColumnDimension('P')->setVisible(false); 
} 

$objValidation2 = $activeSheet->getCell("A1")->getDataValidation(); 
$objValidation2->setType( PHPExcel_Cell_DataValidation::TYPE_LIST ) 
                ->setErrorStyle( PHPExcel_Cell_DataValidation::STYLE_INFORMATION ) 
               ->setAllowBlank(true) 
               ->setShowInputMessage(true)
               ->setShowErrorMessage(true) 
               ->setShowDropDown(true)
               ->setErrorTitle('输入的值有误') 
               ->setError('您输入的值不在下拉框列表内.')
               ->setPromptTitle('下拉选择框')
               ->setPrompt('请从下拉框中选择您需要的值!');
if($str_lensetFormula1('"' . $str_list . '"'); 
else 
       $objValidation2->setFormula1("sheet1!P1:{$endcell}"); 
Copy after login

?

4.本地运行正常,服务器上PHPExcel不能运行

官网:

????????? The following software is required to develop using PHPExcel:

?????????? ? PHP version 5.2.0 or newer

?????????? ? PHP extension php_zip enabled *)

???????????? PHP extension php_xml enabled

???????????? PHP extension php_gd2 enabled (if not compiled in)

? 另外,请检查iconv扩展是否正常,PHPExcel需要他的支持

?

?

?

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 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)

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

After joining the company, I understood what Cache is After joining the company, I understood what Cache is Jul 31, 2023 pm 04:03 PM

The thing is actually like this. At that time, my leader gave me a perf hardware performance monitoring task. During the process of using perf, I entered the command perf list and I saw the following information: My task is to enable these cache events to be counted normally. But the point is, I have no idea what these misses and loads mean.

Fix: Snipping tool not working in Windows 11 Fix: Snipping tool not working in Windows 11 Aug 24, 2023 am 09:48 AM

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

Complete Guide: How to process Excel files using php extension PHPExcel Complete Guide: How to process Excel files using php extension PHPExcel Jul 28, 2023 pm 10:01 PM

Complete Guide: How to Process Excel Files Using PHP Extension PHPExcel Introduction: Excel files are often used as a common format for data storage and exchange when processing large amounts of data and statistical analysis. Using the PHP extension PHPExcel, we can easily read, write and modify Excel files to effectively process Excel data. This article will introduce how to use the PHP extension PHPExcel to process Excel files and provide code examples. 1. Install PHPExc

PHP development: Use PHPExcel to process Excel files PHP development: Use PHPExcel to process Excel files Jun 15, 2023 pm 03:45 PM

With the advent of the digital age, data has become the most important part of our daily lives and work, and Excel files have become one of the important tools for data processing. I believe that many PHP developers will often encounter the use of Excel files for data processing and operations at work. This article will introduce you to the methods and precautions for using the PHPExcel library to process Excel files. What is PHPExcel? PHPExcel is a PHP class

Why does using cache increase computer speed? Why does using cache increase computer speed? Dec 09, 2020 am 11:28 AM

Using the cache can increase the speed of the computer because the cache shortens the waiting time of the CPU. Cache is a small but high-speed memory located between the CPU and the main memory DRAM. The function of Cache is to increase the rate of CPU data input and output; Cache has a small capacity but fast speed, while the memory speed is low but has a large capacity. By optimizing the scheduling algorithm, the performance of the system will be greatly improved.

What is cache? What is cache? Nov 25, 2022 am 11:48 AM

Cache is called cache memory. It is a high-speed small-capacity memory between the central processing unit and the main memory. It is generally composed of high-speed SRAM. This kind of local memory is oriented to the CPU. It is introduced to reduce or eliminate the gap between the CPU and the memory. The impact of the speed difference between them on system performance. Cache capacity is small but fast, memory speed is low but capacity is large. By optimizing the scheduling algorithm, the performance of the system will be greatly improved.

How to Fix Can't Connect to App Store Error on iPhone How to Fix Can't Connect to App Store Error on iPhone Jul 29, 2023 am 08:22 AM

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

See all articles