PHPExcel is a collection of classes that allows you to generate excel documents in PHP. It is incredibly powerful and you can perform almost all (if not all) task that you would normally use in excel.
It is really easy to install, either download the files or they now have a pear channel. For more details visit the PHPExcel site.
This post is mainly for myself so that I can have a quick reference guide for common functions. For full documentation see PHPExcel Documentation.
PHPExcel supports fluent interfaces so function calls can be chained together. The following: Can be written like:
- Create a new object:
$phpExcel = new PHPExcel();
- Getting the active worksheet:
$sheet = $phpExcel->getActiveSheet();
- Add text to a cell:
$sheet->setCellValue("A1", "Hello World");
- Add a new worksheet:
$sheet2 = $phpExcel->createSheet();
- Add a formula to a cell:
- Rename a worksheet:
- Merge cells:
- Unmerge cells:
Adding properties to your file
- Set Author:
$phpExcel ->getProperties()->setCreator("Tom Smith");
- Set Last Modified By:
- Set Title:
- Set Subject:
- Set Description:
$phpExcel->getProperties()->setDescription("A description of the file.");
Formatting A Cell
- Setting Width:
- Setting Auto Width:
- Setting Text Alignment Right:
- Making text of a range of cells bold:
$sheet2->getStyle("A1:F1")->applyFromArray(array("font" => array( "bold" => true)));
- Adding an auto filter to a range of cells:
Output to the browser as a download.
Here is an example of PHPExcel code, it includes most of the functions described above.
Content on this blog is licensed under a Creative Commons Attribution 3.0 License.
Come and work for Clock
Clock is made up of bright, hard-working and talented people and we're always on the look out for more. You can browse the current jobs below or follow us @clock for the latest vacancies.