{"id":32,"date":"2019-11-07T11:54:28","date_gmt":"2019-11-07T17:54:28","guid":{"rendered":"https:\/\/travisolson.net\/code\/?p=32"},"modified":"2019-11-07T11:54:28","modified_gmt":"2019-11-07T17:54:28","slug":"php-export-array-to-excel-or-csv","status":"publish","type":"post","link":"https:\/\/www.travisolson.net\/code\/php-export-array-to-excel-or-csv\/","title":{"rendered":"PHP: Export Array to Excel or CSV"},"content":{"rendered":"<p>This is a basic function that accepts an array of data and an array of field names (column headers), and spits out a file for download.\u00a0 The two types of files are Excel and CSV.<\/p>\n<p>The example is enclosed in a utility class that can contain a number of other functions, but you can just as easily use the function without the class wrapper.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;?php\r\nclass My_Utility {\r\n  function export_array_to_file($data, $field_names, $file_name = '', $file_type = 'EXCEL') {\r\n    switch (strtoupper($file_type)) {\r\n      case \"EXCEL\":\r\n        header(\"Content-type: \".\"application\/x-msexcel\");\r\n        $DELIMITER = \"\\t\";\r\n        $file_ext = \"xls\";\r\n        break;\r\n      case \"CSV\":\r\n        header(\"Content-type: \".\"text\/plain\");\r\n        $DELIMITER = \",\";\r\n        $file_ext = \"csv\";\r\n        break;\r\n    } \r\n  \r\n    if (empty($file_name)) {\r\n      $file_name = \"my_download_\" . date('YmdHis') . \".\" . $file_ext;\r\n    }\r\n    else {\r\n      $file_name .= '.' . $file_ext;\r\n    }\r\n\r\n    \/* Stream fputcsv to php output to send to the browser instead of saving as a file *\/\r\n    header(\"content-disposition: attachment; filename=\".$file_name);\r\n    $outfile = fopen('php:\/\/output', 'w');\r\n  \r\n    if (!is_array($field_names)) {\r\n      $field_names = explode(',', $field_names);\r\n    }\r\n  \r\n    fputcsv($outfile, $field_names, $DELIMITER);\r\n    foreach($data as $row) {\r\n      fputcsv($outfile, $row, $DELIMITER);\r\n    }\r\n    fclose($outfile);\r\n    \r\n    exit();\r\n  }\r\n}<\/pre>\n<p>It&#8217;s worth noting that you shouldn&#8217;t name the first column &#8220;ID&#8221; if you are using the Excel format. This is because Excel thinks that text files starting with IDxxx are in SYLK Format, and it will not open correctly.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a basic function that accepts an array of data and an array of field names (column headers), and spits out a file for download.\u00a0 The two types of files are Excel and CSV. The example is enclosed in a utility class that can contain a number of other functions, but you can just&#8230;  <a class=\"excerpt-read-more\" href=\"https:\/\/www.travisolson.net\/code\/php-export-array-to-excel-or-csv\/\" title=\"Read PHP: Export Array to Excel or CSV\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-32","post","type-post","status-publish","format-standard","hentry","category-php"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>PHP: Export Array to Excel or CSV - Travis Olson&#039;s Code Snippets<\/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:\/\/www.travisolson.net\/code\/php-export-array-to-excel-or-csv\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PHP: Export Array to Excel or CSV - Travis Olson&#039;s Code Snippets\" \/>\n<meta property=\"og:description\" content=\"This is a basic function that accepts an array of data and an array of field names (column headers), and spits out a file for download.\u00a0 The two types of files are Excel and CSV. The example is enclosed in a utility class that can contain a number of other functions, but you can just... Read more &raquo;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.travisolson.net\/code\/php-export-array-to-excel-or-csv\/\" \/>\n<meta property=\"og:site_name\" content=\"Travis Olson&#039;s Code Snippets\" \/>\n<meta property=\"article:published_time\" content=\"2019-11-07T17:54:28+00:00\" \/>\n<meta name=\"author\" content=\"Travis Olson\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Travis Olson\" \/>\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:\/\/www.travisolson.net\/code\/php-export-array-to-excel-or-csv\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.travisolson.net\/code\/php-export-array-to-excel-or-csv\/\"},\"author\":{\"name\":\"Travis Olson\",\"@id\":\"https:\/\/www.travisolson.net\/code\/#\/schema\/person\/0170ab5b2e7a3d88436375cc50336934\"},\"headline\":\"PHP: Export Array to Excel or CSV\",\"datePublished\":\"2019-11-07T17:54:28+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.travisolson.net\/code\/php-export-array-to-excel-or-csv\/\"},\"wordCount\":112,\"publisher\":{\"@id\":\"https:\/\/www.travisolson.net\/code\/#\/schema\/person\/0170ab5b2e7a3d88436375cc50336934\"},\"articleSection\":[\"PHP\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.travisolson.net\/code\/php-export-array-to-excel-or-csv\/\",\"url\":\"https:\/\/www.travisolson.net\/code\/php-export-array-to-excel-or-csv\/\",\"name\":\"PHP: Export Array to Excel or CSV - Travis Olson&#039;s Code Snippets\",\"isPartOf\":{\"@id\":\"https:\/\/www.travisolson.net\/code\/#website\"},\"datePublished\":\"2019-11-07T17:54:28+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.travisolson.net\/code\/php-export-array-to-excel-or-csv\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.travisolson.net\/code\/php-export-array-to-excel-or-csv\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.travisolson.net\/code\/php-export-array-to-excel-or-csv\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.travisolson.net\/code\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PHP: Export Array to Excel or CSV\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.travisolson.net\/code\/#website\",\"url\":\"https:\/\/www.travisolson.net\/code\/\",\"name\":\"Travis Olson&#039;s Code Snippets\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.travisolson.net\/code\/#\/schema\/person\/0170ab5b2e7a3d88436375cc50336934\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.travisolson.net\/code\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/www.travisolson.net\/code\/#\/schema\/person\/0170ab5b2e7a3d88436375cc50336934\",\"name\":\"Travis Olson\",\"logo\":{\"@id\":\"https:\/\/www.travisolson.net\/code\/#\/schema\/person\/image\/\"},\"url\":\"https:\/\/www.travisolson.net\/code\/author\/travis-olson\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PHP: Export Array to Excel or CSV - Travis Olson&#039;s Code Snippets","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.travisolson.net\/code\/php-export-array-to-excel-or-csv\/","og_locale":"en_US","og_type":"article","og_title":"PHP: Export Array to Excel or CSV - Travis Olson&#039;s Code Snippets","og_description":"This is a basic function that accepts an array of data and an array of field names (column headers), and spits out a file for download.\u00a0 The two types of files are Excel and CSV. The example is enclosed in a utility class that can contain a number of other functions, but you can just... Read more &raquo;","og_url":"https:\/\/www.travisolson.net\/code\/php-export-array-to-excel-or-csv\/","og_site_name":"Travis Olson&#039;s Code Snippets","article_published_time":"2019-11-07T17:54:28+00:00","author":"Travis Olson","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Travis Olson","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.travisolson.net\/code\/php-export-array-to-excel-or-csv\/#article","isPartOf":{"@id":"https:\/\/www.travisolson.net\/code\/php-export-array-to-excel-or-csv\/"},"author":{"name":"Travis Olson","@id":"https:\/\/www.travisolson.net\/code\/#\/schema\/person\/0170ab5b2e7a3d88436375cc50336934"},"headline":"PHP: Export Array to Excel or CSV","datePublished":"2019-11-07T17:54:28+00:00","mainEntityOfPage":{"@id":"https:\/\/www.travisolson.net\/code\/php-export-array-to-excel-or-csv\/"},"wordCount":112,"publisher":{"@id":"https:\/\/www.travisolson.net\/code\/#\/schema\/person\/0170ab5b2e7a3d88436375cc50336934"},"articleSection":["PHP"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.travisolson.net\/code\/php-export-array-to-excel-or-csv\/","url":"https:\/\/www.travisolson.net\/code\/php-export-array-to-excel-or-csv\/","name":"PHP: Export Array to Excel or CSV - Travis Olson&#039;s Code Snippets","isPartOf":{"@id":"https:\/\/www.travisolson.net\/code\/#website"},"datePublished":"2019-11-07T17:54:28+00:00","breadcrumb":{"@id":"https:\/\/www.travisolson.net\/code\/php-export-array-to-excel-or-csv\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.travisolson.net\/code\/php-export-array-to-excel-or-csv\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.travisolson.net\/code\/php-export-array-to-excel-or-csv\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.travisolson.net\/code\/"},{"@type":"ListItem","position":2,"name":"PHP: Export Array to Excel or CSV"}]},{"@type":"WebSite","@id":"https:\/\/www.travisolson.net\/code\/#website","url":"https:\/\/www.travisolson.net\/code\/","name":"Travis Olson&#039;s Code Snippets","description":"","publisher":{"@id":"https:\/\/www.travisolson.net\/code\/#\/schema\/person\/0170ab5b2e7a3d88436375cc50336934"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.travisolson.net\/code\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.travisolson.net\/code\/#\/schema\/person\/0170ab5b2e7a3d88436375cc50336934","name":"Travis Olson","logo":{"@id":"https:\/\/www.travisolson.net\/code\/#\/schema\/person\/image\/"},"url":"https:\/\/www.travisolson.net\/code\/author\/travis-olson\/"}]}},"_links":{"self":[{"href":"https:\/\/www.travisolson.net\/code\/wp-json\/wp\/v2\/posts\/32","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.travisolson.net\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.travisolson.net\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.travisolson.net\/code\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.travisolson.net\/code\/wp-json\/wp\/v2\/comments?post=32"}],"version-history":[{"count":1,"href":"https:\/\/www.travisolson.net\/code\/wp-json\/wp\/v2\/posts\/32\/revisions"}],"predecessor-version":[{"id":33,"href":"https:\/\/www.travisolson.net\/code\/wp-json\/wp\/v2\/posts\/32\/revisions\/33"}],"wp:attachment":[{"href":"https:\/\/www.travisolson.net\/code\/wp-json\/wp\/v2\/media?parent=32"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.travisolson.net\/code\/wp-json\/wp\/v2\/categories?post=32"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.travisolson.net\/code\/wp-json\/wp\/v2\/tags?post=32"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}