Jadual Kandungan
php读取excel,以及php打包文件夹为zip文件,
Rumah php教程 php手册 php读取excel,以及php打包文件夹为zip文件,

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

Jun 13, 2016 am 09:29 AM
excel php zip untuk Pek dokumen folder membaca

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>
Salin selepas log masuk
<?<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> 
         
Salin selepas log masuk

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> 
Salin selepas log masuk

 

代码下载:php-read-excel

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

Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn

Alat AI Hot

Undresser.AI Undress

Undresser.AI Undress

Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover

AI Clothes Remover

Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Undress AI Tool

Undress AI Tool

Gambar buka pakaian secara percuma

Clothoff.io

Clothoff.io

Penyingkiran pakaian AI

AI Hentai Generator

AI Hentai Generator

Menjana ai hentai secara percuma.

Artikel Panas

R.E.P.O. Kristal tenaga dijelaskan dan apa yang mereka lakukan (kristal kuning)
2 minggu yang lalu By 尊渡假赌尊渡假赌尊渡假赌
Repo: Cara menghidupkan semula rakan sepasukan
1 bulan yang lalu By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: Cara mendapatkan biji gergasi
1 bulan yang lalu By 尊渡假赌尊渡假赌尊渡假赌

Alat panas

Notepad++7.3.1

Notepad++7.3.1

Editor kod yang mudah digunakan dan percuma

SublimeText3 versi Cina

SublimeText3 versi Cina

Versi Cina, sangat mudah digunakan

Hantar Studio 13.0.1

Hantar Studio 13.0.1

Persekitaran pembangunan bersepadu PHP yang berkuasa

Dreamweaver CS6

Dreamweaver CS6

Alat pembangunan web visual

SublimeText3 versi Mac

SublimeText3 versi Mac

Perisian penyuntingan kod peringkat Tuhan (SublimeText3)

Konfigurasi Projek CakePHP Konfigurasi Projek CakePHP Sep 10, 2024 pm 05:25 PM

Dalam bab ini, kita akan memahami Pembolehubah Persekitaran, Konfigurasi Umum, Konfigurasi Pangkalan Data dan Konfigurasi E-mel dalam CakePHP.

Panduan Pemasangan dan Naik Taraf PHP 8.4 untuk Ubuntu dan Debian Panduan Pemasangan dan Naik Taraf PHP 8.4 untuk Ubuntu dan Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 membawa beberapa ciri baharu, peningkatan keselamatan dan peningkatan prestasi dengan jumlah penamatan dan penyingkiran ciri yang sihat. Panduan ini menerangkan cara memasang PHP 8.4 atau naik taraf kepada PHP 8.4 pada Ubuntu, Debian, atau terbitan mereka

Tarikh dan Masa CakePHP Tarikh dan Masa CakePHP Sep 10, 2024 pm 05:27 PM

Untuk bekerja dengan tarikh dan masa dalam cakephp4, kami akan menggunakan kelas FrozenTime yang tersedia.

Muat naik Fail CakePHP Muat naik Fail CakePHP Sep 10, 2024 pm 05:27 PM

Untuk mengusahakan muat naik fail, kami akan menggunakan pembantu borang. Di sini, adalah contoh untuk muat naik fail.

Penghalaan CakePHP Penghalaan CakePHP Sep 10, 2024 pm 05:25 PM

Dalam bab ini, kita akan mempelajari topik berikut yang berkaitan dengan penghalaan ?

Bincangkan CakePHP Bincangkan CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP ialah rangka kerja sumber terbuka untuk PHP. Ia bertujuan untuk menjadikan pembangunan, penggunaan dan penyelenggaraan aplikasi lebih mudah. CakePHP adalah berdasarkan seni bina seperti MVC yang berkuasa dan mudah difahami. Model, Pandangan dan Pengawal gu

Cara Menyediakan Kod Visual Studio (Kod VS) untuk Pembangunan PHP Cara Menyediakan Kod Visual Studio (Kod VS) untuk Pembangunan PHP Dec 20, 2024 am 11:31 AM

Kod Visual Studio, juga dikenali sebagai Kod VS, ialah editor kod sumber percuma — atau persekitaran pembangunan bersepadu (IDE) — tersedia untuk semua sistem pengendalian utama. Dengan koleksi sambungan yang besar untuk banyak bahasa pengaturcaraan, Kod VS boleh menjadi c

Pengesah Mencipta CakePHP Pengesah Mencipta CakePHP Sep 10, 2024 pm 05:26 PM

Pengesah boleh dibuat dengan menambah dua baris berikut dalam pengawal.

See all articles