{"id":15128,"date":"2016-11-07T16:15:08","date_gmt":"2016-11-07T14:15:08","guid":{"rendered":"http:\/\/www.webcodegeeks.com\/?p=15128"},"modified":"2018-01-09T10:09:10","modified_gmt":"2018-01-09T08:09:10","slug":"php-export-excel-example","status":"publish","type":"post","link":"https:\/\/www.webcodegeeks.com\/php\/php-export-excel-example\/","title":{"rendered":"PHP Export to Excel Example"},"content":{"rendered":"<p>It can become neccessary to export data to a browser in excel format or just create an excel document from data received from a database or any other source. In this example we will create a (PHP) web app that exports data to excel.<\/p>\n<p>For this example we will use:<\/p>\n<ul>\n<li>A computer with PHP &gt;= 5.5 installed<\/li>\n<li>notepad++<\/li>\n<li>Microsoft Excel to test the examples<\/li>\n<li>PHPExcel<\/li>\n<\/ul>\n<p>&nbsp;<br \/>\n[ulp id=&#8217;8njY7i2QRy6sg8pg&#8217;]<\/p>\n<h2>1. PHPExcel<\/h2>\n<p>According to the website, PhpExcel is a Project providing a set of classes for the PHP programming language, which allows you to write to, and read from different spreadsheet file formats, like Excel (BIFF) .xls, Excel 2007 (OfficeOpenXML) .xlsx, CSV, Libre\/OpenOffice Calc .ods, Gnumeric, PDF, HTML, &#8230; This project is built around Microsoft&#8217;s OpenXML standard and PHP.<\/p>\n<p>PHPExcel allows you to create an excel file and add spreadsheet metadata to your file, such as title, description, author, last modified by, category, subject e.t.c. We are going to use PHPExcel to create our Excel documents in this example.<br \/>\nRequirements for PHPExcel are<\/p>\n<ul>\n<li>PHP version 5.2.0 or higher<\/li>\n<li>PHP extension php_zip enabled<\/li>\n<li>PHP extension php_xml enabled<\/li>\n<li>PHP extension php_gd2 enabled (if not compiled in)<\/li>\n<\/ul>\n<p>To use PHP Excel to create an Excel file on the fly,<\/p>\n<ol>\n<li>You have to download it from <a href=\"https:\/\/github.com\/PHPOffice\/PHPExcel\"> github <\/a><\/li>\n<li>Extract the contents to a suitable place on your computer.<\/li>\n<li>Copy the classes folder in the extracted folder and put it in the location where your application resides. So your file structure resembles the one below:\n<pre class=\"brush:bash\">\\var\\www\\Classes\r\n\\var\\www\\your_app\r\n<\/pre>\n<\/li>\n<\/ol>\n<p><span style=\"text-decoration: underline;\"><em>index.php<\/em><\/span><\/p>\n<pre class=\"brush:php; highlight:[18,21,30]; wrap-lines:false;\"> \r\n&lt;?php\r\n\r\n\/** Error reporting *\/\r\nerror_reporting(E_ALL);\r\nini_set('display_errors', TRUE);\r\nini_set('display_startup_errors', TRUE);\r\ndate_default_timezone_set('Europe\/London');\r\n\r\ndefine('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '&lt;br \/&gt;');\r\n\r\n\/** Include PHPExcel *\/\r\nrequire_once dirname(__FILE__) . '\/..\/Classes\/PHPExcel.php';\r\n\r\n\/\/ we Create a new PHPExcel object\r\n$objPHPExcel = new PHPExcel();\r\n\r\n\/\/ Set the excel document properties or metadata\r\n$objPHPExcel-&gt;getProperties()-&gt;setCreator(\" john mark simeon \")\r\n\t\t\t\t\t\t\t -&gt;setLastModifiedBy(\"john mark simeon \")\r\n\t\t\t\t\t\t\t -&gt;setTitle(\"john mark simeon phpexcel document \")\r\n\t\t\t\t\t\t\t -&gt;setSubject(\"john mark simeon phpexcel document\")\r\n\t\t\t\t\t\t\t -&gt;setDescription(\" This is a test document for webcodegeeks.\")\r\n\t\t\t\t\t\t\t -&gt;setKeywords(\"office PHPExcel php\")\r\n\t\t\t\t\t\t\t -&gt;setCategory(\" Test result file \");\r\n\r\n\r\n\/\/ Add some data to the excel document\r\n$objPHPExcel-&gt;setActiveSheetIndex(0)\r\n            -&gt;setCellValue('A1', 'Hello')\r\n            -&gt;setCellValue('B2', 'world!')\r\n            -&gt;setCellValue('C1', 'Hello')\r\n            -&gt;setCellValue('D2', 'world!');\r\n\r\n\/\/ Miscellaneous glyphs, UTF-8\r\n$objPHPExcel-&gt;setActiveSheetIndex(0)\r\n            -&gt;setCellValue('A4', 'Miscellaneous glyphs')\r\n            -&gt;setCellValue('A5', 'test it');\r\n$objPHPExcel-&gt;getActiveSheet()-&gt;getStyle('A4')-&gt;getAlignment()-&gt;setWrapText(true);\r\n\r\n$objPHPExcel-&gt;getActiveSheet()-&gt;setCellValue('A8',\"Hello\\nWorld\");\r\n$objPHPExcel-&gt;getActiveSheet()-&gt;getRowDimension(8)-&gt;setRowHeight(-1);\r\n$objPHPExcel-&gt;getActiveSheet()-&gt;getStyle('A8')-&gt;getAlignment()-&gt;setWrapText(true);\r\n\r\n\r\n$value = \"-ValueA\\n-Value B\\n-Value C\";\r\n$objPHPExcel-&gt;getActiveSheet()-&gt;setCellValue('A10', $value);\r\n$objPHPExcel-&gt;getActiveSheet()-&gt;getRowDimension(10)-&gt;setRowHeight(-1);\r\n$objPHPExcel-&gt;getActiveSheet()-&gt;getStyle('A10')-&gt;getAlignment()-&gt;setWrapText(true);\/\/ we wrap the text in this column so it wouldnt cross it boundary, set this to false and see what happens\r\n$objPHPExcel-&gt;getActiveSheet()-&gt;getStyle('A10')-&gt;setQuotePrefix(true);\r\n\r\n\r\n\r\n\/\/ Rename worksheet\r\necho date('H:i:s') , \" Rename worksheet\" , EOL;\r\n$objPHPExcel-&gt;getActiveSheet()-&gt;setTitle('Simple');\r\n\r\n\r\n\/\/ Set active sheet index to the first sheet, so Excel opens this as the first sheet\r\n$objPHPExcel-&gt;setActiveSheetIndex(0);\r\n\r\n\r\n\/\/ Save Excel 2007 file\r\necho date('H:i:s') , \" Write to Excel2007 format\" , EOL;\r\n$callStartTime = microtime(true);\r\n\r\n$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');\r\n$objWriter-&gt;save(str_replace('.php', '.xlsx', __FILE__));\r\n$callEndTime = microtime(true);\r\n$callTime = $callEndTime - $callStartTime;\r\n?&gt;\r\n<\/pre>\n<p>In the code above we use PHPExcel to create an excel document. Make sure to exactly follow the instructions above on how to get PHPExcel working.<br \/>\nIn line 18 we create a PHPExcel object. In line 21 we set the document properties and we start adding data to the excel document in line 30.<br \/>\nThe code above teaches you how to get started with PHPExcel. You should dive into the documentation to fully learn all the features of PHPExcel.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>index2.php<\/em><\/span><\/p>\n<pre class=\"brush:php;highlight:[48,52,60]; wrap-lines:false;\"> \r\n&lt;?php\r\n\r\n\r\n\r\n&lt;?php\r\n\/** Error reporting *\/\r\nerror_reporting(E_ALL);\r\nini_set('display_errors', TRUE); \r\nini_set('display_startup_errors', TRUE); \r\ndate_default_timezone_set('Europe\/London');\r\n\r\ndefine('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '&lt;br \/&gt;');\r\n\r\n\/** Include PHPExcel *\/\r\nrequire_once dirname(__FILE__) . '\/..\/Classes\/PHPExcel.php';\r\n\r\n\r\n\/\/ Create new PHPExcel object\r\necho date('H:i:s') , \" Create new PHPExcel object\" , EOL;\r\n$objPHPExcel = new PHPExcel();\r\n\r\n\/\/ Set document properties\r\necho date('H:i:s') , \" Set document properties\" , EOL;\r\n\/\/ Set the excel document properties or metadata\r\n$objPHPExcel-&gt;getProperties()-&gt;setCreator(\" john mark simeon \")\r\n\t\t\t\t\t\t\t -&gt;setLastModifiedBy(\"john mark simeon \")\r\n\t\t\t\t\t\t\t -&gt;setTitle(\"john mark simeon phpexcel document \")\r\n\t\t\t\t\t\t\t -&gt;setSubject(\"john mark simeon phpexcel document\")\r\n\t\t\t\t\t\t\t -&gt;setDescription(\" This is a test document for webcodegeeks.\")\r\n\t\t\t\t\t\t\t -&gt;setKeywords(\"office PHPExcel php\")\r\n\t\t\t\t\t\t\t -&gt;setCategory(\" Test result file \");\r\n\r\n\/\/ Set default font\r\necho date('H:i:s') , \" Set default font\" , EOL;\r\n$objPHPExcel-&gt;getDefaultStyle()-&gt;getFont()-&gt;setName('Arial')\r\n                                          -&gt;setSize(13);\r\n\r\n\/\/ Add some data, resembling some different data types\r\necho date('H:i:s') , \" Add some data\" , EOL;\r\n$objPHPExcel-&gt;getActiveSheet()-&gt;setCellValue('A1', 'String')\r\n                              -&gt;setCellValue('B1', 'Simple')\r\n                              -&gt;setCellValue('C1', 'PHPExcel');\r\n\r\n$objPHPExcel-&gt;getActiveSheet()-&gt;setCellValue('A2', 'String')\r\n                              -&gt;setCellValue('B2', 'Symbols')\r\n                              -&gt;setCellValue('C2', '!+&amp;=()~\u00a7\u00b1\u00e6\u00fe');\r\n\r\n$objPHPExcel-&gt;getActiveSheet()-&gt;setCellValue('A3', 'String')\r\n                              -&gt;setCellValue('B3', 'UTF-8')\r\n                              -&gt;setCellValue('C3', '\u0421\u043e\u0437\u0434\u0430\u0442\u044c MS Excel \u041a\u043d\u0438\u0433\u0438 \u0438\u0437 PHP \u0441\u043a\u0440\u0438\u043f\u0442\u043e\u0432');\r\n\r\n$objPHPExcel-&gt;getActiveSheet()-&gt;setCellValue('A4', 'Number')\r\n                              -&gt;setCellValue('B4', 'Integer')\r\n                              -&gt;setCellValue('C4', 12);\r\n\r\n$objPHPExcel-&gt;getActiveSheet()-&gt;setCellValue('A5', 'Number')\r\n                              -&gt;setCellValue('B5', 'Float')\r\n                              -&gt;setCellValue('C5', 34.56);\r\n\r\n$objPHPExcel-&gt;getActiveSheet()-&gt;setCellValue('A6', 'Number')\r\n                              -&gt;setCellValue('B6', 'Negative')\r\n                              -&gt;setCellValue('C6', -7.89);\r\n\r\n$objPHPExcel-&gt;getActiveSheet()-&gt;setCellValue('A7', 'Boolean')\r\n                              -&gt;setCellValue('B7', 'True')\r\n                              -&gt;setCellValue('C7', true);\r\n\r\n$objPHPExcel-&gt;getActiveSheet()-&gt;setCellValue('A8', 'Boolean')\r\n                              -&gt;setCellValue('B8', 'False')\r\n                              -&gt;setCellValue('C8', false);\r\n\r\n$dateTimeNow = time();\r\n$objPHPExcel-&gt;getActiveSheet()-&gt;setCellValue('A9', 'Date\/Time')\r\n                              -&gt;setCellValue('B9', 'Date')\r\n                              -&gt;setCellValue('C9', PHPExcel_Shared_Date::PHPToExcel( $dateTimeNow ));\r\n$objPHPExcel-&gt;getActiveSheet()-&gt;getStyle('C9')-&gt;getNumberFormat()-&gt;setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDD2);\r\n\r\n$objPHPExcel-&gt;getActiveSheet()-&gt;setCellValue('A10', 'Date\/Time')\r\n                              -&gt;setCellValue('B10', 'Time')\r\n                              -&gt;setCellValue('C10', PHPExcel_Shared_Date::PHPToExcel( $dateTimeNow ));\r\n$objPHPExcel-&gt;getActiveSheet()-&gt;getStyle('C10')-&gt;getNumberFormat()-&gt;setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME4);\r\n\r\n$objPHPExcel-&gt;getActiveSheet()-&gt;setCellValue('A11', 'Date\/Time')\r\n                              -&gt;setCellValue('B11', 'Date and Time')\r\n                              -&gt;setCellValue('C11', PHPExcel_Shared_Date::PHPToExcel( $dateTimeNow ));\r\n$objPHPExcel-&gt;getActiveSheet()-&gt;getStyle('C11')-&gt;getNumberFormat()-&gt;setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_DATETIME);\r\n\r\n$objPHPExcel-&gt;getActiveSheet()-&gt;setCellValue('A12', 'NULL')\r\n                              -&gt;setCellValue('C12', NULL);\r\n\r\n$objRichText = new PHPExcel_RichText();\r\n$objRichText-&gt;createText('\u4f60\u597d ');\r\n\r\n$objPayable = $objRichText-&gt;createTextRun('\u4f60 \u597d \u5417\uff1f');\r\n$objPayable-&gt;getFont()-&gt;setBold(true);\r\n$objPayable-&gt;getFont()-&gt;setItalic(true);\r\n$objPayable-&gt;getFont()-&gt;setColor( new PHPExcel_Style_Color( PHPExcel_Style_Color::COLOR_DARKGREEN ) );\r\n\r\n$objRichText-&gt;createText(', unless specified otherwise on the invoice.');\r\n\r\n$objPHPExcel-&gt;getActiveSheet()-&gt;setCellValue('A13', 'Rich Text')\r\n                              -&gt;setCellValue('C13', $objRichText);\r\n\r\n\r\n$objRichText2 = new PHPExcel_RichText();\r\n$objRichText2-&gt;createText(\"black text\\n\");\r\n\r\n$objRed = $objRichText2-&gt;createTextRun(\"red text\");\r\n$objRed-&gt;getFont()-&gt;setColor( new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_RED  ) );\r\n\r\n$objPHPExcel-&gt;getActiveSheet()-&gt;getCell(\"C14\")-&gt;setValue($objRichText2);\r\n$objPHPExcel-&gt;getActiveSheet()-&gt;getStyle(\"C14\")-&gt;getAlignment()-&gt;setWrapText(true);\r\n\r\n\r\n$objPHPExcel-&gt;getActiveSheet()-&gt;getColumnDimension('B')-&gt;setAutoSize(true);\r\n$objPHPExcel-&gt;getActiveSheet()-&gt;getColumnDimension('C')-&gt;setAutoSize(true);\r\n\r\n$objRichText3 = new PHPExcel_RichText();\r\n$objRichText3-&gt;createText(\"Hello \");\r\n\r\n$objUnderlined = $objRichText3-&gt;createTextRun(\"underlined\");\r\n$objUnderlined-&gt;getFont()-&gt;setUnderline(true);\r\n$objRichText3-&gt;createText(' World.');\r\n\r\n$objPHPExcel-&gt;getActiveSheet()\r\n    -&gt;getCell(\"C15\")\r\n    -&gt;setValue($objRichText3);\r\n\r\n\r\n$objPHPExcel-&gt;getActiveSheet()-&gt;setCellValue('A17', 'Hyperlink');\r\n\r\n$objPHPExcel-&gt;getActiveSheet()-&gt;setCellValue('C17', 'www.phpexcel.net');\r\n$objPHPExcel-&gt;getActiveSheet()-&gt;getCell('C17')-&gt;getHyperlink()-&gt;setUrl('http:\/\/www.phpexcel.net');\r\n$objPHPExcel-&gt;getActiveSheet()-&gt;getCell('C17')-&gt;getHyperlink()-&gt;setTooltip('Navigate to website');\r\n\r\n$objPHPExcel-&gt;getActiveSheet()-&gt;setCellValue('C18', '=HYPERLINK(\"mailto:abc@def.com\",\"abc@def.com\")');\r\n\r\n\r\n\/\/ Rename worksheet\r\necho date('H:i:s') , \" Rename worksheet\" , EOL;\r\n$objPHPExcel-&gt;getActiveSheet()-&gt;setTitle('Datatypes');\r\n\r\n\r\n\/\/ Set active sheet index to the first sheet, so Excel opens this as the first sheet\r\n$objPHPExcel-&gt;setActiveSheetIndex(0);\r\n\r\n\r\n\/\/ Save Excel 2007 file\r\necho date('H:i:s') , \" Write to Excel2007 format\" , EOL;\r\n$callStartTime = microtime(true);\r\n\r\n$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');\r\n$objWriter-&gt;save(str_replace('.php', '.xlsx', __FILE__));\r\n$callEndTime = microtime(true);\r\n$callTime = $callEndTime - $callStartTime;\r\n\r\necho date('H:i:s') , \" File written to \" , str_replace('.php', '.xlsx', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL;\r\necho 'Call time to write Workbook was ' , sprintf('%.4f',$callTime) , \" seconds\" , EOL;\r\n\/\/ Echo memory usage\r\necho date('H:i:s') , ' Current memory usage: ' , (memory_get_usage(true) \/ 1024 \/ 1024) , \" MB\" , EOL;\r\n\r\n\r\necho date('H:i:s') , \" Reload workbook from saved file\" , EOL;\r\n$callStartTime = microtime(true);\r\n\r\n$objPHPExcel = PHPExcel_IOFactory::load(str_replace('.php', '.xlsx', __FILE__));\r\n\r\n$callEndTime = microtime(true);\r\n$callTime = $callEndTime - $callStartTime;\r\necho 'Call time to reload Workbook was ' , sprintf('%.4f',$callTime) , \" seconds\" , EOL;\r\n\/\/ Echo memory usage\r\necho date('H:i:s') , ' Current memory usage: ' , (memory_get_usage(true) \/ 1024 \/ 1024) , \" MB\" , EOL;\r\n\r\n\r\n\/\/var_dump($objPHPExcel-&gt;getActiveSheet()-&gt;toArray());\r\n\r\n\r\n\/\/ Echo memory peak usage\r\necho date('H:i:s') , \" Peak memory usage: \" , (memory_get_peak_usage(true) \/ 1024 \/ 1024) , \" MB\" , EOL;\r\n\r\n\/\/ Echo done\r\necho date('H:i:s') , \" Done testing file\" , EOL;\r\necho 'File has been created in ' , getcwd() , EOL;\r\n<\/pre>\n<p>Compared to the first script the example above seems to be more complex as it uses more features of PHPExcel. We add primitive values of differeny types, boolean, Integer, float( line 48, 52, 60). We set cell values to an hyperlink, email, e.t.c. Using PHPExcel can be quite overwhelming for a beginner, but if you follow and read the instructions and scripts above you will grasp the concept fast enough.<\/p>\n<p>Lets look at a very simple example with only the very basics.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>index3.php<\/em><\/span><\/p>\n<pre class=\"brush:php; wrap-lines:false;\"> \r\n&lt;?php\r\n\/** Error reporting *\/\r\nerror_reporting(E_ALL);\r\nini_set('display_errors', TRUE);\r\nini_set('display_startup_errors', TRUE);\r\ndate_default_timezone_set('Europe\/London');\r\n\r\ndefine('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '&lt;br \/&gt;');\r\n\r\n\/** Include PHPExcel *\/\r\nrequire_once dirname(__FILE__) . '\/..\/Classes\/PHPExcel.php';\r\n\r\n\/\/ Create new PHPExcel object\r\n$objPHPExcel = new PHPExcel();\r\n\r\n\/\/ Set document properties\r\n$objPHPExcel-&gt;getProperties()-&gt;setCreator(\"Maarten Balliauw\")\r\n\t\t\t\t\t\t\t -&gt;setLastModifiedBy(\"Maarten Balliauw\")\r\n\t\t\t\t\t\t\t -&gt;setTitle(\"Office 2007 XLSX Test Document\")\r\n\t\t\t\t\t\t\t -&gt;setSubject(\"Office 2007 XLSX Test Document\")\r\n\t\t\t\t\t\t\t -&gt;setDescription(\"Test document for Office 2007 XLSX, generated using PHP classes.\")\r\n\t\t\t\t\t\t\t -&gt;setKeywords(\"office 2007 openxml php\")\r\n\t\t\t\t\t\t\t -&gt;setCategory(\"Test result file\");\r\n\r\n\r\n\r\n\/\/ Add some data\r\n$objPHPExcel-&gt;setActiveSheetIndex(0)\r\n            -&gt;setCellValue('A1', 'just')\r\n            -&gt;setCellValue('B1', 'the')\r\n            -&gt;setCellValue('C1', 'basics')\r\n            -&gt;setCellValue('D1', '!!!!!');\r\n\r\n\/\/ Miscellaneous glyphs, UTF-8\r\n$objPHPExcel-&gt;setActiveSheetIndex(0)\r\n            -&gt;setCellValue('A4', 'Basics Only');\r\n            \r\n\r\n\r\n$objPHPExcel-&gt;getActiveSheet()-&gt;setCellValue('A8',\"Hello\\nWorld\");\r\n$objPHPExcel-&gt;getActiveSheet()-&gt;getRowDimension(8)-&gt;setRowHeight(-1);\r\n$objPHPExcel-&gt;getActiveSheet()-&gt;getStyle('A8')-&gt;getAlignment()-&gt;setWrapText(true);\r\n\r\n\r\n$value = \"-ValueA\\n-Value B\\n-Value C\";\r\n$objPHPExcel-&gt;getActiveSheet()-&gt;setCellValue('A10', $value);\r\n$objPHPExcel-&gt;getActiveSheet()-&gt;getRowDimension(10)-&gt;setRowHeight(-1);\r\n$objPHPExcel-&gt;getActiveSheet()-&gt;getStyle('A10')-&gt;getAlignment()-&gt;setWrapText(true);\r\n$objPHPExcel-&gt;getActiveSheet()-&gt;getStyle('A10')-&gt;setQuotePrefix(true);\r\n\r\n\r\n\r\n\/\/ Rename worksheet\r\n$objPHPExcel-&gt;getActiveSheet()-&gt;setTitle('Simple');\r\n\r\n\r\n\/\/ Set active sheet index to the first sheet, so Excel opens this as the first sheet\r\n$objPHPExcel-&gt;setActiveSheetIndex(0);\r\n\r\n\r\n\/\/ Save Excel 2007 file\r\n\r\n$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');\r\n$objWriter-&gt;save(str_replace('.php', '.xlsx', __FILE__));\r\n\r\n\r\n\r\necho 'Files have been created in ' , getcwd() , EOL;\r\n<\/pre>\n<p>The script above goes straight to the point, it just adds sone data and creates the excel document(nothing fanciful or complicated).<\/p>\n<h2>2. Summary<\/h2>\n<p>In this example we learnt how to create an excel document in PHP with PHPExcel. We have also learnt how to use phpexcel functions to create an excel document.<\/p>\n<h2>3. Download the source code<\/h2>\n<div class=\"download\">\n<p><strong>Download<\/strong><br \/>\nYou can download the full source code of this example here:\u00a0<strong><a href=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/11\/phpexporttoexcel.zip\">phpexporttoexcel<\/a><\/strong><\/p>\n<p>&nbsp;<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>It can become neccessary to export data to a browser in excel format or just create an excel document from data received from a database or any other source. In this example we will create a (PHP) web app that exports data to excel. For this example we will use: A computer with PHP &gt;= &hellip;<\/p>\n","protected":false},"author":164,"featured_media":930,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[122],"class_list":["post-15128","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","tag-php"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>PHP Export to Excel Example - Web Code Geeks - 2026<\/title>\n<meta name=\"description\" content=\"It can become neccessary to export data to a browser in excel format or just create an excel document from data received from a database or any other\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.webcodegeeks.com\/php\/php-export-excel-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PHP Export to Excel Example - Web Code Geeks - 2026\" \/>\n<meta property=\"og:description\" content=\"It can become neccessary to export data to a browser in excel format or just create an excel document from data received from a database or any other\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.webcodegeeks.com\/php\/php-export-excel-example\/\" \/>\n<meta property=\"og:site_name\" content=\"Web Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webcodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2016-11-07T14:15:08+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-01-09T08:09:10+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Olayemi Odunayo\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webcodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@webcodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Olayemi Odunayo\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"11 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-export-excel-example\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-export-excel-example\/\"},\"author\":{\"name\":\"Olayemi Odunayo\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/417918d9b5811210265e8590509718b8\"},\"headline\":\"PHP Export to Excel Example\",\"datePublished\":\"2016-11-07T14:15:08+00:00\",\"dateModified\":\"2018-01-09T08:09:10+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-export-excel-example\/\"},\"wordCount\":492,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-export-excel-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg\",\"keywords\":[\"php\"],\"articleSection\":[\"PHP\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.webcodegeeks.com\/php\/php-export-excel-example\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-export-excel-example\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/php\/php-export-excel-example\/\",\"name\":\"PHP Export to Excel Example - Web Code Geeks - 2026\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-export-excel-example\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-export-excel-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg\",\"datePublished\":\"2016-11-07T14:15:08+00:00\",\"dateModified\":\"2018-01-09T08:09:10+00:00\",\"description\":\"It can become neccessary to export data to a browser in excel format or just create an excel document from data received from a database or any other\",\"breadcrumb\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-export-excel-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.webcodegeeks.com\/php\/php-export-excel-example\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-export-excel-example\/#primaryimage\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-export-excel-example\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.webcodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PHP\",\"item\":\"https:\/\/www.webcodegeeks.com\/category\/php\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"PHP Export to Excel Example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"name\":\"Web Code Geeks\",\"description\":\"Web Developers Resource Center\",\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.webcodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webcodegeeks\",\"https:\/\/x.com\/webcodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/417918d9b5811210265e8590509718b8\",\"name\":\"Olayemi Odunayo\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/016b2a353262962fceecf5c274161cd9ef60fdf1593ec95e71d37f5fd9b444f6?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/016b2a353262962fceecf5c274161cd9ef60fdf1593ec95e71d37f5fd9b444f6?s=96&d=mm&r=g\",\"caption\":\"Olayemi Odunayo\"},\"description\":\"I am a programmer and web developer, who has experience in developing websites and writing desktop and mobile applications. I have worked with both schematic and schemaless databases. I am also familiar with third party API and working with cloud servers. I do programming on the client side with Java, JavaScript, html, Ajax and CSS while I use PHP for server side programming.\",\"sameAs\":[\"https:\/\/www.webcodegeeks.com\/\"],\"url\":\"https:\/\/www.webcodegeeks.com\/author\/olayemi-odunayo\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PHP Export to Excel Example - Web Code Geeks - 2026","description":"It can become neccessary to export data to a browser in excel format or just create an excel document from data received from a database or any other","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.webcodegeeks.com\/php\/php-export-excel-example\/","og_locale":"en_US","og_type":"article","og_title":"PHP Export to Excel Example - Web Code Geeks - 2026","og_description":"It can become neccessary to export data to a browser in excel format or just create an excel document from data received from a database or any other","og_url":"https:\/\/www.webcodegeeks.com\/php\/php-export-excel-example\/","og_site_name":"Web Code Geeks","article_publisher":"https:\/\/www.facebook.com\/webcodegeeks","article_published_time":"2016-11-07T14:15:08+00:00","article_modified_time":"2018-01-09T08:09:10+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg","type":"image\/jpeg"}],"author":"Olayemi Odunayo","twitter_card":"summary_large_image","twitter_creator":"@webcodegeeks","twitter_site":"@webcodegeeks","twitter_misc":{"Written by":"Olayemi Odunayo","Est. reading time":"11 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.webcodegeeks.com\/php\/php-export-excel-example\/#article","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/php\/php-export-excel-example\/"},"author":{"name":"Olayemi Odunayo","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/417918d9b5811210265e8590509718b8"},"headline":"PHP Export to Excel Example","datePublished":"2016-11-07T14:15:08+00:00","dateModified":"2018-01-09T08:09:10+00:00","mainEntityOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/php\/php-export-excel-example\/"},"wordCount":492,"commentCount":0,"publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/php\/php-export-excel-example\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg","keywords":["php"],"articleSection":["PHP"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.webcodegeeks.com\/php\/php-export-excel-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.webcodegeeks.com\/php\/php-export-excel-example\/","url":"https:\/\/www.webcodegeeks.com\/php\/php-export-excel-example\/","name":"PHP Export to Excel Example - Web Code Geeks - 2026","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/php\/php-export-excel-example\/#primaryimage"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/php\/php-export-excel-example\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg","datePublished":"2016-11-07T14:15:08+00:00","dateModified":"2018-01-09T08:09:10+00:00","description":"It can become neccessary to export data to a browser in excel format or just create an excel document from data received from a database or any other","breadcrumb":{"@id":"https:\/\/www.webcodegeeks.com\/php\/php-export-excel-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.webcodegeeks.com\/php\/php-export-excel-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/php\/php-export-excel-example\/#primaryimage","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.webcodegeeks.com\/php\/php-export-excel-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.webcodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"PHP","item":"https:\/\/www.webcodegeeks.com\/category\/php\/"},{"@type":"ListItem","position":3,"name":"PHP Export to Excel Example"}]},{"@type":"WebSite","@id":"https:\/\/www.webcodegeeks.com\/#website","url":"https:\/\/www.webcodegeeks.com\/","name":"Web Code Geeks","description":"Web Developers Resource Center","publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.webcodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.webcodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.webcodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webcodegeeks","https:\/\/x.com\/webcodegeeks"]},{"@type":"Person","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/417918d9b5811210265e8590509718b8","name":"Olayemi Odunayo","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/016b2a353262962fceecf5c274161cd9ef60fdf1593ec95e71d37f5fd9b444f6?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/016b2a353262962fceecf5c274161cd9ef60fdf1593ec95e71d37f5fd9b444f6?s=96&d=mm&r=g","caption":"Olayemi Odunayo"},"description":"I am a programmer and web developer, who has experience in developing websites and writing desktop and mobile applications. I have worked with both schematic and schemaless databases. I am also familiar with third party API and working with cloud servers. I do programming on the client side with Java, JavaScript, html, Ajax and CSS while I use PHP for server side programming.","sameAs":["https:\/\/www.webcodegeeks.com\/"],"url":"https:\/\/www.webcodegeeks.com\/author\/olayemi-odunayo\/"}]}},"_links":{"self":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/15128","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/users\/164"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/comments?post=15128"}],"version-history":[{"count":0,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/15128\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media\/930"}],"wp:attachment":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media?parent=15128"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/categories?post=15128"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/tags?post=15128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}