1. Introduction
  2. Formatting Tutorial
  3. Workbook::close
  4. Workbook::&addWorksheet
  5. Workbook::&addFormat
  6. Workbook::setCountry
  7. Workbook::&setTempDir
  8. Workbook::setVersion
  9. Workbook::setCustomColor
  10. Workbook::worksheets
  11. Worksheet::getName
  12. Worksheet::
    setInputEncoding
  13. Worksheet::select
  14. Worksheet::activate
  15. Worksheet::setFirstSheet
  16. Worksheet::protect
  17. Worksheet::setColumn
  18. Worksheet::writeCol
  19. Worksheet::writeRow
  20. Worksheet::setSelection
  21. Worksheet::freezePanes
  22. Worksheet::thawPanes
  23. Worksheet::
    hideScreenGridlines
  24. Worksheet::setPortrait
  25. Worksheet::setLandscape
  26. Worksheet::setPaper
  27. Worksheet::setHeader
  28. Worksheet::setFooter
  29. Worksheet::setMerge
  30. Worksheet::
    centerHorizontally
  31. Worksheet::
    centerVertically
  32. Worksheet::setMargins
  33. Worksheet::setMargins_LR
  34. Worksheet::setMargins_TB
  35. Worksheet::setMarginLeft
  36. Worksheet::
    setMarginRight
  37. Worksheet::setMarginTop
  38. Worksheet::
    setMarginBottom
  39. Worksheet::repeatRows
  40. Worksheet::repeatColumns
  41. Worksheet::printArea
  42. Worksheet::hideGridlines
  43. Worksheet::
    printRowColHeaders
  44. Worksheet::fitToPages
  45. Worksheet::
    setHPagebreaks
  46. Worksheet::
    setVPagebreaks
  47. Worksheet::setZoom
  48. Worksheet::setPrintScale
  49. Worksheet::write
  50. Worksheet::writeNumber
  51. Worksheet::writeString
  52. Worksheet::writeNote
  53. Worksheet::writeBlank
  54. Worksheet::writeFormula
  55. Worksheet::writeUrl
  56. Worksheet::setRow
  57. Worksheet::mergeCells
  58. Worksheet::insertBitmap
  59. Worksheet::setOutline
  60. Spreadsheet_Excel_Writer
  61. send
  62. rowcolToCell
  63. Format::setAlign
  64. Format::setVAlign
  65. Format::setHAlign
  66. Format::setMerge
  67. Format::setLocked
  68. Format::setUnLocked
  69. Format::setBold
  70. Format::setBottom
  71. Format::setTop
  72. Format::setLeft
  73. Format::setRight
  74. Format::setBorder
  75. Format::setBorderColor
  76. Format::setBottomColor
  77. Format::setTopColor
  78. Format::setLeftColor
  79. Format::setRightColor
  80. Format::setFgColor
  81. Format::setBgColor
  82. Format::setColor
  83. Format::setPattern
  84. Format::setUnderline
  85. Format::setItalic
  86. Format::setSize
  87. Format::setTextWrap
  88. Format::setTextRotation
  89. Format::setNumFormat
  90. Format::setStrikeOut
  91. Format::setOutLine
  92. Format::setShadow
  93. Format::setScript
  94. Format::setFontFamily

Introduction

Introduction – how to generate Excel files

What is Spreadsheet_Excel_Writer?

Spreadsheet_Excel_Writer is a tool for creating Excel files without the need for COM components. The files generated by the current version of Spreadsheet_Excel_Writer correspond to the Excel 5 (BIFF5) format, so all functionality until that version of Excel (but not beyond) should be available.

Using it

The most common use for Spreadsheet_Excel_Writer will be spitting out large (or not so large) amounts of information in the form of a spreadsheet, which is easy to manipulate with a fairly ubiquitous spreadsheet program such as Excel (or OpenOffice).

So let's cut to the chase and see how this is done:

Typical usage

<?php
require_once 'Spreadsheet/Excel/Writer.php';

// Creating a workbook
$workbook = new Spreadsheet_Excel_Writer();

// sending HTTP headers
$workbook->send('test.xls');

// Creating a worksheet
$worksheet =& $workbook->addWorksheet('My first worksheet');

// The actual data
$worksheet->write(00'Name');
$worksheet->write(01'Age');
$worksheet->write(10'John Smith');
$worksheet->write(1130);
$worksheet->write(20'Johann Schmidt');
$worksheet->write(2131);
$worksheet->write(30'Juan Herrera');
$worksheet->write(3132);

// Let's send the file
$workbook->close();
?>

The first thing you should notice, is that we created a workbook before any worksheets. All worksheets are contained within a workbook, and a workbook may contain several worksheets.

