Create an Excel file in PHP

Source: Internet
Author: User
Tags php excel
: This article describes how to create an Excel file in PHP. For more information about PHP tutorials, see. The PHPExcel plug-in is used here. first, download the corresponding library file. The latest version is 1.8.0.

Download a Zip package and copy the Classes folder to the project folder to start the operation. The following is the PHP code.

Include_once 'classes/PHPExcel. php'; // plug-in main file include_once 'classes/PHPExcel/Writer/excel2007.php'; // Excel2007 Writer echo "Step 1: Create a new php Excel Object
"; $ ObjPHPExcel = new PHPExcel (); echo" Step 2: Set file attributes (optional)
"; $ ObjPHPExcel-> getProperties ()-> setCreator (" Coding "); // Set The Author $ objPHPExcel-> getProperties ()-> setLastModifiedBy (" JustCoding "); // set the last warranty period $ objPHPExcel-> getProperties ()-> setTitle ('This is the title'); // set the title $ objPHPExcel-> getProperties () -> setSubject ('This is the topic '); // set the topic $ objPHPExcel-> getProperties ()-> setDescription ('This is the remark'); // Set the note echo "Step 3: add data
"; $ ObjPHPExcel-> setActiveSheetIndex (0); // select and activate the first worksheet (at least one worksheet exists in the Excel file by default, and the subscript is 0) $ objPHPExcel-> getActiveSheet () -> setTitle ('worksheet 1'); // Set the worksheet name $ objPHPExcel-> getActiveSheet ()-> setCellValue ('A1', 'Hello '); // add data to the worksheet $ objPHPExcel-> getActiveSheet ()-> setCellValue ('B2', ''); // add data to the worksheet $ objPHPExcel-> createSheet (); // create a new worksheet $ objPHPExcel-> setActiveSheetIndex (1); // select and activate the second worksheet $ objPHPExcel-> getActiveSheet ()-> setTitle ('worksheet 2 '); // Set the worksheet name $ objPHPExcel-> getActiveSheet ()-> setCellValue ('A3 ', 'hes'); // add data to the worksheet $ objPHPExcel-> getActiveSheet () -> setCellValue ('b1 ', ''); // add data to the worksheet echo "Step 4: Save as Excel2007
"; $ ObjWriter = new PHPExcel_Writer_Excel2007 ($ objPHPExcel); // Create an Excel2007 writer $ objWriter-> save('demo.xlsx'); // echo file written in the project directory by specified file name
";

Run the project. you can see that an Excel file is generated under the project folder. right-click to view properties and you can see the relevant content set in the code.


The above describes how to create an Excel file in PHP, including some content, and hope to help those who are interested in the PHP Tutorial.

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.