Home >Backend Development >PHP Tutorial >Create Excel file with PHP
The PHPExcel plug-in is used here. First, download the corresponding library file. The latest version is 1.8.0. The download address is: PHPExcel official website address.
Download a Zip compressed package and copy the Classes folder inside Go to the project folder and you can start the operation. The following is the PHP code
include_once 'Classes/PHPExcel.php'; // 插件主文件 include_once 'Classes/PHPExcel/Writer/Excel2007.php'; // Excel2007写入器 echo "第一步:创建新PHPExcel对象<br />"; $objPHPExcel = new PHPExcel(); echo "第二步:设置文件属性(可选)<br />"; $objPHPExcel->getProperties()->setCreator("Coding"); // 设置作者 $objPHPExcel->getProperties()->setLastModifiedBy("JustCoding"); // 设置最后一次保存者 $objPHPExcel->getProperties()->setTitle('这是标题'); // 设置标题 $objPHPExcel->getProperties()->setSubject('这是主题'); // 设置主题 $objPHPExcel->getProperties()->setDescription('这是备注'); // 设置备注 echo "第三步:添加数据<br />"; $objPHPExcel->setActiveSheetIndex(0); // 选择并激活第一个工作表(默认Excel文件至少有一个工作表,下标为0) $objPHPExcel->getActiveSheet()->setTitle('工作表1'); // 设置工作表名称 $objPHPExcel->getActiveSheet()->setCellValue('A1', 'Hello'); // 添加数据到工作表 $objPHPExcel->getActiveSheet()->setCellValue('B2', '你好'); // 添加数据到工作表 $objPHPExcel->createSheet(); // 创建新工作表 $objPHPExcel->setActiveSheetIndex(1); // 选择并激活第二个工作表 $objPHPExcel->getActiveSheet()->setTitle('工作表2'); // 设置工作表名称 $objPHPExcel->getActiveSheet()->setCellValue('A3', 'He'); // 添加数据到工作表 $objPHPExcel->getActiveSheet()->setCellValue('B1', '你好'); // 添加数据到工作表 echo "第四步:保存为Excel2007格式<br />"; $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel); // 创建Excel2007写入器 $objWriter->save('Demo.xlsx'); // 按指定文件名保存在项目目录下 echo "文件写入完成<br />";Run the project and you can see that an Excel file is generated under the project folder. Right-click to view the properties and you can see the relevant content set in the code.
The above introduces how to create Excel files with PHP, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.