Another important thing, which you should have in mind when programming with Spreadsheet_Excel_Writer, is that ampersand sign (&) that appears when we created our worksheet. That ampersand means we are referencing a Worksheet object instead of copying it. If you don't know what that means, don't worry, all you have to remember is to always use ampersands when calling addWorksheet() for creating a worksheet, or addFormat() for creating a format.

Saving to a regular file

You may have noticed also the following line:


// sending HTTP headers
$workbook->send('test.xls');

What that means is that we are sending our spreadsheet to a browser. But what if we just want to save the spreadsheet in our machine? Well, you just have to omit that line and give a valid file path to the workbook constructor.

For example, if we wanted to save the same spreadsheet we created in our first example to a file named 'test.xls', we would do it like so:

Saving to a regular file

<?php
require_once 'Spreadsheet/Excel/Writer.php';

// We give the path to our file here
$workbook = new Spreadsheet_Excel_Writer('test.xls');

$worksheet =& $workbook->addWorksheet('My first worksheet');

$worksheet->write(00'Name');
$worksheet->write(01'Age');
$worksheet->write(10'John Smith');
$worksheet->write(1130);
$worksheet->write(20'Johann Schmidt');
$worksheet->write(2131);
$worksheet->write(30'Juan Herrera');
$worksheet->write(3132);

// We still need to explicitly close the workbook
$workbook->close();
?>

More tutorials

If you would like to learn about formatting (fonts, cell color, text alignment, etc...) with Spreadsheet_Excel_Writer, you can check the formatting tutorial here.

Spreadsheet_Excel_Writer (Previous) how to format cells in a spreadsheet (Next)
Last updated: Sat, 16 Feb 2019 — Download Documentation
Do you think that something on this page is wrong? Please file a bug report.
View this page in:
  • English

User Notes:

Note by: avinashnkadam@yahoo.com
I am getting the following error.

Fatal error: Call-time pass-by-reference has been removed in D:\wamp\www\PHPTutorial\PEAR\Spreadsheet\Excel\Writer\Worksheet.php on line 2490

I am running Windows 7 with WAMP
Note by: aishwaryv8@gmail.com
I am getting the following error while using the Spreadsheet_Excel_Writer(). Please let me know how to resolve the error.thanks in advance.


PHP Warning: require_once(Spreadsheet/Excel/Writer.php): failed to open stream: No such file or directory in /home/tr523966/public_html/project/generate_excel.php on line
Note by: me@suyashjain.com
i am getting the following error.

PHP Fatal error: Call-time pass-by-reference has been removed in Spreadsheet/Excel/Writer/Worksheet.php on line 2490

i am running ubuntu 14 with the following php

php -v
PHP 5.5.9-1ubuntu4.5 (cli) (built: Oct 29 2014 11:59:42)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
with Zend OPcache v7.0.3, Copyright (c) 1999-2014, by Zend Technologies

Note by: daniel-jj@gmx.de
If you have issues with this PEAR package check this out: http://phpexcel.codeplex.com/
Note by: info@joomla-r-us.com
Thanks for improving the original spreadsheet-excel-writer!

I have integrated the PEAR spreadsheet-excel-writer into a Joomla component. The component allows you to define Worksheets which are populated with data from the database, by specifying your own custom SQL queries. One or more Worksheets can then be assembled into a Workbook - all in a nice user friendly UI within the Joomla framework.

If you have a Joomla website, or can set one up, please check out:
http://joomla-r-us.com

Note by: holubecml@gmail.com
Is it possible to let the script generate an xls file from html form and send it automatically as an attachement in an email?
Thans a lot,
Miro, Slovakia
Note by: fatrandy13@yahoo.com (Mike M.)
A simple chart/graph function would be great... You specify the rows and columns for the chart and chart type (bar, line, pie, etc...)


// make it a worksheet of its own
$chart_worksheet =& $workbook->addWorksheet($my_chart);

// add it to an existing worksheet...
$worksheet->addGraph(int $startCol, int $endCol, int $startRow, int $endRow, mixed $graphType);
Note by: chris@pineconehill.com
As of PHP5, all objects are passed as reference, so there's no need to use '=&'.
Note by: alangrus
CSVtoXLS.php Program Notes

I am often asked to provide output from the database in a spreadsheet XLS file. I built a shell around spreadsheet-excel-writer to use as a production conversion tool, to accomplish high format XLS file creation from an ASCII CSV file. As a PHP program, it should work on any O/S that you can install PHP 4.3 or better onto.

CSVtoXLS.php is a PHP utility to convert CSV data into business class XLS spreadsheet files, tested as compatible with OpenOffice 2.0 Calc and Excel 2000. It is provides for a high level of spreadsheet formatting and also offers formulas. CSVtoXLS.php takes advantage of those features and provides meaningful formatting and a simple Column SUM automatically generated at the bottom of any Decimal Column.

See Narrative and Download at http://performantsystems.com/CSVtoXLS.html
- Alan Gruskoff