{"id":5794,"date":"2019-02-12T05:29:09","date_gmt":"2019-02-12T05:29:09","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=5794"},"modified":"2022-04-13T15:56:37","modified_gmt":"2022-04-13T15:56:37","slug":"how-to-fetch-data-in-excel-or-generate-excel-file-in-php","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/how-to-fetch-data-in-excel-or-generate-excel-file-in-php\/","title":{"rendered":"How to fetch data in excel or generate excel file in PHP"},"content":{"rendered":"<p>File structure for this tutorial :<\/p>\n<ol>\n<li>config.php (database connection file)<\/li>\n<li>index.php (For fetching data )<\/li>\n<li>genrate-excel.php (For genrating excel file )<\/li>\n<\/ol>\n<p>Create a sql table tblemployee.<br>\nStructure of sql table employe .<\/p>\n<p>[code language=&#8221;sql&#8221;]<br>\nCREATE TABLE `tblemploye` (<br>\n  `id` int(11) NOT NULL,<br>\n  `fullName` varchar(120) NOT NULL,<br>\n  `emailId` varchar(150) NOT NULL,<br>\n  `phoneNumber` int(11) NOT NULL,<br>\n  `department` varchar(100) NOT NULL,<br>\n  `joiningDate` varchar(100) NOT NULL<br>\n) ENGINE=InnoDB DEFAULT CHARSET=latin1;<br>\nALTER TABLE `tblemploye`<br>\n  ADD PRIMARY KEY (`id`);<br>\nALTER TABLE `tblemploye`<br>\n  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5;<br>\nCOMMIT;<br>\n[\/code]<\/p>\n<p>Now insert some data into this table.<br>\n[code language=&#8221;sql&#8221;]<br>\nINSERT INTO `tblemploye` (`id`, `fullName`, `emailId`, `phoneNumber`, `department`, `joiningDate`) VALUES<br>\n(1, &#8216;Amardeep Dubey&#8217;, &#8216;ad@gmail.com&#8217;, 1234567890, &#8216;IT&#8217;, &#8216;2018-05-01&#8217;),<br>\n(2, &#8216;Chandan Kumar&#8217;, &#8216;ck@gmail.com&#8217;, 45455454, &#8216;Director&#8217;, &#8216;2017-08-12&#8217;),<br>\n(3, &#8216;Mantosh Singh&#8217;, &#8216;ms@gmail.com&#8217;, 23423423, &#8216;Account&#8217;, &#8216;2016-10-01&#8217;),<br>\n(4, &#8216;Bittu Kumar&#8217;, &#8216;bittu@gmail.com&#8217;, 834856384, &#8216;SEO&#8217;, &#8216;2017-12-01&#8217;);<br>\n(5, &#8216;Vikash Verma&#8217;, &#8216;vv@gmail.com&#8217;, 1234567890, &#8216;SEO&#8217;, &#8216;2018-05-01&#8217;),<br>\n(6, &#8216;Narayan Rajak&#8217;, &#8216;ck@gmail.com&#8217;, 45455454, &#8216;IT&#8217;, &#8216;2017-08-12&#8217;),<br>\n[\/code]<\/p>\n<p><strong>Index.php<\/strong> in this file we will  read the data from database.<br>\n[code language=&#8221;php&#8221;]<br>\n&lt;?php<br>\n$query=mysqli_query($con,&#8221;select * from tblemploye&#8221;);<br>\n$cnt=1;<br>\nwhile ($row=mysqli_fetch_array($query)) {<br>\n?&gt;<br>\n  &lt;tr&gt;<br>\n                &lt;td&gt;&lt;?php echo $cnt;  ?&gt;&lt;\/td&gt;<br>\n                &lt;td&gt;&lt;?php echo $row[&#8216;fullName&#8217;]?&gt;&lt;\/td&gt;<br>\n                &lt;td&gt;&lt;?php echo $row[&#8217;emailId&#8217;]?&gt;&lt;\/td&gt;<br>\n                &lt;td&gt;&lt;?php echo $row[&#8216;phoneNumber&#8217;]?&gt;&lt;\/td&gt;<br>\n                &lt;td&gt;&lt;?php echo $row[&#8216;department&#8217;]?&gt;&lt;\/td&gt;<br>\n                &lt;td&gt;&lt;?php echo $row[&#8216;joiningDate&#8217;]?&gt;&lt;\/td&gt;<br>\n            &lt;\/tr&gt;<br>\n&lt;?php<br>\n$cnt++;<br>\n} ?&gt;<br>\n&lt;\/table&gt;<br>\n[\/code]<br>\n<strong>genrate-excel.php<\/strong> in this file we will fetch the data from database and generate excel file for the same data.<br>\n[code language=&#8221;php&#8221;]<br>\n&lt;?php<br>\n\/\/ Database Connection file<br>\ninclude(&#8216;config.php&#8217;);<br>\n?&gt;<br>\n&lt;table border=&#8221;1&#8243;&gt;<br>\n&lt;thead&gt;<br>\n&lt;tr&gt;<br>\n&lt;th&gt;Sr.&lt;\/th&gt;<br>\n&lt;th&gt;Name&lt;\/th&gt;<br>\n&lt;th&gt;Email id&lt;\/th&gt;<br>\n&lt;th&gt;Phone Number&lt;\/th&gt;<br>\n&lt;th&gt;Department&lt;\/th&gt;<br>\n&lt;th&gt;Joining Date&lt;\/th&gt;<br>\n&lt;\/tr&gt;<br>\n&lt;\/thead&gt;<br>\n&lt;?php<br>\n\/\/ File name<br>\n$filename=&#8221;EmpData&#8221;;<br>\n\/\/ Fetching data from data base<br>\n$query=mysqli_query($con,&#8221;select * from tblemploye&#8221;);<br>\n$cnt=1;<br>\nwhile ($row=mysqli_fetch_array($query)) {<br>\n?&gt;<br>\n            &lt;tr&gt;<br>\n                &lt;td&gt;&lt;?php echo $cnt;  ?&gt;&lt;\/td&gt;<br>\n                &lt;td&gt;&lt;?php echo $row[&#8216;fullName&#8217;];?&gt;&lt;\/td&gt;<br>\n                &lt;td&gt;&lt;?php echo $row[&#8217;emailId&#8217;];?&gt;&lt;\/td&gt;<br>\n                &lt;td&gt;&lt;?php echo $row[&#8216;phoneNumber&#8217;];?&gt;&lt;\/td&gt;<br>\n                &lt;td&gt;&lt;?php echo $row[&#8216;department&#8217;];?&gt;&lt;\/td&gt;<br>\n                &lt;td&gt;&lt;?php echo $row[&#8216;joiningDate&#8217;];?&gt;&lt;\/td&gt;<br>\n            &lt;\/tr&gt;<br>\n&lt;?php<br>\n$cnt++;<br>\n\/\/ Genrating Execel  filess<br>\nheader(&#8220;Content-type: application\/octet-stream&#8221;);<br>\nheader(&#8220;Content-Disposition: attachment; filename=&#8221;.$filename.&#8221;-Report.xls&#8221;);<br>\nheader(&#8220;Pragma: no-cache&#8221;);<br>\nheader(&#8220;Expires: 0&#8221;);<br>\n} ?&gt;<br>\n&lt;\/table&gt;<br>\n[\/code]<\/p>\n<p><strong>header(\u201cContent-type: application\/octet-stream\u201d);<\/strong><br>\n<strong>header(\u201cContent-Disposition: attachment; filename=\u201d.$filename.\u201d-Report.xls\u201d);<\/strong><br>\nThe content-type should be whatever it is known to be, if you know it. application\/octet-stream is defined as \u201carbitrary binary data\u201d in RFC 2046\u2033.<br>\nMeans \u201cI don\u2019t know what the hell this is. Please save it as a file, preferably named $filename.\u201d-Report.xls\u201d.<br>\n<strong>header(\u201cPragma: no-cache\u201d);<\/strong><br>\ncache-control is the HTTP\/1.1 implementation . cache-control  used to prevent the client from caching the response.<br>\n<strong>header(\u201cExpires: 0\u201d);<\/strong><br>\nThe Expires header specifies when content will expire, or how long content is \u201cfresh.\u201d After this time, the portal server will always check back with the remote server to see if the content has changed.<br>\nExpires: 0<br>\nThe value 0 indicates that the content expires immediately and would have to be re-requested before being displayed again.<\/p>\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_23540\"><iframe loading=\"lazy\"  id=\"_ytid_92779\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_23540\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>File structure for this tutorial : config.php (database connection file) index.php (For fetching data ) genrate-excel.php (For genrating excel file ) Create a sql table tblemployee. Structure of sql table&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[5150],"tags":[5193,5194],"class_list":["post-5794","post","type-post","status-publish","format-standard","hentry","category-php","tag-generate-excel-file-in-php","tag-how-to-fetch-data-in-excel"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/5794","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=5794"}],"version-history":[{"count":3,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/5794\/revisions"}],"predecessor-version":[{"id":25551,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/5794\/revisions\/25551"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=5794"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=5794"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=5794"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}