I started using the PHPExcel module for outputting a bunch of results to Excel (or any application that can open Excel, such as Open Office, or Libre Office). One thing I noticed, was that the documentation was dodgy at best. So I’ve started taking notes for my own knowledge base.
If you want to write a cell value and ensure its a number:
$PHPExcelDocument->getActiveSheet()->setCellValue('D3', $number_to_add', PHPExcel_Cell_DataType::TYPE_NUMERIC);
Unfortunately (unlike the PEAR spreadsheet writer extension) you have to use cells, i.e. A1, D16 to reference the column/row to output something to, so you do need to keep a track of the cells you are using, etc. I wrote a small function to convert number columns to letters (for this reason):
/** * Takes a column number (i.e. 1) and converts it to a column letter (1 => A) * * @param integer $num * @return string */ function number_to_text($num) { return chr((int) $num+65); }
Setting properties of the Excel file:
$spread = new PHPExcel(); $spread->getProperties() ->setTitle('My Export') ->setCreator('Chris Tate-Davies') ->setCreated(date('Y-m-d H:i:s')) ->setLastModifiedBy('Chris Tate-Davies');