: 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.