PHP Operation Excel–phpexcel Basic usage

Source: Internet
Author: User
Tags save file

The use of PHP for writing and reading Excel, mainly through the Phpexcel plug-in to complete. Ready to work: 1. Download the Phpexcel sdk,:https://github.com/phpoffice/phpexcel. 2.SDK after decompression will be inside the classes file (including Phpexcel files and phpexcel.php) copied to their own project one, phpexcel implementation of the Excel operation step first need to introduce class file, introduce phpexcel.php file. 1. Create a new Excel table (instantiate Phpexcel Class) 2, creating sheet (built-in table) (Createsheet () method, Setactivesheet () method, Getactivesheet () method) 3, Fill data (Setcellvalue () method) 4, Save file (Phpexcel_iofactory::createwriter () method, Save method) Two, Phpexcel implement read Excel operation step first need to introduce class file, Introduce the iofactory.php file. 1. Instantiate Excel read object 2, load Excel file (load all, select load) 3, read Excel file (read all, read line)

  

<?PHP//using Phpexcel to implement an Excel file write Demo sample:$dir=dirname(__file__);//find the path where the current script is locatedrequire $dir.‘ /phpexcel.php ';//add the class file that is required to read Excel$objPHPExcel=NewPhpexcel ();//instantiate a Phpexcel () object$objSheet=$objPHPExcel->getactivesheet ();//Select the current sheet object$objSheet->settitle (' 2018test ');//Name the current sheet object//General method: Fill data with Setcellvalue ()$objSheet->setcellvalue ("A1", "Zhang Yi")->setcellvalue ("B1", "Zhang Yi");//populating data with setcellvalues ()//Trickery mode: Populating data with FromArray ()$array=Array(    Array("", "" "," C1 "," Zhang San "),Array("", "", "C2", "John Doe"));$objSheet->fromarray ($array);//use FromArray () to populate data directly at once$objWriter= Phpexcel_iofactory::createwriter ($objPHPExcel, ' Excel2007 ');//set the type to write to Excel$objWriter->save ($dir.‘ /test.xlsx ');//Save File
<?PHP//using Phpexcel to implement Excel file reading demo example://Read Excel data using PHPHeader("Content-type:text/html;charset=utf-8");//Set page content is HTML, encoding format is Utf-8$dir=dirname(__file__);//find the path where the current script is locatedrequire $dir.‘ /phpexcel/iofactory.php ';$filename=$dir.‘ /test.xlsx ';$objPHPExcelReader= Phpexcel_iofactory::load ($filename);//Load Excel fileforeach($objPHPExcelReader->getworksheetiterator () as $sheet)//Loop Read Sheet{    foreach($sheet->getrowiterator () as $row)//line-wise processing    {        if($row->getrowindex () <1)//determines which row to start reading from        {            Continue; }        foreach($row->getcelliterator () as $cell)//column-wise read        {            $data=$cell->getvalue ();//get data in cell            Echo $data; }        Echo' <br/> '; }}

PHP Operation Excel–phpexcel Basic usage

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.