Table of Contents
号码管理系统

注意如果需要插入操作,需要姓名和号码都不为空

phpexcel入门

Jun 23, 2016 pm 01:34 PM

最近接触了一下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>
Copy after login
">
姓名 :
号码 :
备注 :
     

getActiveSheet();//获取行数和列数$data_phone = array(array());$row_phone = $sheet->getHighestRow();$column_str = $sheet->getHighestColumn();$column_phone = PHPExcel_Cell::columnIndexFromString($column_str);//建立一个表格用来打印电话号码和用户名function table(){ echo "

"; echo ""; echo ""; echo ""; echo "";}//用来打印有色字体function font($str, $color){ echo ""; echo $str; echo ""; echo "";}//判断如果是查找if (!empty($_POST["search"])){ for ($i = 1; $i getCellByColumnAndRow($j, $i)->getValue(); } }// var_dump($data_phone); table(); for ($i = 1; $i "; echo ""; echo ""; echo ""; } } //号码匹配 else if (!empty($_POST["number"])) { if (0 != substr_count($phone_item, $_POST["number"])) { echo ""; echo ""; echo ""; echo ""; } } } echo "
姓名号码
".$data_phone[$i][0]."".$data_phone[$i][1]."
".$data_phone[$i][0]."".$data_phone[$i][1]."
";}//如果是插入操作else if (!empty($_POST["insert"])){ if (!empty($_POST["name"]) && !empty($_POST["num"])) { $had = FALSE; $name; $num; //确保将要插入的条目当前系统中不存在 for ($i = 2; $i getCellByColumnAndRow(0, $i)->getValue(); $num = $sheet->getCellByColumnAndRow(1, $i)->getValue(); if (strcmp($_POST["name"], $name) == 0 && strcmp($_POST["num"], $num) == 0) { $had = TRUE; break; } } table(); //如果不存在,则插入 if (FALSE== $had) { font("插入成功", "green"); $sheet->insertNewRowBefore($row_phone + 1, 1); $sheet->setCellValueByColumnAndRow(0, $row_phone + 1, $_POST["name"]); $sheet->setCellValueByColumnAndRow(1, $row_phone + 1, $_POST["num"]); //此处纠结了好久,原来就只是用writer把当前对象加载一下,就可以保存了. $phpwriter = PHPExcel_IOFactory::createWriter($phpexcel, "Excel5"); $phpwriter->save($filename); echo ""; echo "".$_POST["name"].""; echo "".$_POST["num"].""; echo ""; } //如果存在,则不执行任何动作 else { font("插入失败,条目已存在", "red"); echo ""; echo "".$name.""; echo "".$num.""; echo ""; } echo ""; }}//删除操作else if (!empty($_POST["delete"])){ $had = FALSE; if (!empty($_POST["name"]) && !empty($_POST["num"])) { for ($i = 2; $i getCellByColumnAndRow(0, $i)->getValue(); $num = $sheet->getCellByColumnAndRow(1, $i)->getValue(); if (strcmp($name, $_POST["name"]) == 0 && strcmp($num, $_POST["num"]) ==0) { $sheet->removeRow($i, 1); $objWriter = PHPExcel_IOFactory::createWriter($phpexcel, "Excel5"); $objWriter->save($filename); $had = TRUE; } } } //加入用户名和号码有一个是空的,则执行这个分支 if ($had == FALSE) { font("请输入正确的姓名和号码", "red"); font("如果不确定,可以通过查找先定位", "red"); font("即将跳转到删除页面,请稍后...", "green"); //延迟3秒显示上面的信息,然后跳转到删除页面 header("Refresh:3;url=del.php"); } else { font("用户删除成功", "green"); }}?>

如果当前页面的删除操作不满足输入条件,则跳转到专门的删除页面del.php, 代码如下:

<title>号码删除页面</title><meta http-equiv="Content-Type" content="text/html; charset=utf8">
Copy after login
" >getActiveSheet();$row_phone = $sheet->getHighestRow();$column = $sheet->getHighestColumn();$column_phone = PHPExcel_Cell::columnIndexFromString($column);for ($i = 2; $i getCellByColumnAndRow(0, $i)->getValue(); $num = $sheet->getCellByColumnAndRow(1, $i)->getValue(); //将每一条电话信息用checkbox的方式列出来 if (!empty($name) || !empty($num)) { echo '
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 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
1664
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
Explain different error types in PHP (Notice, Warning, Fatal Error, Parse Error). Explain different error types in PHP (Notice, Warning, Fatal Error, Parse Error). Apr 08, 2025 am 12:03 AM

There are four main error types in PHP: 1.Notice: the slightest, will not interrupt the program, such as accessing undefined variables; 2. Warning: serious than Notice, will not terminate the program, such as containing no files; 3. FatalError: the most serious, will terminate the program, such as calling no function; 4. ParseError: syntax error, will prevent the program from being executed, such as forgetting to add the end tag.

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.

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

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

What are HTTP request methods (GET, POST, PUT, DELETE, etc.) and when should each be used? What are HTTP request methods (GET, POST, PUT, DELETE, etc.) and when should each be used? Apr 09, 2025 am 12:09 AM

HTTP request methods include GET, POST, PUT and DELETE, which are used to obtain, submit, update and delete resources respectively. 1. The GET method is used to obtain resources and is suitable for read operations. 2. The POST method is used to submit data and is often used to create new resources. 3. The PUT method is used to update resources and is suitable for complete updates. 4. The DELETE method is used to delete resources and is suitable for deletion operations.

Explain the difference between self::, parent::, and static:: in PHP OOP. Explain the difference between self::, parent::, and static:: in PHP OOP. Apr 09, 2025 am 12:04 AM

In PHPOOP, self:: refers to the current class, parent:: refers to the parent class, static:: is used for late static binding. 1.self:: is used for static method and constant calls, but does not support late static binding. 2.parent:: is used for subclasses to call parent class methods, and private methods cannot be accessed. 3.static:: supports late static binding, suitable for inheritance and polymorphism, but may affect the readability of the code.

How does PHP handle file uploads securely? How does PHP handle file uploads securely? Apr 10, 2025 am 09:37 AM

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

See all articles