php讀取excel,以及php打包檔案夾為zip檔案,_PHP教程

來源:互聯網
上載者:User

php讀取excel,以及php打包檔案夾為zip檔案,


1.把檔案下載到本地,放在在Apache環境下
2.d.xlsx是某遊戲的伺服器名和玩家列表,本程式只適合此種xlsx檔案結構,其他結構請修改index.php源碼
3.訪問zip.php的功能是把產生的files檔案夾打包成files.zip
4.訪問index.php即可產生files檔案夾,裡面0.js---n.js 分別存放各個伺服器人名,server_name_list.js存放伺服器列表。
5.Classes 存放的是php讀取excel的功能模組,具體任務邏輯都在index.php

A.PHP讀取excel支援excel2007

demo邏輯代碼:其中的(arrayRecursive,JSON方法是json資料處理功能,可相容漢字)
主要藉助了:PHPExcel外掛程式,附件中有Classes檔案夾,官網:http://www.codeplex.com/PHPExcel
index.php
php /** Error reporting */error_reporting(0);header("Content-type: text/html; charset=utf-8"); ?>標題    php     /**************************************************************     *     *  使用特定function對數組中所有元素做處理     *  @param  string  &$array     要處理的字串     *  @param  string  $function   要執行的函數     *  @return boolean $apply_to_keys_also     是否也應用到key上     *  @access public     *     *************************************************************/    function arrayRecursive(&$array, $function, $apply_to_keys_also = false)    {        static $recursive_counter = 0;        if (++$recursive_counter > 1000) {            die('possible deep recursion attack');        }        foreach ($array as $key => $value) {            if (is_array($value)) {                arrayRecursive($array[$key], $function, $apply_to_keys_also);            } else {                $array[$key] = $function($value);            }                 if ($apply_to_keys_also && is_string($key)) {                $new_key = $function($key);                if ($new_key != $key) {                    $array[$new_key] = $array[$key];                    unset($array[$key]);                }            }        }        $recursive_counter--;    }         /**************************************************************     *     *  將數群組轉換為JSON字串(相容中文)     *  @param  array   $array      要轉換的數組     *  @return string      轉換得到的json字串     *  @access public     *     *************************************************************/    function JSON($array) {        arrayRecursive($array, 'urlencode', true);        $json = json_encode($array);        return urldecode($json);    }    require_once 'Classes\PHPExcel.php';      require_once 'Classes\PHPExcel\IOFactory.php';      require_once 'Classes\PHPExcel\Reader\Excel2007.php';      $uploadfile='d.xlsx';            $objReader = PHPExcel_IOFactory::createReader('Excel2007');/*Excel5 for 2003 excel2007 for 2007*/     $objPHPExcel = PHPExcel_IOFactory::load($uploadfile);    $sheet = $objPHPExcel->getSheet(0);      $highestRow = $sheet->getHighestRow(); // 取得總行數      $highestColumn = $sheet->getHighestColumn(); // 取得總列數      /*方法【推薦】*/      $objWorksheet = $objPHPExcel->getActiveSheet();              $highestRow = $objWorksheet->getHighestRow();   // 取得總行數           $highestColumn = $objWorksheet->getHighestColumn();              $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);//總列數      $list = array();    for ($row = 1;$row <= $highestRow;$row++)         {          $strs=array();          //注意highestColumnIndex的列數索引從0開始          for ($col = 0;$col < $highestColumnIndex;$col++)            {              $strs[$col] =$objWorksheet->getCellByColumnAndRow($col, $row)->getValue();          }          array_push($list, $strs);      }    //讀取完畢 $list    //處理資料,產生新的結構    $n = 0;    $ser = array();    for($i = 0 ; $i < count($list); $i++ ){        $ser[$n][0] = $list[$i][0];         if(!is_array(@$ser[$n][1])){            $ser[$n][1] = array();        }         array_push($ser[$n][1], $list[$i][1]);        if($i != count($list) -1){          if($list[$i][0] != $list[$i+1][0]){                $n++;            }         }     }    /*輸出檔案*/    $sname = array();    $f = 'files/';//存放目錄    if (! file_exists ( $f )) {        mkdir ( $f );    }    for($j = 0;$j < count($ser); $j++){        $file = $f.$j.'.js';        echo $file."
"; $fp=fopen("$file", "w+"); //開啟檔案指標,建立檔案 if ( !is_writable($file) ){ die("檔案:" .$file. "不可寫,請檢查!"); } if (is_writable($file) == false) { die('我是雞毛,我不能'); } $data = $ser[$j][1]; array_push($sname, $ser[$j][0]); file_put_contents ($file, JSON($data)); fclose($fp); //關閉指標 } $file = $f.'server_name_list.js'; echo $file."
";; $fp=fopen("$file", "w+"); //開啟檔案指標,建立檔案 if ( !is_writable($file) ){ die("檔案:" .$file. "不可寫,請檢查!"); } if (is_writable($file) == false) { die('我是雞毛,我不能'); } file_put_contents ($file, JSON($sname)); echo "產生完畢!"; echo '打包組建檔案' ?>

B.PHP打包檔案夾為zip檔案

zip.php

php /** Error reporting */error_reporting(0);header("Content-type: text/html; charset=utf-8"); ?>標題php    function addFileToZip($path,$zip){    $handler=opendir($path); //開啟當前檔案夾由$path指定。    while(($filename=readdir($handler))!==false){        if($filename != "." && $filename != ".."){//檔案夾檔案名稱字為'.'和‘..’,不要對他們進行操作            if(is_dir($path."/".$filename)){// 如果讀取的某個對象是檔案夾,則遞迴                addFileToZip($path."/".$filename, $zip);            }else{ //將檔案加入zip對象                $zip->addFile($path."/".$filename);            }        }    }    @closedir($path);}$zip=new ZipArchive();if($zip->open('files.zip', ZipArchive::OVERWRITE)=== TRUE){    addFileToZip('files', $zip); //調用方法,對要打包的根目錄進行操作,並將ZipArchive的對象傳遞給方法    $zip->close(); //關閉處理的zip檔案}echo '打包完畢!'."
";echo "下載files.zip"?>

代碼下載:php-read-excel

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




http://www.bkjia.com/PHPjc/834763.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/834763.htmlTechArticlephp讀取excel,以及php打包檔案夾為zip檔案, 1.把檔案下載到本地,放在在Apache環境下 2.d.xlsx是某遊戲的伺服器名和玩家列表,本程式只適合...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.