Find the Best Cosmetic Hospitals

Explore trusted cosmetic hospitals and make a confident choice for your transformation.

“Invest in yourself — your confidence is always worth it.”

Explore Cosmetic Hospitals

Start your journey today — compare options in one place.

How to fetch data in excel or generate excel file in PHP

File structure for this tutorial :

  1. config.php (database connection file)
  2. index.php (For fetching data )
  3. genrate-excel.php (For genrating excel file )

Create a sql table tblemployee.
Structure of sql table employe .

[code language=”sql”]
CREATE TABLE `tblemploye` (
`id` int(11) NOT NULL,
`fullName` varchar(120) NOT NULL,
`emailId` varchar(150) NOT NULL,
`phoneNumber` int(11) NOT NULL,
`department` varchar(100) NOT NULL,
`joiningDate` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `tblemploye`
ADD PRIMARY KEY (`id`);
ALTER TABLE `tblemploye`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5;
COMMIT;
[/code]

Now insert some data into this table.
[code language=”sql”]
INSERT INTO `tblemploye` (`id`, `fullName`, `emailId`, `phoneNumber`, `department`, `joiningDate`) VALUES
(1, ‘Amardeep Dubey’, ‘ad@gmail.com’, 1234567890, ‘IT’, ‘2018-05-01’),
(2, ‘Chandan Kumar’, ‘ck@gmail.com’, 45455454, ‘Director’, ‘2017-08-12’),
(3, ‘Mantosh Singh’, ‘ms@gmail.com’, 23423423, ‘Account’, ‘2016-10-01’),
(4, ‘Bittu Kumar’, ‘bittu@gmail.com’, 834856384, ‘SEO’, ‘2017-12-01’);
(5, ‘Vikash Verma’, ‘vv@gmail.com’, 1234567890, ‘SEO’, ‘2018-05-01’),
(6, ‘Narayan Rajak’, ‘ck@gmail.com’, 45455454, ‘IT’, ‘2017-08-12’),
[/code]

Index.php in this file we will read the data from database.
[code language=”php”]
<?php
$query=mysqli_query($con,”select * from tblemploye”);
$cnt=1;
while ($row=mysqli_fetch_array($query)) {
?>
<tr>
<td><?php echo $cnt; ?></td>
<td><?php echo $row[‘fullName’]?></td>
<td><?php echo $row[’emailId’]?></td>
<td><?php echo $row[‘phoneNumber’]?></td>
<td><?php echo $row[‘department’]?></td>
<td><?php echo $row[‘joiningDate’]?></td>
</tr>
<?php
$cnt++;
} ?>
</table>
[/code]
genrate-excel.php in this file we will fetch the data from database and generate excel file for the same data.
[code language=”php”]
<?php
// Database Connection file
include(‘config.php’);
?>
<table border=”1″>
<thead>
<tr>
<th>Sr.</th>
<th>Name</th>
<th>Email id</th>
<th>Phone Number</th>
<th>Department</th>
<th>Joining Date</th>
</tr>
</thead>
<?php
// File name
$filename=”EmpData”;
// Fetching data from data base
$query=mysqli_query($con,”select * from tblemploye”);
$cnt=1;
while ($row=mysqli_fetch_array($query)) {
?>
<tr>
<td><?php echo $cnt; ?></td>
<td><?php echo $row[‘fullName’];?></td>
<td><?php echo $row[’emailId’];?></td>
<td><?php echo $row[‘phoneNumber’];?></td>
<td><?php echo $row[‘department’];?></td>
<td><?php echo $row[‘joiningDate’];?></td>
</tr>
<?php
$cnt++;
// Genrating Execel filess
header(“Content-type: application/octet-stream”);
header(“Content-Disposition: attachment; filename=”.$filename.”-Report.xls”);
header(“Pragma: no-cache”);
header(“Expires: 0”);
} ?>
</table>
[/code]

header(“Content-type: application/octet-stream”);
header(“Content-Disposition: attachment; filename=”.$filename.”-Report.xls”);
The content-type should be whatever it is known to be, if you know it. application/octet-stream is defined as “arbitrary binary data” in RFC 2046″.
Means “I don’t know what the hell this is. Please save it as a file, preferably named $filename.”-Report.xls”.
header(“Pragma: no-cache”);
cache-control is the HTTP/1.1 implementation . cache-control used to prevent the client from caching the response.
header(“Expires: 0”);
The Expires header specifies when content will expire, or how long content is “fresh.” After this time, the portal server will always check back with the remote server to see if the content has changed.
Expires: 0
The value 0 indicates that the content expires immediately and would have to be re-requested before being displayed again.

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals
I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I have worked at <a href="https://www.cotocus.com/">Cotocus</a>. I share tech blog at <a href="https://www.devopsschool.com/">DevOps School</a>, travel stories at <a href="https://www.holidaylandmark.com/">Holiday Landmark</a>, stock market tips at <a href="https://www.stocksmantra.in/">Stocks Mantra</a>, health and fitness guidance at <a href="https://www.mymedicplus.com/">My Medic Plus</a>, product reviews at <a href="https://www.truereviewnow.com/">TrueReviewNow</a> , and SEO strategies at <a href="https://www.wizbrand.com/">Wizbrand.</a> Do you want to learn <a href="https://www.quantumuting.com/">Quantum Computing</a>? <strong>Please find my social handles as below;</strong> <a href="https://www.rajeshkumar.xyz/">Rajesh Kumar Personal Website</a> <a href="https://www.youtube.com/TheDevOpsSchool">Rajesh Kumar at YOUTUBE</a> <a href="https://www.instagram.com/rajeshkumarin">Rajesh Kumar at INSTAGRAM</a> <a href="https://x.com/RajeshKumarIn">Rajesh Kumar at X</a> <a href="https://www.facebook.com/RajeshKumarLog">Rajesh Kumar at FACEBOOK</a> <a href="https://www.linkedin.com/in/rajeshkumarin/">Rajesh Kumar at LINKEDIN</a> <a href="https://www.wizbrand.com/rajeshkumar">Rajesh Kumar at WIZBRAND</a> <a href="https://www.rajeshkumar.xyz/dailylogs">Rajesh Kumar DailyLogs</a>

Related Posts

What is CodeIgniter and use cases of CodeIgniter?

What is CodeIgniter? CodeIgniter is an open-source PHP web application framework designed to simplify and accelerate web development. It follows the Model-View-Controller (MVC) architectural pattern and provides…

Read More

What is PHP and use cases of PHP?

What is PHP? Are you wondering what PHP is and how it works? Look no further! In this article, we will explore the ins and outs of…

Read More

Complete guide on PHP certification courses, tutorials & training

What is PHP Hypertext Preprocessor is known as PHP. Many developers use PHP, an open-source server-side programming language, to create websites. In addition, it is a general-purpose…

Read More

Complete PHP with Laravel Certification Guide & tutorials

What is PHP with Laravel? PHP was visioned eventually in the fall of 1994 by Rasmus Lerdorf. PHP means Hypertext Preprocessor. It’s a extensively- used, open source…

Read More

Top 50 PHP Interview Questions and Answers

1) What is PHP? PHP is an open-source, interpreted, and object-oriented scripting language that can be executed at the server-side. PHP is well suited for web development….

Read More

Complete guide of PHP certification courses, tutorials & training

PHP is a great beginner programming language for anyone who is wanting to cross over into the coding world. Though easy to learn, it is an extremely…

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x