search
Homephp教程php手册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>文件夹文件名字为'.'和&lsquo;..&rsquo;,不要对他们进行操作</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

Statement
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
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

See all articles

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

Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Safe Exam Browser

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.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),