国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > PHP > 正文

phpexcel入門

2020-03-22 19:45:40
字體:
來源:轉載
供稿:網友
  • 最近接觸了一下PHP.發現入門倒還蠻容易的,畢竟和C++很像.但是入門的內容無非是一些最簡單的基礎,真正的難點在于PHP的很多拓展.

    這幾天概要的學習了一下PHPExcel,之所以說自己是概要的學習,是因為主要是以看網上的例子作為學習的主線,結合官方pdf文檔.

    下面就簡要的對于近期的學習做一個小結.

    1. 結構:

    PHPExcel的構建具有很清晰的結構.明確了這個點,對于接下來的學習至關重要.

    工作簿: 官方文檔中叫workbook.對應PHPExcel中的PHPExcel類.

    工作表: worksheet , 對應PHPExcel中的sheet表格,具體類名我沒有看,可以通過getSheet獲取到.

    單元格: Cell .存儲數據的最小單元.

    以上的這三個概念是從上到下的包含關系,工作簿包含工作表,工作表包含單元格.

    當然還有另外兩個重要的抽象概念: 讀和寫.

    在PHPExcel中,讀寫這兩個動作被抽象成了類.這個用法還是很方便的.當我們需要讀一個表格的時候,我們使用reader類的對象加載文件.當我們需要寫的時候,只要再用writer類打開對象即可.

    2. 類與方法.

    PHPExcel里面的類有很多.我最近用到的沒有幾個.簡單列舉一下:

    PHPExcel

    PHPExcel_Writer

    PHPExcel_Reader

    PHPExcel_IOFactory (這就是傳說中的工廠設計模式,根據調用的方法,來構造出writer類的對象或者reader類的對象.)

    PHPExcel的方法我目前接觸到的如下:

    PHPExcel_IOFactory::load 加載excel文件.默認調用了reader類的方法,返回的是PHPExcel類的對象.

    PHPExcel_IOFactory::createWriter(PHPExcel, "Excel5") 這里用來創建一個已經存在的PHPExcel類的對象的寫類,后面的參數用來指定excel的后綴.常用的還有Excel007.

    PHPExcel->getActiveSheet 獲取當前默認激活著的表格.

    PHPExcel->getSheet(index) 根據index獲取sheet

    PHPExcel->removeSheetByIndex 根據index刪除sheet表格.

    PHPExcel->addSheet() 添加一個新的sheet表格

    PHPExcel->addExternalSheet() 添加一個外部表格,說到這個方法,就要提到另外一個關鍵字,clone.這個關鍵字可以克隆出一個表格的復制品.

    Sheet->getCellByColumnAndRow() 注意PHPExcel中,column的下標是從0計算的,而row的下標是從1開始計算的.

    Sheet->getHighestRow() 獲取當前表格的最大行數

    Sheet->getHighestColumn() 獲取當前表格的最大列數

    PHPExcel_Cell::columnIndexFromString() 當前的列數獲取到以后,這個列是以字母的形式存在的,用起來很不方便,所以就有了這個函數,他可以把字母的列轉成數字.

    Sheet->getCell(A1) 它的參數類似這個樣子.同樣可以獲取一個Cell的內容.

    Sheet->setCellValueByColumnAndRow(column, row, html' target='_blank'>value) 給column和row指代的cell更新值為value

    Sheet->getRowIterator() 獲取當前行的迭代器

    Sheet->insertNewRowBefore($currentRow, $rownum) 在當前行的前面插入$rownum個行.

    PHPExcel_Cell->setValue() 為當前的Cell設置一個值.

    需要留意的是,PHPExcel并不僅僅只能輸出Excel文件,還可以輸出PDF,html文件等.這個我沒有了解過.不做討論.

    下面是我自己設計實現的一個小例子.電話簿管理系統.支持查找插入刪除.當然并沒有嚴格的限制一些方面.實現的只是基本的功能.不過相信可以幫助同學們理解PHPExcel.

    excel表格式如下:

    名字 號碼 備注

    phone.php負責查找和插入:

    <html><head><title>  號碼管理系統</title><meta http-equiv="Content-Type" Content="text/html; charset=utf-8"/></head><body><center>  <h3>號碼管理系統<h3></center><h4>  注意如果需要插入操作,需要姓名和號碼都不為空</h4><form method="POST" action="<?php echo $_SERVER["PHP_SELF"]; ?>">  <br>姓名 : <input type="text" name="name"/>   <br>號碼 : <input type="text" name="num"/>   <br>備注 : <textarea name="addition" rows="5" cols="20">   </textarea>                             <br><input type="submit" name="search" value="查詢"/>        <input type="submit" name="insert" value="插入"/>       <input type="submit" name="delete" value="刪除" /><p><?php//設置全局的錯誤報告error_reporting(E_ALL);//將PHPExcel包含到PHP的include路徑中set_include_path(get_include_path().PATH_SEPARATOR."../Classes");//加載PHPExcel的文件require_once("PHPExcel.php");require_once("PHPExcel/IOFactory.php");$filename = "phonelist.xls";//加載Excel文件$phpexcel = PHPExcel_IOFactory::load($filename);$sheet = $phpexcel->getActiveSheet();//獲取行數和列數$data_phone = array(array());$row_phone = $sheet->getHighestRow();$column_str = $sheet->getHighestColumn();$column_phone = PHPExcel_Cell::columnIndexFromString($column_str);//建立一個表格用來打印電話號碼和用戶名function table(){    echo "<br><table border=1 cellpading=20>";    echo "<tr>";    echo "<td width=80>姓名</td>";    echo "<td width=150>號碼</td>";    echo "</tr>";}//用來打印有色字體function font($str, $color){    echo "<font color=".$color.">";    echo $str;    echo "</font>";    echo "</br>";}//判斷如果是查找if (!empty($_POST["search"])){    for ($i = 1; $i <= $row_phone; $i++)    {        for ($j = 0; $j < $column_phone; $j++)        {            //遍歷行列,將獲取到的內容添加到一個預先定義好的數組中            $data_phone[$i - 1][$j] = $sheet->getCellByColumnAndRow($j, $i)->getValue();        }    }//    var_dump($data_phone);    table();    for ($i = 1; $i < $row_phone; $i++)    {        //將數組內容整合成一個字符串,然后進行子串比對        $phone_item = implode($data_phone[$i]);        //名字匹配        if (!empty($_POST["name"]))        {            if (0 != substr_count($phone_item, $_POST["name"]))            {                echo "<tr>";                echo "<td>".$data_phone[$i][0]."</td>";                echo "<td>".$data_phone[$i][1]."</td>";                echo "</tr>";            }        }        //號碼匹配        else if (!empty($_POST["number"]))        {            if (0 != substr_count($phone_item, $_POST["number"]))            {                echo "<tr>";                echo "<td>".$data_phone[$i][0]."</td>";                echo "<td>".$data_phone[$i][1]."</td>";                echo "</tr>";            }        }    }    echo "</table>";}//如果是插入操作else if (!empty($_POST["insert"])){    if (!empty($_POST["name"]) && !empty($_POST["num"]))    {        $had = FALSE;        $name;        $num;         //確保將要插入的條目當前系統中不存在        for ($i = 2; $i <= $row_phone; $i++)        {            $name = $sheet->getCellByColumnAndRow(0, $i)->getValue();            $num = $sheet->getCellByColumnAndRow(1, $i)->getValue();            if (strcmp($_POST["name"], $name) == 0 &&                strcmp($_POST["num"], $num) == 0)            {                $had = TRUE;                break;            }        }        table();        //如果不存在,則插入        if (FALSE== $had)        {            font("插入成功", "green");            $sheet->insertNewRowBefore($row_phone + 1, 1);            $sheet->setCellValueByColumnAndRow(0, $row_phone + 1, $_POST["name"]);            $sheet->setCellValueByColumnAndRow(1, $row_phone + 1, $_POST["num"]);            //此處糾結了好久,原來就只是用writer把當前對象加載一下,就可以保存了.            $phpwriter = PHPExcel_IOFactory::createWriter($phpexcel, "Excel5");            $phpwriter->save($filename);            echo "<tr>";            echo "<td>".$_POST["name"]."</td>";            echo "<td>".$_POST["num"]."</td>";            echo "</tr>";        }        //如果存在,則不執行任何動作        else         {            font("插入失敗,條目已存在", "red");            echo "<tr>";            echo "<td>".$name."</td>";            echo "<td>".$num."</td>";            echo "</tr>";        }        echo "</table>";    }}//刪除操作else if (!empty($_POST["delete"])){    $had = FALSE;    if (!empty($_POST["name"]) && !empty($_POST["num"]))    {        for ($i = 2; $i <= $row_phone; $i++)        {            $name = $sheet->getCellByColumnAndRow(0, $i)->getValue();            $num = $sheet->getCellByColumnAndRow(1, $i)->getValue();            if (strcmp($name, $_POST["name"]) == 0 &&                 strcmp($num, $_POST["num"]) ==0)            {                $sheet->removeRow($i, 1);                $objWriter = PHPExcel_IOFactory::createWriter($phpexcel, "Excel5");                $objWriter->save($filename);                $had = TRUE;            }        }    }    //加入用戶名和號碼有一個是空的,則執行這個分支    if ($had == FALSE)    {        font("請輸入正確的姓名和號碼", "red");        font("如果不確定,可以通過查找先定位", "red");        font("即將跳轉到刪除頁面,請稍后...", "green");        //延遲3秒顯示上面的信息,然后跳轉到刪除頁面        header("Refresh:3;url=del.php");    }    else    {        font("用戶刪除成功", "green");    }}?></form></body></html>

    如果當前頁面的刪除操作不滿足輸入條件,則跳轉到專門的刪除頁面del.php, 代碼如下:

    <html><head><title>號碼刪除頁面</title><meta http-equiv="Content-Type" content="text/html; charset=utf8"/></head><body><form method="POST" action="<?php $_SERVER["PHP_SELF"] ?>" ><?phperror_reporting(E_ALL);set_include_path(get_include_path().PATH_SEPARATOR."../Classes");require_once("PHPExcel.php");require_once("PHPExcel/IOFactory.php");$filename = "phonelist.xls";$phpexcel = PHPExcel_IOFactory::load($filename);$sheet = $phpexcel->getActiveSheet();$row_phone = $sheet->getHighestRow();$column = $sheet->getHighestColumn();$column_phone = PHPExcel_Cell::columnIndexFromString($column);for ($i = 2; $i <= $row_phone; $i++){    $name = $sheet->getCellByColumnAndRow(0, $i)->getValue();    $num = $sheet->getCellByColumnAndRow(1, $i)->getValue();    //將每一條電話信息用checkbox的方式列出來    if (!empty($name) || !empty($num))    {        echo '<input type="checkbox" name="chk[]" value='.$i."/>".$name." ".$num."<br>";    }}?><input type="submit" value="刪除" name="delete"/></form><?phperror_reporting(E_ALL);set_include_path(get_include_path().PATH_SEPARATOR."../Classes");require_once("PHPExcel.php");require_once("PHPExcel/IOFactory.php");function font($str, $color){    echo "<font color=".$color.">";    echo $str;    echo "</font>";    echo "</br>";}$filename = "phonelist.xls";$phpexcel = PHPExcel_IOFactory::load($filename);$sheet = $phpexcel->getActiveSheet();$row_phone = $sheet->getHighestRow();$column = $sheet->getHighestColumn();$column_phone = PHPExcel_Cell::columnIndexFromString($column);//這個用法是網上借鑒的,還是蠻好用的$chk = $_POST["chk"];for ($i = $row_phone; $i >= 2; $i--){    $name = $sheet->getCellByColumnAndRow(0, $i)->getValue();    $num = $sheet->getCellByColumnAndRow(1, $i)->getValue();    if (empty($name) && empty($num))    {        continue;    }    //在當前勾選的內容中查找,如果在數組中說明勾選了.則刪除    if (in_array($i, $chk))    {        $sheet->removeRow($i, 1);        $clicked = TRUE;        echo "<font color=green>".$name." -- ".$num."</font>";        echo "<font color=red>"."  已經成功刪除</font><br>";        sleep(1);    }}//如果執行了至少一次刪除操作,則刷新頁面,保存修改后excel表格if (TRUE == $clicked){    $objWriter = PHPExcel_IOFactory::createWriter($phpexcel, "Excel5");    $objWriter->save($filename);    $url=$SERVER["PHP_SELF"];    header("refresh:3;url=del.php");}?></body></html>

    希望以上的這個例子能夠對同學們有所啟發.當然PHPExcel和PHP本身而言,這里的內容簡直太少.但是對于入門而言.這也不是一個中規中矩的參考.

    PHP編程

    鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

  • 發表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發表
    主站蜘蛛池模板: 盘锦市| 海林市| 和林格尔县| 宁海县| 南通市| 阿合奇县| 什邡市| 岳阳市| 拜泉县| 吉隆县| 策勒县| 嘉义县| 大竹县| 沙坪坝区| 青铜峡市| 龙泉市| 虹口区| 宁武县| 阿图什市| 盐城市| 乐业县| 错那县| 湖州市| 临沧市| 邳州市| 兴山县| 兰坪| 喀喇沁旗| 阳西县| 柘荣县| 武汉市| 南澳县| 吴江市| 岳池县| 扶绥县| 怀安县| 万州区| 焦作市| 建水县| 荔浦县| 乡城县|