A method for writing and reading Excel files using Phpexcel

Source: Internet
Author: User
This article mainly introduces the use of Phpexcel to implement the Excel file writing and reading knowledge. Has a good reference value. Let's take a look at the little series.

As an original Java party, used PHP only to know that the original for Excel file writing and reading can be so simple!

The use of PHP for the reading of Excel, mainly through the Phpexcel plug-in to complete.

Phpexcel Download Address: Phpexcel download

First, phpexcel implementation of the write Excel operation steps

First, you need to introduce the class file and introduce the phpexcel.php file.

1. Create a new Excel table (instantiate the Phpexcel Class)

2. Create sheet (built-in table) (Createsheet () method, Setactivesheet () method, Getactivesheet () method)

3. Fill data (Setcellvalue () method)

4. Save file (Phpexcel_iofactory::createwriter () method, Save method)

Second, Phpexcel implement read Excel operation step

First, you need to introduce the class file and introduce the iofactory.php file.

1. Instantiating an Excel read object

2. Load Excel file (load all, select load)

3. Read the Excel file (read all, read-line)

Use Phpexcel to write and read code for Excel files:

<?php $dir = dirname (__file__); Find the path where the current script is located/*require $dir. ' \lib\phpexcel_1.8.0_doc\classes\phpexcel.php ';   Add the class file required to read Excel $objPHPExcel = new Phpexcel (); Instantiates a phpexcel () object $objSheet = $objPHPExcel->getactivesheet ();   Select the current Sheet object $objSheet->settitle (' Helen '); Name the current sheet object//Conventional way: Fill data with Setcellvalue () $objSheet->setcellvalue ("A1", "Zhang San")->setcellvalue ("B1", "John Doe"); Fill data//Trickery mode with setcellvalues (): Fill data with FromArray () $array = Array ("", "B1", "Zhang San"), Array ("", "B2", "John Doe")); $objSheet->fromarray ($array); Using FromArray () to populate data directly $objWriter = Phpexcel_iofactory::createwriter ($objPHPExcel, ' Excel2007 '); Sets the type of writing to Excel $objWriter->save ($dir. '/test.xlsx '); *//save file//Read Excel data require $dir. ' \lib\phpexcel_1.8.0_ Doc\classes\phpexcel\iofactory.php '; $filename = $dir. ' \test.xlsx '; $objPHPExcelReader = Phpexcel_iofactory::load ($filename); Load Excel file foreach ($objPHPExcelReader->getworksheetiterator () as $sheet)//loop read sheet {foreach ($sheet, Getrowiterator () as$row)//Line processing {if ($row->getrowindex () <2)//determines from which row to start reading {continue;  } foreach ($row->getcelliterator () as $cell)//per-column Read {$data = $cell->getvalue ();//Get echo $data of data in cell; } echo ' <br/> '; }}?>

The above is the whole content of this article, I hope that everyone's study has helped.


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.