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

Format::setNumFormat

Format::setNumFormat – Sets the numeric format.

Synopsis

require_once "Spreadsheet/Excel/Writer.php";

void Format::setNumFormat ( string $num_format )

Description

Sets the numeric format. It can be date, time, currency, etc... The following table lists possible values for $num_format and the corresponding types that a numeric format expects as arguments.

Numeric formats and types
0 Decimal The amount of zeros specifies the amount of digits that will be shown
0.00 Decimal The amount of zeros after the decimal dot specifies the amount of decimal digits that will be shown
#.## Decimal The amount of sharp signs after the decimal dot specifies the maximum amount of decimal digits that will be shown
0% Percent The amount of zeros specifies the amount of digits that will be shown.
0.000% Percent The amount of zeros after the decimal dot specifies the amount of decimal digits that will be shown.
$#.#;[Red]($#.#) Currency Zeros and sharp signs have the same meaning as in other formats.
??/?? Fraction The amount of question signs in the denominator determines its precision (maximum amount of digits in the denominator).
# ??/?? Fraction A fraction with an integer part. Zeros and sharp signs are used for defining the integer part, and they have the same meaning as in other formats.
0.00E+# Scientific In scientific notation base and exponent are formated according to the same rules applied to decimals. For scientific notation zeros and sharp signs appear to be equivalent.
D-MMM-YY Date A date represented in the given notation. Month can be a one or two digits month, or a three letter month. Year can have 2 or 4 digits. The argument to be formated as a date is considered to be the number of days since December 30 1899 (Excel's day zero). For dates preceding day zero, negative numbers can be used.
D/M/YYYY h:mm:ss Date/Time A date represented in the given notation. The argument to be formated as a date is considered to be the number of days since Excel's day zero.
h:mm:ss AM/PM Time A time represented in the given notation. Be careful, the argument to be formated as a time has to be given in days. For example an argument of 0.5 would be presented as '12:00:00 PM'.

The information here presented comes from OpenOffice.org's Documentation of the Microsoft Excel File Format (http://sc.openoffice.org/excelfileformat.pdf).

Parameter

  • string $num_format - The numeric format.

Note

This function can not be called statically.

Example

Using setNumFormat()

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

$workbook = new Spreadsheet_Excel_Writer();
$worksheet =& $workbook->addWorksheet();

// We'll show dates with a three letter month and four digit year format
$date_format =& $workbook->addFormat();
$date_format->setNumFormat('D-MMM-YYYY');

// number of seconds in a day
$seconds_in_a_day 86400;
// Unix timestamp to Excel date difference in seconds
$ut_to_ed_diff $seconds_in_a_day 25569;

// show Excel's day zero date
$worksheet->write(00"Excel's day zero");
$worksheet->write(010$date_format);

// show today's date
$now time();
$worksheet->write(10"Today's date:");
$worksheet->write(11, ($now $ut_to_ed_diff) / $seconds_in_a_day$date_format);

$workbook->send('num_formatting.xls');
$workbook->close();
?>
Sets the orientation of the text (Previous) Sets font as strikeout. (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: daniel@abrich.eu
The problem is that I can't use polish currency "z?" as a NumFormat. Any suggestions?
Note by: i@peter.am
Full table of formats:
http://img808.imageshack.us/img808/5599/formats.png
Note by: sascha_moetsch@hotmail.com
The format can also be defined by a number, so f.e.
$f_standard->setNumFormat(0);
gives you a Standard-Number.

A complete list of all indexes can be found here:
http://openbook.galileocomputing.de/php_pear/05_0_file_formats-007.htm
(in Table 5.8)
Note by: amit.more@xecomit.com
I have generated the excel spreadsheet using Spreadsheet_Excel_writer. Every thing is working fine in Openoffice, but in MS Office when I try to change cell format lets say For currency then every time i am getting this error "Style 'Currency' not found" or for comma "Style 'Comma' not found"?
Suggestions are welcome.
Note by: a.eibach@gmx.net
After 45 (!) minutes of searching, I finally found the solution to my problem in the PERL original of SEW.
I hope this will help out other desperate folks here.

My problem was that the numbers looked OK in Excel if you didn't touch a thing, but once you moved around in the cells, Excel (I use 2007) always kept transforming the cell to a sort of date format; nor were I able to explicitly set TEXT format, i. e. absolutely static.

The trick is simple, and of course, magnificent: :)

$fmt =& $xls->addFormat();
$fmt->setNumFormat('@');

Now your TEXT will stay TEXT and even Excel will recognize the format as text, if you check back in cell properties.
Note by: user@example.com
As the time or strtotime functions generate timestamp according to the timezone setting, the excel date given by the sample code might not be correct for computers not having the timezone as UTC, so add the following codes at the beginning.


if (function_exists('date_default_timezone_set')) {
date_default_timezone_set('UTC');
}
Note by: zetflo@gmail
Hi,

perhaps, this library support only the symbol $.

I've the same problem with symbol ¤
Note by: oldhand@fusemail.com
Noticed some strange behaviour with this when setting more complicated number formats e.g. currency

I was trying to set currency to display for UK pounds as follows £159,000

The format for this is £#,##0;-£#,##0

When setting this the format appeared to get corrupted by character encoding as my PHP file was UTF8 format.

If I forced the formatting to ISO-8859-1 before inserting this everything worked - see example below:

$myNumFormat = '£#,##0;-£#,##0';
$iso88591Format = iconv("utf-8","iso-8859-1",$myNumFormat);
$cFormat =& $xls->addFormat();
$cFormat->setNumFormat($iso88591Format);

Hope that helps saves someone else a few hours!