An example of reading and writing Excel table files in PHP.

Source: Internet
Author: User

Test environment: php5.6.24. There is no compatibility problem with this piece.

Need more chestnuts, see Phpexcel's examples. It's pretty powerful.

Read the Excel file.

The first step, download the open source Phpexcel class library file, the official website is http://www.codeplex.com/PHPExcel. There are also many sample packages inside.

The second step, read the Basic code example:

<?PHPrequire_once' Classes/phpexcel.php ';require_once' Classes/phpexcel/iofactory.php ';require_once' Classes/phpexcel/reader/excel5.php ';$file _url= './excel/phplv.xls ';$objReader= Phpexcel_iofactory::createreader (' Excel5 ');$objPHPExcel=$objReader->load ($file _url);//set the currently active worksheet$objPHPExcel->setactivesheetindex (1);//Gets the table of the current activity. I'll use this for the next operation. Labor is not like chain operation, too ugly.$activeSheet=$objPHPExcel-Getactivesheet ();//Maximum number of rows in the current table$highestRow=$activeSheet-Gethighestrow ();//Maximum number of columns in the current table$highestColumn=$activeSheet-Gethighestcolumn ();Echo"Maximum column:$highestColumn";Echo"Maximum row:$highestRow";Echo' ;$cell=function($cell) Use($activeSheet) {    return $activeSheet->getcell ("$cell")getValue ();};$str 1=$cell(' A13 ');Echo $str 1;Exit;

Export the Excel table file.

The first step, ditto is to download the Phpexcel class library file first.

The second step, export the Excel file sample code:

1 //--------------------------------Export an Excel file--------------------------------2 require_once'./classes/phpexcel.php ';3 $objPHPExcel=NewPhpexcel ();4 //Some descriptions of Excel files. There are more options in classes/phpexcel/documentproperties.php5 $prop=$objPHPExcel-getProperties ();6 $prop->setcreator (' Sweat_xiaoma ');7 $prop->setlastmodifiedby (' Xiaoma ');8 $prop->settitle (' Office ' XLSX Document ');9 $prop->setsubject (' Office ' XLSX Document ');Ten $prop->setdescription (' Document for Office ' XLSX, generated using PHP classes. '); One $prop->setkeywords (' Office OpenXML PHP '); A $prop->setcategory (' Result file '); -  - //sets the index of the current sheet to use the $objPHPExcel->setactivesheetindex (0); - //you can then set the contents of the cell.  - $activeSheet=$objPHPExcel-Getactivesheet (); - $activeSheet->setcellvalue (' A1 ', ' study number ')); + $activeSheet->setcellvalue (' B1 ', ' Grade '); - $activeSheet->setcellvalue (' C1 ', ' class ')); + $activeSheet->setcellvalue (' D1 ', ' name '); A $activeSheet->setcellvalue (' E1 ', ' sex '); at  - //sets the caption for the currently used worksheet.  - $activeSheet->settitle (' Sheet 1 cheerleaders '); - //file name. Used in the following header.  - $filename= ' Student Information tab _ '.Date(' Y-m-dhis '); -  in /* - * Generate xlsx file to */ + //header (' Content-type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet '); - //Header (' content-disposition:attachment;filename= '. $filename. '. Xlsx "'); the //Header (' cache-control:max-age=0 '); * //$objWriter =phpexcel_iofactory::createwriter ($objPHPExcel, ' Excel2007 '); $ Panax Notoginseng /* - * Generate XLS file the */ + Header(' Content-type:application/vnd.ms-excel '); A Header(' Content-disposition:attachment;filename= '.$filename.‘. XLS "'); the Header(' Cache-control:max-age=0 '); + $objWriter= Phpexcel_iofactory::createwriter ($objPHPExcel, ' Excel5 '); - $objWriter->save (' Php://output '); $ Exit;

An example of reading and writing Excel table files in PHP.

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.