Example of reading and writing an excel file in php .,

Source: Internet
Author: User
Tags php excel

Example of reading and writing an excel file in php .,

Test environment: php5.6.24. there is no compatibility problem.

For more chestnuts, see PHPExcel examples. It is quite powerful.

Read excel files.

Step 1: Download the open-source PHP Excel class library file. The official website is http://www.codeplex.com/phpexcel. There are also many sample packages.

Step 2: 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 worksheet $ objPHPExcel-> setActiveSheetIndex (1); // obtain the table of the current activity. This will be used for subsequent operations. It's too ugly for employers to like chained operations. $ activeSheet = $ objPHPExcel-> getActiveSheet (); // The 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 '

 

Export an excel file.

The first step is to download the PHPExcel class library file.

Step 2: export the sample code of an excel file:

1 // export the excel file ------------------------------ 2 require_once './Classes/PHPExcel. php'; 3 $ objPHPExcel = new PHPExcel (); 4 // some descriptions of the excel file. In Classes/PHPExcel/DocumentProperties. php has more options 5 $ prop = $ objPHPExcel-> getProperties (); 6 $ prop-> setCreator ('sweat _ xiaom '); 7 $ prop-> setLastModifiedBy ('xiaom'); 8 $ prop-> setTitle ('Office 2007 XLSX document '); 9 $ prop-> setSubject ('Office 2007 XLSX document'); 10 $ prop-> setDescription ('Document for Office 2007 XLSX, generated using PHP classes. '); 11 $ prop-> setKeywords ('Office 2007 openxml php'); 12 $ Prop-> setCategory ('result file'); 13 14 // you can specify the index of the current worksheet to use. 15 $ objPHPExcel-> setActiveSheetIndex (0 ); 16 // then you can set the content on the cell. 17 $ activeSheet = $ objPHPExcel-> getActiveSheet (); 18 $ activeSheet-> setCellValue ('a1', 'student ID '); 19 $ activeSheet-> setCellValue ('b1 ', 'Year'); 20 $ activeSheet-> setCellValue ('c1 ', 'class'); 21 $ activeSheet-> setCellValue ('d1', 'name '); 22 $ activeSheet-> setCellValue ('e1 ', 'Gender'); 23 24 // set the title for the currently used worksheet. 25 $ activeSheet-> setTitle ('worksheet 1 Lala la'); 26 // file name. The following header is used. 27 $ filename = 'Student Information statistical table _'. date ('Y-m-dHis '); 28 29/* 30 * generate xlsx file 31 */32 // header ('content-Type: application/vnd. openxmlformats-officedocument.spreadsheetml.sheet '); 33 // header ('content-Disposition: attachment; filename = "'.w.filename.'.xlsx"'); 34 // header ('cache-Control: max-age = 0'); 35 // $ objWriter = PHPExcel_IOFactory: createWriter ($ objPHPExcel, 'excel2007 '); 36 37/* 38 * generate the xls file 39 */40 header ('content-Type: application/vnd. ms-excel '); 41 header ('content-Disposition: attachment; filename = "'.w.filename.'.xls"'); 42 header ('cache-Control: max-age = 0 '); 43 $ objWriter = PHPExcel_IOFactory: createWriter ($ objPHPExcel, 'excel5'); 44 $ objWriter-> save ('php: // output'); 45 exit;

 

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.