{"id":323,"date":"2007-09-19T12:43:05","date_gmt":"2007-09-19T02:43:05","guid":{"rendered":"https:\/\/eisabainyo.net\/weblog\/2007\/09\/19\/export-into-excel-with-formula\/"},"modified":"2007-09-19T12:50:37","modified_gmt":"2007-09-19T02:50:37","slug":"export-into-excel-with-formula-in-php","status":"publish","type":"post","link":"https:\/\/eisabainyo.net\/weblog\/2007\/09\/19\/export-into-excel-with-formula-in-php\/","title":{"rendered":"Export into excel with formula in PHP"},"content":{"rendered":"<p>In PHP, exporting data into an excel file (.csv) can be achieved very easily by sending a raw HTTP <a href=\"http:\/\/au2.php.net\/manual\/en\/function.header.php\">header<\/a>.   And having a formula in cells is no brainier either (and I only found that out after searching through google and not finding any solution - hehe). <\/p>\n<p>The following is a rough example of how you do it.  What I am trying to do here as an example is have an excel formula in Age column to calculate the person's age automatically.  <\/p>\n<pre>\r\n        header('Content-Type: application\/csv');\r\n        header('Content-Disposition: inline; filename=\"report.csv\"');  \r\n\r\n        \/\/ print column titles\r\n        echo 'Name,Year born,Age' . \"\\n\";  \r\n\r\n        \/\/ a full numeric representation of a year, 4 digits; eg: 2007\r\n        $year = date('Y'); \r\n\r\n        \/\/ $count starts from 2 because the first row is reserved for column titles\r\n        $count = 2;  \r\n\r\n\twhile ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { \r\n \t    echo $row['name'] . ',';\r\n            echo $row['year'] . ',';\r\n            \/\/ formula = Current year - Bx(Year born)  B is the column position\r\n            echo '=' . $year . '-B' . $count; \r\n            echo \"\\\\n\"; \r\n            \/\/ increment $count by 1 (same as $count = $count + 1)\r\n            $count++; \r\n        }\r\n        exit;\r\n<\/pre>\n<p>[tags]php, programming, csv, snippet[\/tags]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In PHP, exporting data into an excel file (.csv) can be achieved very easily by<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,8],"tags":[],"class_list":["post-323","post","type-post","status-publish","format-standard","hentry","category-php","category-snippets"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Export into excel with formula in PHP | Tech Leadership Advice &amp; Resources<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/eisabainyo.net\/weblog\/2007\/09\/19\/export-into-excel-with-formula-in-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Export into excel with formula in PHP | Tech Leadership Advice &amp; Resources\" \/>\n<meta property=\"og:description\" content=\"In PHP, exporting data into an excel file (.csv) can be achieved very easily by\" \/>\n<meta property=\"og:url\" content=\"https:\/\/eisabainyo.net\/weblog\/2007\/09\/19\/export-into-excel-with-formula-in-php\/\" \/>\n<meta property=\"og:site_name\" content=\"Tech Leadership Advice &amp; Resources\" \/>\n<meta property=\"article:published_time\" content=\"2007-09-19T02:43:05+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2007-09-19T02:50:37+00:00\" \/>\n<meta name=\"author\" content=\"Isabel Nyo\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Isabel Nyo\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/eisabainyo.net\/weblog\/2007\/09\/19\/export-into-excel-with-formula-in-php\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/eisabainyo.net\/weblog\/2007\/09\/19\/export-into-excel-with-formula-in-php\/\"},\"author\":{\"name\":\"Isabel Nyo\",\"@id\":\"https:\/\/eisabainyo.net\/weblog\/#\/schema\/person\/33457dd19f1ad9bbd4b0cb50c54dfcab\"},\"headline\":\"Export into excel with formula in PHP\",\"datePublished\":\"2007-09-19T02:43:05+00:00\",\"dateModified\":\"2007-09-19T02:50:37+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/eisabainyo.net\/weblog\/2007\/09\/19\/export-into-excel-with-formula-in-php\/\"},\"wordCount\":96,\"publisher\":{\"@id\":\"https:\/\/eisabainyo.net\/weblog\/#\/schema\/person\/33457dd19f1ad9bbd4b0cb50c54dfcab\"},\"articleSection\":[\"PHP\",\"Snippets\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/eisabainyo.net\/weblog\/2007\/09\/19\/export-into-excel-with-formula-in-php\/\",\"url\":\"https:\/\/eisabainyo.net\/weblog\/2007\/09\/19\/export-into-excel-with-formula-in-php\/\",\"name\":\"Export into excel with formula in PHP | Tech Leadership Advice &amp; Resources\",\"isPartOf\":{\"@id\":\"https:\/\/eisabainyo.net\/weblog\/#website\"},\"datePublished\":\"2007-09-19T02:43:05+00:00\",\"dateModified\":\"2007-09-19T02:50:37+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/eisabainyo.net\/weblog\/2007\/09\/19\/export-into-excel-with-formula-in-php\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/eisabainyo.net\/weblog\/2007\/09\/19\/export-into-excel-with-formula-in-php\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/eisabainyo.net\/weblog\/2007\/09\/19\/export-into-excel-with-formula-in-php\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/eisabainyo.net\/weblog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Export into excel with formula in PHP\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/eisabainyo.net\/weblog\/#website\",\"url\":\"https:\/\/eisabainyo.net\/weblog\/\",\"name\":\"Career Resources for Professionals in Tech\",\"description\":\"Books, worksheets, templates, frameworks and other useful resources for Chief Technology Officers (CTOs), VPs of Engineering &amp; Technology Directors\",\"publisher\":{\"@id\":\"https:\/\/eisabainyo.net\/weblog\/#\/schema\/person\/33457dd19f1ad9bbd4b0cb50c54dfcab\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/eisabainyo.net\/weblog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/eisabainyo.net\/weblog\/#\/schema\/person\/33457dd19f1ad9bbd4b0cb50c54dfcab\",\"name\":\"Isabel Nyo\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/eisabainyo.net\/weblog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/3d4b1a4e0f425adb39b242b0d62c5fac07c82f8314a24631f1d16f47bdf006d8?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/3d4b1a4e0f425adb39b242b0d62c5fac07c82f8314a24631f1d16f47bdf006d8?s=96&d=mm&r=g\",\"caption\":\"Isabel Nyo\"},\"logo\":{\"@id\":\"https:\/\/eisabainyo.net\/weblog\/#\/schema\/person\/image\/\"},\"description\":\"My interests: Web Development, Web Design, Web Applications, Web 2.0, AJAX, Search Engine Optimisation, Latest Technologies and more..\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Export into excel with formula in PHP | Tech Leadership Advice &amp; Resources","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:\/\/eisabainyo.net\/weblog\/2007\/09\/19\/export-into-excel-with-formula-in-php\/","og_locale":"en_US","og_type":"article","og_title":"Export into excel with formula in PHP | Tech Leadership Advice &amp; Resources","og_description":"In PHP, exporting data into an excel file (.csv) can be achieved very easily by","og_url":"https:\/\/eisabainyo.net\/weblog\/2007\/09\/19\/export-into-excel-with-formula-in-php\/","og_site_name":"Tech Leadership Advice &amp; Resources","article_published_time":"2007-09-19T02:43:05+00:00","article_modified_time":"2007-09-19T02:50:37+00:00","author":"Isabel Nyo","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Isabel Nyo","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/eisabainyo.net\/weblog\/2007\/09\/19\/export-into-excel-with-formula-in-php\/#article","isPartOf":{"@id":"https:\/\/eisabainyo.net\/weblog\/2007\/09\/19\/export-into-excel-with-formula-in-php\/"},"author":{"name":"Isabel Nyo","@id":"https:\/\/eisabainyo.net\/weblog\/#\/schema\/person\/33457dd19f1ad9bbd4b0cb50c54dfcab"},"headline":"Export into excel with formula in PHP","datePublished":"2007-09-19T02:43:05+00:00","dateModified":"2007-09-19T02:50:37+00:00","mainEntityOfPage":{"@id":"https:\/\/eisabainyo.net\/weblog\/2007\/09\/19\/export-into-excel-with-formula-in-php\/"},"wordCount":96,"publisher":{"@id":"https:\/\/eisabainyo.net\/weblog\/#\/schema\/person\/33457dd19f1ad9bbd4b0cb50c54dfcab"},"articleSection":["PHP","Snippets"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/eisabainyo.net\/weblog\/2007\/09\/19\/export-into-excel-with-formula-in-php\/","url":"https:\/\/eisabainyo.net\/weblog\/2007\/09\/19\/export-into-excel-with-formula-in-php\/","name":"Export into excel with formula in PHP | Tech Leadership Advice &amp; Resources","isPartOf":{"@id":"https:\/\/eisabainyo.net\/weblog\/#website"},"datePublished":"2007-09-19T02:43:05+00:00","dateModified":"2007-09-19T02:50:37+00:00","breadcrumb":{"@id":"https:\/\/eisabainyo.net\/weblog\/2007\/09\/19\/export-into-excel-with-formula-in-php\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/eisabainyo.net\/weblog\/2007\/09\/19\/export-into-excel-with-formula-in-php\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/eisabainyo.net\/weblog\/2007\/09\/19\/export-into-excel-with-formula-in-php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/eisabainyo.net\/weblog\/"},{"@type":"ListItem","position":2,"name":"Export into excel with formula in PHP"}]},{"@type":"WebSite","@id":"https:\/\/eisabainyo.net\/weblog\/#website","url":"https:\/\/eisabainyo.net\/weblog\/","name":"Career Resources for Professionals in Tech","description":"Books, worksheets, templates, frameworks and other useful resources for Chief Technology Officers (CTOs), VPs of Engineering &amp; Technology Directors","publisher":{"@id":"https:\/\/eisabainyo.net\/weblog\/#\/schema\/person\/33457dd19f1ad9bbd4b0cb50c54dfcab"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/eisabainyo.net\/weblog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/eisabainyo.net\/weblog\/#\/schema\/person\/33457dd19f1ad9bbd4b0cb50c54dfcab","name":"Isabel Nyo","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/eisabainyo.net\/weblog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/3d4b1a4e0f425adb39b242b0d62c5fac07c82f8314a24631f1d16f47bdf006d8?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3d4b1a4e0f425adb39b242b0d62c5fac07c82f8314a24631f1d16f47bdf006d8?s=96&d=mm&r=g","caption":"Isabel Nyo"},"logo":{"@id":"https:\/\/eisabainyo.net\/weblog\/#\/schema\/person\/image\/"},"description":"My interests: Web Development, Web Design, Web Applications, Web 2.0, AJAX, Search Engine Optimisation, Latest Technologies and more.."}]}},"_links":{"self":[{"href":"https:\/\/eisabainyo.net\/weblog\/wp-json\/wp\/v2\/posts\/323","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/eisabainyo.net\/weblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eisabainyo.net\/weblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eisabainyo.net\/weblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/eisabainyo.net\/weblog\/wp-json\/wp\/v2\/comments?post=323"}],"version-history":[{"count":0,"href":"https:\/\/eisabainyo.net\/weblog\/wp-json\/wp\/v2\/posts\/323\/revisions"}],"wp:attachment":[{"href":"https:\/\/eisabainyo.net\/weblog\/wp-json\/wp\/v2\/media?parent=323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eisabainyo.net\/weblog\/wp-json\/wp\/v2\/categories?post=323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eisabainyo.net\/weblog\/wp-json\/wp\/v2\/tags?post=323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}