How can you export multiple worksheets using PHP?
In PHP, you can utilize the PHPExcel library to export multiple worksheets. Here is a simple example:
// 引入PHPExcel库
require_once 'PHPExcel/Classes/PHPExcel.php';
// 创建一个PHPExcel对象
$objPHPExcel = new PHPExcel();
// 创建第一个工作表
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setTitle('Sheet1');
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'Hello');
$objPHPExcel->getActiveSheet()->setCellValue('A2', 'World');
// 创建第二个工作表
$objPHPExcel->createSheet();
$objPHPExcel->setActiveSheetIndex(1);
$objPHPExcel->getActiveSheet()->setTitle('Sheet2');
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'Foo');
$objPHPExcel->getActiveSheet()->setCellValue('A2', 'Bar');
// 导出Excel文件
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('example.xlsx');
In this example, we first create a PHPExcel object and create two worksheets within it. Then, using the createWriter method in the PHPExcel_IOFactory class, we export the PHPExcel object as an Excel file. Finally, we save the exported Excel file as example.xlsx.
It is important to note that when exporting multiple worksheets, you need to use the createSheet method to create additional worksheets and use the setActiveSheetIndex method to specify the currently active worksheet.