<?php
// spreadsheet data
$data = array(
array('', 'Math', 'Literature', 'Science'),
array('John', 24, 54, 38),
array('Mark', 67, 22, 57),
array('Tim', 69, 32, 58),
array('Sarah', 81, 78, 68),
array('Susan', 16, 44, 38),
);
// include package
include 'Spreadsheet/Excel/Writer.php';
// create empty file
$excel = new Spreadsheet_Excel_Writer('grades.xls');
// add worksheet
$sheet =& $excel->addWorksheet('Class I');
// add data to worksheet
$rowCount=0;
foreach ($data as $row) {
for($x=0; $x<sizeof($row); $x++) {
$sheet->write($rowCount, $x, $row[$x]);
}
// get cell coordinates
$start = Spreadsheet_Excel_Writer::rowcolToCell($rowCount, 1);
$end = Spreadsheet_Excel_Writer::rowcolToCell($rowCount, (sizeof($row)-1));
// add AVERAGE() formula to terminating cell of each row
// except the first (header) row
if ($rowCount != 0) {
$sheet->writeFormula($rowCount, sizeof($row), "=AVERAGE($start:$end)");
}
$rowCount++;
}
// save file to disk
if ($excel->close() === true) {
echo 'Spreadsheet successfully saved!';
} else {
echo 'ERROR: Could not save spreadsheet.';
}
?>