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.