php读取excel,以及php打包文件夹为zip文件,
php读取excel,以及php打包文件夹为zip文件,
<span class="pln"><span>1.把文件下载到本地,放在在Apache环境下</span><br /><span>2.d.xlsx是某游戏的服务器名和玩家列表,本程序只适合此种xlsx文件结构,其他结构请修改index.php源码</span><br /><span>3.访问zip.php的功能是把生成的files文件夹打包成files.zip</span><br /><span>4.访问index.php即可生成files文件夹,里面0.js---n.js 分别存放各个服务器人名,server_name_list.js存放服务器列表。</span><br /><span>5.Classes 存放的是php读取excel的功能模块,具体任务逻辑都在index.php<br /><br />A.PHP读取excel支持excel2007<br /><br />demo逻辑代码:其中的(arrayRecursive,JSON方法是json数据处理功能,可兼容汉字)<br /></span>主要借助了:PHPExcel插件,附件中有Classes文件夹,官网:http://www.codeplex.com/PHPExcel<br />index.php<br /></span>
<?<span>php </span><span>/*</span><span>* Error reporting </span><span>*/</span> <span>error_reporting</span>(0<span>); </span><span>header</span>("Content-type: text/html; charset=utf-8"<span>); </span>?> <!doctype html> <html> <head> <meta charset="utf-8"> <title>标题</title> </head> <body> <?<span>php </span><span>/*</span><span>************************************************************* * * 使用特定function对数组中所有元素做处理 * @param string &$array 要处理的字符串 * @param string $function 要执行的函数 * @return boolean $apply_to_keys_also 是否也应用到key上 * @access public * ************************************************************</span><span>*/</span> <span>function</span> arrayRecursive(&<span>$array</span>, <span>$function</span>, <span>$apply_to_keys_also</span> = <span>false</span><span>) { </span><span>static</span> <span>$recursive_counter</span> = 0<span>; </span><span>if</span> (++<span>$recursive_counter</span> > 1000<span>) { </span><span>die</span>('possible deep recursion attack'<span>); } </span><span>foreach</span> (<span>$array</span> <span>as</span> <span>$key</span> => <span>$value</span><span>) { </span><span>if</span> (<span>is_array</span>(<span>$value</span><span>)) { arrayRecursive(</span><span>$array</span>[<span>$key</span>], <span>$function</span>, <span>$apply_to_keys_also</span><span>); } </span><span>else</span><span> { </span><span>$array</span>[<span>$key</span>] = <span>$function</span>(<span>$value</span><span>); } </span><span>if</span> (<span>$apply_to_keys_also</span> && <span>is_string</span>(<span>$key</span><span>)) { </span><span>$new_key</span> = <span>$function</span>(<span>$key</span><span>); </span><span>if</span> (<span>$new_key</span> != <span>$key</span><span>) { </span><span>$array</span>[<span>$new_key</span>] = <span>$array</span>[<span>$key</span><span>]; </span><span>unset</span>(<span>$array</span>[<span>$key</span><span>]); } } } </span><span>$recursive_counter</span>--<span>; } </span><span>/*</span><span>************************************************************* * * 将数组转换为JSON字符串(兼容中文) * @param array $array 要转换的数组 * @return string 转换得到的json字符串 * @access public * ************************************************************</span><span>*/</span> <span>function</span> JSON(<span>$array</span><span>) { arrayRecursive(</span><span>$array</span>, 'urlencode', <span>true</span><span>); </span><span>$json</span> = json_encode(<span>$array</span><span>); </span><span>return</span> <span>urldecode</span>(<span>$json</span><span>); } </span><span>require_once</span> 'Classes\PHPExcel.php'<span>; </span><span>require_once</span> 'Classes\PHPExcel\IOFactory.php'<span>; </span><span>require_once</span> 'Classes\PHPExcel\Reader\Excel2007.php'<span>; </span><span>$uploadfile</span>='d.xlsx'<span>; </span><span>$objReader</span> = PHPExcel_IOFactory::createReader('Excel2007');<span>/*</span><span>Excel5 for 2003 excel2007 for 2007</span><span>*/</span> <span>$objPHPExcel</span> = PHPExcel_IOFactory::load(<span>$uploadfile</span><span>); </span><span>$sheet</span> = <span>$objPHPExcel</span>->getSheet(0<span>); </span><span>$highestRow</span> = <span>$sheet</span>->getHighestRow(); <span>//</span><span> 取得总行数 </span> <span>$highestColumn</span> = <span>$sheet</span>->getHighestColumn(); <span>//</span><span> 取得总列数 </span> <span>/*</span><span>方法【推荐】</span><span>*/</span> <span>$objWorksheet</span> = <span>$objPHPExcel</span>-><span>getActiveSheet(); </span><span>$highestRow</span> = <span>$objWorksheet</span>->getHighestRow(); <span>//</span><span> 取得总行数 </span> <span>$highestColumn</span> = <span>$objWorksheet</span>-><span>getHighestColumn(); </span><span>$highestColumnIndex</span> = PHPExcel_Cell::columnIndexFromString(<span>$highestColumn</span>);<span>//</span><span>总列数 </span> <span>$list</span> = <span>array</span><span>(); </span><span>for</span> (<span>$row</span> = 1;<span>$row</span> <= <span>$highestRow</span>;<span>$row</span>++<span>) { </span><span>$strs</span>=<span>array</span><span>(); </span><span>//</span><span>注意highestColumnIndex的列数索引从0开始 </span> <span>for</span> (<span>$col</span> = 0;<span>$col</span> < <span>$highestColumnIndex</span>;<span>$col</span>++<span>) { </span><span>$strs</span>[<span>$col</span>] =<span>$objWorksheet</span>->getCellByColumnAndRow(<span>$col</span>, <span>$row</span>)-><span>getValue(); } </span><span>array_push</span>(<span>$list</span>, <span>$strs</span><span>); } </span><span>//</span><span>读取完毕 $list //处理数据,生成新的结构</span> <span>$n</span> = 0<span>; </span><span>$ser</span> = <span>array</span><span>(); </span><span>for</span>(<span>$i</span> = 0 ; <span>$i</span> < <span>count</span>(<span>$list</span>); <span>$i</span>++<span> ){ </span><span>$ser</span>[<span>$n</span>][0] = <span>$list</span>[<span>$i</span>][0<span>]; </span><span>if</span>(!<span>is_array</span>(@<span>$ser</span>[<span>$n</span>][1<span>])){ </span><span>$ser</span>[<span>$n</span>][1] = <span>array</span><span>(); } </span><span>array_push</span>(<span>$ser</span>[<span>$n</span>][1], <span>$list</span>[<span>$i</span>][1<span>]); </span><span>if</span>(<span>$i</span> != <span>count</span>(<span>$list</span>) -1<span>){ </span><span>if</span>(<span>$list</span>[<span>$i</span>][0] != <span>$list</span>[<span>$i</span>+1][0<span>]){ </span><span>$n</span>++<span>; } } } </span><span>/*</span><span>输出文件</span><span>*/</span> <span>$sname</span> = <span>array</span><span>(); </span><span>$f</span> = 'files/';<span>//</span><span>存放目录</span> <span>if</span> (! <span>file_exists</span> ( <span>$f</span><span> )) { </span><span>mkdir</span> ( <span>$f</span><span> ); } </span><span>for</span>(<span>$j</span> = 0;<span>$j</span> < <span>count</span>(<span>$ser</span>); <span>$j</span>++<span>){ </span><span>$file</span> = <span>$f</span>.<span>$j</span>.'.js'<span>; </span><span>echo</span> <span>$file</span>."<br />"<span>; </span><span>$fp</span>=<span>fopen</span>("<span>$file</span>", "w+"); <span>//</span><span>打开文件指针,创建文件</span> <span>if</span> ( !<span>is_writable</span>(<span>$file</span><span>) ){ </span><span>die</span>("文件:" .<span>$file</span>. "不可写,请检查!"<span>); } </span><span>if</span> (<span>is_writable</span>(<span>$file</span>) == <span>false</span><span>) { </span><span>die</span>('我是鸡毛,我不能'<span>); } </span><span>$data</span> = <span>$ser</span>[<span>$j</span>][1<span>]; </span><span>array_push</span>(<span>$sname</span>, <span>$ser</span>[<span>$j</span>][0<span>]); </span><span>file_put_contents</span> (<span>$file</span>, JSON(<span>$data</span><span>)); </span><span>fclose</span>(<span>$fp</span>); <span>//</span><span>关闭指针</span> <span> } </span><span>$file</span> = <span>$f</span>.'server_name_list.js'<span>; </span><span>echo</span> <span>$file</span>."<br />"<span>;; </span><span>$fp</span>=<span>fopen</span>("<span>$file</span>", "w+"); <span>//</span><span>打开文件指针,创建文件</span> <span>if</span> ( !<span>is_writable</span>(<span>$file</span><span>) ){ </span><span>die</span>("文件:" .<span>$file</span>. "不可写,请检查!"<span>); } </span><span>if</span> (<span>is_writable</span>(<span>$file</span>) == <span>false</span><span>) { </span><span>die</span>('我是鸡毛,我不能'<span>); } </span><span>file_put_contents</span> (<span>$file</span>, JSON(<span>$sname</span><span>)); </span><span>echo</span> "生成完毕!"<span>; </span><span>echo</span> '<a href="zip.php">打包生成文件</a>' ?> </body> </html>
B.PHP打包文件夹为zip文件
zip.php
<?<span>php </span><span>/*</span><span>* Error reporting </span><span>*/</span> <span>error_reporting</span>(0<span>); </span><span>header</span>("Content-type: text/html; charset=utf-8"<span>); </span>?> <!doctype html> <html> <head> <meta charset="utf-8"> <title>标题</title> </head> <body> <?<span>php </span><span>function</span> addFileToZip(<span>$path</span>,<span>$zip</span><span>){ </span><span>$handler</span>=<span>opendir</span>(<span>$path</span>); <span>//</span><span>打开当前文件夹由$path指定。</span> <span>while</span>((<span>$filename</span>=<span>readdir</span>(<span>$handler</span>))!==<span>false</span><span>){ </span><span>if</span>(<span>$filename</span> != "." && <span>$filename</span> != ".."){<span>//</span><span>文件夹文件名字为'.'和‘..’,不要对他们进行操作</span> <span>if</span>(<span>is_dir</span>(<span>$path</span>."/".<span>$filename</span>)){<span>//</span><span> 如果读取的某个对象是文件夹,则递归</span> addFileToZip(<span>$path</span>."/".<span>$filename</span>, <span>$zip</span><span>); }</span><span>else</span>{ <span>//</span><span>将文件加入zip对象</span> <span>$zip</span>->addFile(<span>$path</span>."/".<span>$filename</span><span>); } } } @</span><span>closedir</span>(<span>$path</span><span>); } </span><span>$zip</span>=<span>new</span><span> ZipArchive(); </span><span>if</span>(<span>$zip</span>->open('files.zip', ZipArchive::OVERWRITE)=== <span>TRUE</span><span>){ addFileToZip(</span>'files', <span>$zip</span>); <span>//</span><span>调用方法,对要打包的根目录进行操作,并将ZipArchive的对象传递给方法</span> <span>$zip</span>->close(); <span>//</span><span>关闭处理的zip文件</span> <span>} </span><span>echo</span> '打包完毕!'."<br />"<span>; </span><span>echo</span> "<a href='files.zip'>下载files.zip</a>" ?> </body> </html>
代码下载:php-read-excel
http://files.cnblogs.com/zhidong123/php-read-excel.zip

Heiße KI -Werkzeuge

Undresser.AI Undress
KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover
Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool
Ausziehbilder kostenlos

Clothoff.io
KI-Kleiderentferner

AI Hentai Generator
Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

Heiße Werkzeuge

Notepad++7.3.1
Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version
Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1
Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6
Visuelle Webentwicklungstools

SublimeText3 Mac-Version
Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

Heiße Themen

In diesem Kapitel werden wir die Umgebungsvariablen, die allgemeine Konfiguration, die Datenbankkonfiguration und die E-Mail-Konfiguration in CakePHP verstehen.

PHP 8.4 bringt mehrere neue Funktionen, Sicherheitsverbesserungen und Leistungsverbesserungen mit einer beträchtlichen Menge an veralteten und entfernten Funktionen. In dieser Anleitung wird erklärt, wie Sie PHP 8.4 installieren oder auf PHP 8.4 auf Ubuntu, Debian oder deren Derivaten aktualisieren. Obwohl es möglich ist, PHP aus dem Quellcode zu kompilieren, ist die Installation aus einem APT-Repository wie unten erläutert oft schneller und sicherer, da diese Repositorys in Zukunft die neuesten Fehlerbehebungen und Sicherheitsupdates bereitstellen.

Um in cakephp4 mit Datum und Uhrzeit zu arbeiten, verwenden wir die verfügbare FrozenTime-Klasse.

Um am Datei-Upload zu arbeiten, verwenden wir den Formular-Helfer. Hier ist ein Beispiel für den Datei-Upload.

In diesem Kapitel lernen wir die folgenden Themen im Zusammenhang mit dem Routing kennen.

CakePHP ist ein Open-Source-Framework für PHP. Es soll die Entwicklung, Bereitstellung und Wartung von Anwendungen erheblich vereinfachen. CakePHP basiert auf einer MVC-ähnlichen Architektur, die sowohl leistungsstark als auch leicht zu verstehen ist. Modelle, Ansichten und Controller gu

Visual Studio Code, auch bekannt als VS Code, ist ein kostenloser Quellcode-Editor – oder eine integrierte Entwicklungsumgebung (IDE) –, die für alle gängigen Betriebssysteme verfügbar ist. Mit einer großen Sammlung von Erweiterungen für viele Programmiersprachen kann VS Code c

Der Validator kann durch Hinzufügen der folgenden zwei Zeilen im Controller erstellt werden.
