最近接触了一下PHP.发现入门倒还蛮容易的,毕竟和C++很像.但是入门的内容无非是一些最简单的基础,真正的难点在于PHP的很多拓展.
这几天概要的学习了一下PHPExcel,之所以说自己是概要的学习,是因为主要是以看网上的例子作为学习的主线,结合官方pdf文档.
下面就简要的对于近期的学习做一个小结.
1. 结构:
PHPExcel的构建具有很清晰的结构.明确了这个点,对于接下来的学习至关重要.
工作簿: 官方文档中叫workbook.对应PHPExcel中的PHPExcel类.
工作表: worksheet , 对应PHPExcel中的sheet表格,具体类名我没有看,可以通过getSheet获取到.
单元格: Cell .存储数据的最小单元.
以上的这三个概念是从上到下的包含关系,工作簿包含工作表,工作表包含单元格.
当然还有另外两个重要的抽象概念: 读和写.
在PHPExcel中,读写这两个动作被抽象成了类.这个用法还是很方便的.当我们需要读一个表格的时候,我们使用reader类的对象加载文件.当我们需要写的时候,只要再用writer类打开对象即可.
2. 类与方法.
PHPExcel里面的类有很多.我最近用到的没有几个.简单列举一下:
PHPExcel
PHPExcel_Writer
PHPExcel_Reader
PHPExcel_IOFactory (这就是传说中的工厂设计模式,根据调用的方法,来构造出writer类的对象或者reader类的对象.)
PHPExcel的方法我目前接触到的如下:
PHPExcel_IOFactory::load 加载excel文件.默认调用了reader类的方法,返回的是PHPExcel类的对象.
PHPExcel_IOFactory::createWriter(PHPExcel, "Excel5") 这里用来创建一个已经存在的PHPExcel类的对象的写类,后面的参数用来指定excel的后缀.常用的还有Excel007.
PHPExcel->getActiveSheet 获取当前默认激活着的表格.
PHPExcel->getSheet(index) 根据index获取sheet
PHPExcel->removeSheetByIndex 根据index删除sheet表格.
PHPExcel->addSheet() 添加一个新的sheet表格
PHPExcel->addExternalSheet() 添加一个外部表格,说到这个方法,就要提到另外一个关键字,clone.这个关键字可以克隆出一个表格的复制品.
Sheet->getCellByColumnAndRow() 注意PHPExcel中,column的下标是从0计算的,而row的下标是从1开始计算的.
Sheet->getHighestRow() 获取当前表格的最大行数
Sheet->getHighestColumn() 获取当前表格的最大列数
PHPExcel_Cell::columnIndexFromString() 当前的列数获取到以后,这个列是以字母的形式存在的,用起来很不方便,所以就有了这个函数,他可以把字母的列转成数字.
Sheet->getCell(A1) 它的参数类似这个样子.同样可以获取一个Cell的内容.
Sheet->setCellValueByColumnAndRow(column, row, value) 给column和row指代的cell更新值为value
Sheet->getRowIterator() 获取当前行的迭代器
Sheet->insertNewRowBefore($currentRow, $rownum) 在当前行的前面插入$rownum个行.
PHPExcel_Cell->setValue() 为当前的Cell设置一个值.
需要留意的是,PHPExcel并不仅仅只能输出Excel文件,还可以输出PDF,html文件等.这个我没有了解过.不做讨论.
下面是我自己设计实现的一个小例子.电话簿管理系统.支持查找插入删除.当然并没有严格的限制一些方面.实现的只是基本的功能.不过相信可以帮助同学们理解PHPExcel.
excel表格式如下:
名字 号码 备注
phone.php负责查找和插入:
<title> 号码管理系统</title><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><center> <h1 id="号码管理系统-h">号码管理系统<h1></h1> </h1> </center><h4 id="注意如果需要插入操作-需要姓名和号码都不为空"> 注意如果需要插入操作,需要姓名和号码都不为空</h4>
如果当前页面的删除操作不满足输入条件,则跳转到专门的删除页面del.php, 代码如下:
<title>号码删除页面</title><meta http-equiv="Content-Type" content="text/html; charset=utf8">

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.


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

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version
SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
