Table of Contents
php读取excel,以及php打包文件夹为zip文件,
Home php教程 php手册 php读取excel,以及php打包文件夹为zip文件,

php读取excel,以及php打包文件夹为zip文件,

Jun 13, 2016 am 09:29 AM
excel php zip for Pack document folder read

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>
Copy after login
<?<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> 
         
Copy after login

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> 
Copy after login

 

代码下载:php-read-excel

http://files.cnblogs.com/zhidong123/php-read-excel.zip

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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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
1666
14
PHP Tutorial
1273
29
C# Tutorial
1252
24
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.

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

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 vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

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.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

See all articles