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

Worksheet::writeFormula

Worksheet::writeFormula – Write a formula to the specified row and column (zero indexed).

Synopsis

require_once "Spreadsheet/Excel/Writer.php";

integer Worksheet::writeFormula ( integer $row , integer $col , string $formula , mixed $format=0 )

Description

Write a formula to the specified row and column (zero indexed). In case of error it will write the error message (instead of the formula) in the corresponding row and column.

Parameter

  • integer $row - Zero indexed row

  • integer $col - Zero indexed column

  • string $formula - The formula text string

  • mixed $format - The optional XF format

Return value

integer - 0 for normal termination, -1 for an error in the formula, -2 for row or column out of range.

Note

This function can not be called statically.

Formulas must start with an equal sign ('=').

Arguments given to an Excel function should be separated by comas (','), not by semicolons (';').

Example

Using writeFormula()

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

$workbook = new Spreadsheet_Excel_Writer('formula.xls');
$worksheet =& $workbook->addWorksheet();

$worksheet->write(002);
$worksheet->write(01"and");
$worksheet->write(022);
$worksheet->write(03"makes");
$worksheet->writeFormula(04"=SUM(A1,C1)");

$workbook->close();
?>
Write a blank cell to the specified row and column (zero indexed). (Previous) Write a hyperlink. This is comprised of two elements: the visible label and (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: mauricio.tellez@gmail.com
If you have the following formula in excel:

=Hoja1.B1+Hoja2.C4

you must translate to excel_writer like this:

$sheet->writeFormula($row, $col, "=Hoja1!B1+Hoja2!C4");
Note by: Bastian Hoyer
$worksheet->writeFormula(5, 0, '=VLOOKUP("John Smith",A1:A3,1,0)');

works for me.. so advanced functions should be no problem
Note by: tom@inxco.be
Note that advanced formula's like VLOOKUP do not work...
Note by: alvaro@demogracia.com
There's a builtin function to convert row & col into Excel coordinates:

Spreadsheet_Excel_Writer::rowcolToCell()

It's also worth noting that function names show be in English, i.e., you can't use localized names as in real Excel.
Note by: Le Tange
private function getLetterColumn($num)
{
if($num> 26)
{
$str = chr((($num - 1) / 26) + 64)
.chr((($num - 1) % 26) + 65);
}
else
{
$str = chr($num + 64);
}
return $str;
}
Note by: woehlken@quadracom.de
As excel columns range from A to ZZ this may be an even simpler approach:

function excel_column($col_number) {
if( ($col_number < 0) || ($col_number > 701)) die('Column must be between 0(A) and 701(ZZ)');
if($col_number < 26) {
return(chr(ord('A') + $col_number));
} else {
$remainder = floor($col_number / 26) - 1;
return(chr(ord('A') + $remainder) . excel_column($col_number % 26));
}
}

Regards
Marc
Note by: luis.sv@gmail.com
Function to convert a column number to the Excel column format.
For example:
0 = 'A';
1 = 'B';
2 = 'C'; ...
10 = 'K'; ...

I wrote it until 'NZ', but you can continue...

function excel_column($col_number){
$xls_columns=array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
'Q','R','S','T','U','V','W','X','Y','Z','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ',
'AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ',
'BA','BB','BC','BD','BE','BF','BG','BH','BI','BJ',
'BK','BL','BM','BN','BO','BP','BQ','BR','BS','BT','BU','BV','BW','BX','BY','BZ',
'CA','CB','CC','CD','CE','CF','CG','CH','CI','CJ',
'CK','CL','CM','CN','CO','CP','CQ','CR','CS','CT','CU','CV','CW','CX','CY','CZ',
'DA','DB','DC','DD','DE','DF','DG','DH','DI','DJ',
'DK','DL','DM','DN','DO','DP','DQ','DR','DS','DT','DU','DV','DW','DX','DY','DZ',
'EA','EB','EC','ED','EE','EF','EG','EH','EI','EJ',
'EK','EL','EM','EN','EO','EP','EQ','ER','ES','ET','EU','EV','EW','EX','EY','EZ',
'FA','FB','FC','FD','FE','FF','FG','FH','FI','FJ',
'FK','FL','FM','FN','FO','FP','FQ','FR','FS','FT','FU','FV','FW','FX','FY','FZ',
'GA','GB','GC','GD','GE','GF','GG','GH','GI','GJ',
'GK','GL','GM','GN','GO','GP','GQ','GR','GS','GT','GU','GV','GW','GX','GY','GZ',
'HA','HB','HC','HD','HE','HF','HG','HH','HI','HJ',
'HK','HL','HM','HN','HO','HP','HQ','HR','HS','HT','HU','HV','HW','HX','HY','HZ',
'IA','IB','IC','ID','IE','IF','IG','IH','II','IJ',
'IK','IL','IM','IN','IO','IP','IQ','IR','IS','IT','IU','IV','IW','IX','IY','IZ',
'KA','KB','KC','KD','KE','KF','KG','KH','KI','KJ',
'KK','KL','KM','KN','KO','KP','KQ','KR','KS','KT','KU','KV','KW','KX','KY','KZ',
'LA','LB','LC','LD','LE','LF','LG','LH','LI','LJ',
'LK','LL','LM','LN','LO','LP','LQ','LR','LS','LT','LU','LV','LW','LX','LY','LZ',
'MA','MB','MC','MD','ME','MF','MG','MH','MI','MJ',
'MK','ML','MM','MN','MO','MP','MQ','MR','MS','MT','MU','MV','MW','MX','MY','MZ',
'NA','NB','NC','ND','NE','NF','NG','NH','NI','NJ',
'NK','NL','NM','NN','NO','NP','NQ','NR','NS','NT','NU','NV','NW','NX','NY','NZ');
return $xls_columns[$col_number];
}