使用PHPExcel要点解析
我们在网站开发中经常都会用到
以前准备做一个PHP将excel导入mysql数据库的通用程序,基本完成之后就没有管它了。上个月,教务处老师叫我帮忙做一个“国家等级考试报名查询系统”,也需要将学生excel信息导入导出,照片zip压缩导入导出。
上一周刚完成这个程序,虽然用java这样的语言处理比较简单,不过PHP增加这个功能却是5.2以上的版本才能够完成(需要新增的PHP_zip扩展)。毕竟一个月的接触,也略微懂了一些最基础的用法,也遇到了一起才起手很容易碰到的问题,网上关于这个介绍比较简略,我想具体的介绍一下它的基本用法和可能遇到的基本问题。
首先,你需要打开PHP_zip扩展,找到你起作用的PHP.INI文件,并打开这个扩展,将PHP文件夹里面的ext文件夹中的PHP_zip.dll文件找到,并复制到system32系统文件夹中(具体要看你的配置方法)。
网上最多的问题就是使用PHPExcel的时候,没有打开这个扩展。就会显示诸如 下面我只想说说一些需要注意和容易错的地方。 1.使用PHPExcel创建excel文档对象有2种。 一种是直接创建 include 'PHPExcel/Writer/Excel2007.PHP'; 另外一种是通过reader类进行读取创建 require 'PHPExcel/Reader/Excel2007.PHP'; 我这里想说明的是,这里包含excel2007.PHP, 就是创建的xlsx类型的excel,也就是ms office2007打开的,如果要创建以前的xls的话(office2007以前版本),就需要包含excel5.PHP,而不是 excel2007,不管是reader类还是writer类,这个要注意。比如上面说的2种用法就应该直接创建: include 'PHPExcel/Writer/Excel5.PHP'; 通过reader类进行读取创建 require 'PHPExcel/Reader/Excel5.PHP'; 2.看了使用PHPExcel给出的文档之后很有可能问,我就是想读取excel的一个单元格的值,怎么没有给出怎么读呢?这个开始我也遇到过,你可以这样: $sheet = $objPHPExcel->getActiveSheet(); 当然,这个一般情况下也是能用了,如果你的excel是公式,你就应该 $sheet = $objPHPExcel->getActiveSheet(); 3.使用PHPExcel关于长数字被转换成科学计数法的问题。 并且最后几位被忽略为0,这个问题困扰了我很久,就是比如身份证,学号这样的数字,如果你直接setValue的话,出来的excel被自动转换成科学计数法,网上本来这样的资料少,而且我发现大多数还是错的。我查到了一篇文档,是通过改动PHPexcel源码实现的 Writer/Excel5文件,第202行, 然后写入excel的时候通过以文本格式写入就可以了(不修改源代码以 文本格式写入也是科学技术 法) $objPHPExcel->getActiveSheet()->setCellValueExplicit($letters_arr[$j+1] . ($i+1),$this->student_info[$i][$j],PHPExcel_Cell_DataType::TYPE_STRING); 4.使用PHPExcel如何得到excel的列数和行数? 往往开始使用PHPexcel会觉得不好用的地方也是如此,因为它的test示例程序也没有给出这个。以下是我在codeplex问的问题以及解答。大家看了也就知道了(帖子后面图片)。 5.使用PHPExcel如何通过循环得到每一个单元格的值。 ObjPHPExcel这个对象你print_r输出会 看到很多复杂的东西,其实通过自带的方法会很简单。我是采用这样的方法 $letters_arr = array(1=>'A',2=>'B',3=>'C',4=>'D',5=>'E',6=>'F',7=>'G',8=>'H',9=>'I',10=>'J',11=>'K',12=>'L',13=>'M', 14=>'N',15=>'O',16=>'P',17=>'Q',18=>'R',19=>'S',20=>'T',21=>'U',22=>'V',23=>'W',24=>'X',25=>'Y',26=>'Z'); 通过设置一个字符的数组,就可以用循环变量循环列数了。这里我还出了个小笑话,以前我居然想通过A字符转换为ASC码来循环,未果,我翻了一下PHP的书才知道PHP转换字符成整形和C,C++不是一样的,真是基础不扎实啊。 6.使用PHPExcel关于数据库和excel的编码问题。 这个也需要大家注意,excel是采用的UTF-8编码,于是你每次从数据库读出数据后,应该不要忘了转换一次。 用print_r打印出来一看,那个单元格也是空的,这个并不是转换编码问题,因为如果是编码,应该打印出乱码, 这个却是PHPexcel没有读出那一列的汉字。这个问题我也不是太明白,我打开reader的excel5.PHP文件 把$this->_defaultEncoding = 'isoXXXXX';改为$this->_defaultEncoding = 'UTF-8';解决的,就能读出乱码了,之后可以通过转换编码解决。我在官方网站问了也未果,大家有更好的方法也可以跟我说说,谢谢。
$objPHPExcel = new PHPExcel();
$objReader = new PHPExcel_Reader_Excel2007;
$objPHPExcel = $objReader->load("mytest.xlsx");
$objPHPExcel = new PHPExcel();
$objReader = new PHPExcel_Reader_Excel5;
$objPHPExcel = $objReader->load("mytest.xlsx");
$string = $sheet->getCell('F2')->getValue();
$string = $sheet->getCell('F2')-> getCalculatedValue();<ol class="dp-xml">
<li class="alt"><span><span>if ($cell-</span><span class="tag">></span><span>hasHyperlink()) { </span></span></li>
<li>
<span>$worksheet-</span><span class="tag">></span><span>writeUrl($row, $column,<br> str_replace('sheet://', 'internal:', <br>$cell-</span><span class="tag">></span><span>getHyperlink()-</span><span class="tag">></span><span>getUrl()), <br>$cell-</span><span class="tag">></span><span>getValue(), $formats[$styleHash]); </span>
</li>
<li class="alt"><span>} </span></li>
<li><span>else { </span></li>
<li class="alt">
<span>$worksheet-</span><span class="tag">></span><span>write($row, $column, $cell-</span><span class="tag">><br></span><span>getValue(), $formats[$styleHash],$style-</span><span class="tag">><br></span><span>getNumberFormat()-</span><span class="tag">></span><span>getFormatCode()); </span>
</li>
<li><span>} </span></li>
<li class="alt">
<span>改为if ($cell-</span><span class="tag">></span><span>hasHyperlink()) { </span>
</li>
<li>
<span>$worksheet-</span><span class="tag">></span><span>writeUrl($row, $column, <br>str_replace('sheet://', 'internal:', <br>$cell-</span><span class="tag">></span><span>getHyperlink()-</span><span class="tag">></span><span>getUrl()), $cell-</span><span class="tag">><br></span><span>getValue(), $formats[$styleHash]); </span>
</li>
<li class="alt">
<span>}else if($cell-</span><span class="tag">></span><span>getDataType() == <br></span>PHP<span>Excel_Cell_DataType::TYPE_STRING ) { </span>
</li>
<li>
<span>$worksheet-</span><span class="tag">></span><span>writeString($row,$column,$cell-</span><span class="tag">><br></span><span>getValue(),$formats[$styleHash]); </span>
</li>
<li class="alt"><span>} </span></li>
<li><span>else { </span></li>
<li class="alt">
<span>$worksheet-</span><span class="tag">></span><span>write($row,$column,$cell-</span><span class="tag">><br></span><span>getValue(),$formats[$styleHash],$style-</span><span class="tag">><br></span><span>getNumberFormat()-</span><span class="tag">></span><span>getFormatCode());} </span>
</li>
</ol>
$objPHPExcel->getActiveSheet()->getStyle($letters_arr[$j+1] . ($i+1))->getNumberFormat()->setFormatCode("@");
$this->student_info[$i][$j]=iconv("gbk","UTF-8",$this->student_info[$i][$j]);但是记住,问题却没有这样简单,当你使用PHPExcel从excel读取数据的时候,可能会发现居然有汉字的列会没有读出来,是空的。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

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 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.

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

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.
