Phpexcel Getting Started

Source: Internet
Author: User
Recently contacted PHP. It's pretty easy to get started, after all, like C + +. But the introductory content is nothing more than some of the simplest foundations, the real difficulty lies in the many extensions of PHP.

These days the summary of the study of the Phpexcel, the reason that he is a summary of the study, is mainly to look at the online example as the main line of learning, combined with the official PDF document.

Here is a brief summary of the recent study.

1. Structure:

The construction of the Phpexcel has a very clear structure. This point is clearly defined and is essential for subsequent learning.

Workbook: Official document called workbook. Corresponds to the Phpexcel class in the Phpexcel.

Worksheet: worksheet, corresponding to the phpexcel in the sheet table, the specific class name I did not see, can be obtained through the getsheet.

Cell: Cell. The smallest unit that stores data.

The above three concepts are top-to-bottom containing relationships, workbooks contain worksheets, and worksheets contain cells.

There are, of course, two other important abstractions: reading and writing.

In Phpexcel, these two actions are abstracted into classes. This usage is very convenient. When we need to read a table, we use the Reader class object to load the file. When we need to write, we just have to open the object with the writer class.


2. Classes and methods.

There are a lot of classes in phpexcel. I haven't used a few recently. Simply list:

Phpexcel

Phpexcel_writer

Phpexcel_reader

Phpexcel_iofactory (This is the legendary factory design pattern, based on the method of invocation, constructs the object of writer class or reader class object.)

The Phpexcel method I am currently exposed to IS as follows:

Phpexcel_iofactory::load load the Excel file. The method of the reader class is called by default and returns an object of the Phpexcel class.

Phpexcel_iofactory::createwriter (Phpexcel, "Excel5") is used here to create a write class for an already existing object of the Phpexcel class, The following parameters are used to specify the suffix of Excel. There are also Excel007.

Phpexcel->getactivesheet Gets the table that is currently activated by default.

Phpexcel->getsheet (index) gets sheet based on index

Phpexcel->removesheetbyindex deletes the sheet table based on index.

Phpexcel->addsheet () Add a new sheet table

Phpexcel->addexternalsheet () Add an external table, and speaking of this method, mention another keyword, clone. This keyword can clone a copy of a table.

Sheet->getcellbycolumnandrow () Note in Phpexcel, the subscript of column is calculated from 0, and row subscript is calculated from 1.

Sheet->gethighestrow () Gets the maximum number of rows in the current table

Sheet->gethighestcolumn () Gets the maximum number of columns in the current table

Phpexcel_cell::columnindexfromstring () The current number of columns is obtained later, the column is in the form of letters, it is inconvenient to use, so there is this function, he can convert the letter column into a number.

Sheet->getcell (A1) its parameters are similar to this. You can also get the contents of a cell.

Sheet->setcellvaluebycolumnandrow (column, row, value) updates the value of the cell to the column and row reference values

Sheet->getrowiterator () Gets the iterator for the current row

Sheet->insertnewrowbefore ($currentRow, $rownum) inserts $rownum rows before the current line.

Phpexcel_cell->setvalue () sets a value for the current Cell.


Note that Phpexcel does not only output Excel files, but also output pdf,html files. I didn't know that. No discussion.

Here is a small example of my own design implementation. Phone book Management system. Support for lookup Insert deletion. Of course, there are no strict restrictions on some aspects. The implementation is only basic functionality. But I believe that can help students understand phpexcel.

The Excel table format is as follows:

Name Number remark

Phone.php is responsible for finding and inserting:

  Number Management System
 
  
 
    
  
   

Number Management System

Note If you need to insert an action, the name and number are not empty


If the deletion of the current page does not meet the input criteria, jump to the dedicated delete page del.php, the code is as follows:

<title>Number Delete Page</title>
 
  
 
 ";} $filename = "Phonelist.xls"; $phpexcel = Phpexcel_iofactory::load ($filename); $sheet = $phpexcel->getactivesheet (); $row _phone = $sheet->gethighestrow (), $column = $sheet->gethighestcolumn (); $column _phone = Phpexcel_cell::  Columnindexfromstring ($column);//This usage is on-line reference, or good use $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;        }//Find in the currently checked content, if the description in the array is checked. Delete if (In_array ($i, $chk)) {$sheet->removerow ($i, 1);        $clicked = TRUE;        echo "". $name. "--". $num. ";  echo "". " has been successfully deleted
"; Sleep (1); }}//If at least one delete operation is performed, refresh the page, save the modified Excel table if (TRUE = = $clicked) {$objWriter = Phpexcel_iofactory::createwriter ($phpexcel, " Excel5 "); $objWriter->save ($filename); $url = $SERVER ["Php_self"]; Header ("refresh:3;url=del.php");}? >

I hope the above example can inspire the students. Of course, Phpexcel and PHP itself, the content is simply too small. But for starters. This is not a good reference.
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.