// Load the Excel file require_once 'PHPExcel/PHPExcel.php'; $excel = PHPExcel_IOFactory::load("example.xls"); // Get the worksheet $worksheet = $excel->getActiveSheet(); // Write data to the worksheet $worksheet->setCellValue('A1', 'Hello World!'); // Save the worksheet $writer = PHPExcel_IOFactory::createWriter($excel, 'Excel5'); $writer->save("example.xls");
// Create a new Excel file require_once 'PHPExcel/PHPExcel.php'; $excel = new PHPExcel(); // Add some data to the worksheet $worksheet = $excel->getActiveSheet(); $worksheet->setCellValue('A1', 'Month') ->setCellValue('B1', 'Revenue') ->setCellValue('A2', 'Jan') ->setCellValue('B2', 1000) ->setCellValue('A3', 'Feb') ->setCellValue('B3', 2000) ->setCellValue('A4', 'Mar') ->setCellValue('B4', 3000); // Create a new chart $chart = new PHPExcel_Chart( 'my_chart', // Name of chart new PHPExcel_Chart_Title('Revenue by Month'), // Title new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_TOP_RIGHT), // Legend new PHPExcel_Chart_PlotArea(NULL, array( new PHPExcel_Chart_DataSeries( PHPExcel_Chart_DataSeries::TYPE_LINECHART, // Type of chart PHPExcel_Chart_DataSeries::GROUPING_STANDARD, // Grouping range(0, 2), // Index of X-axis labels array( // Series new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$B$2:$B$4', NULL, 3), ) ), ) )); // Add the chart to the worksheet $worksheet->addChart($chart); // Save the file $writer = PHPExcel_IOFactory::createWriter($excel, 'Excel2007'); $writer->save("example.xlsx");In this example, we are creating a new Excel file and adding a chart that displays the revenue by month. We are using a line chart to display the trend. Finally, we are saving the file to disk. In conclusion, PHPExcel is a powerful PHP library that can be used to work with Excel spreadsheets. It allows one to read, write, and manipulate Excel files programmatically. It is a package library that can be easily included in PHP projects.