This tutorial will learn you How to export Mysql data from web application to Excel file using PHP programming language. This functionality is mostly required in enterprise level web application. There are lots of data are transfer on daily basis and manage that into separate excel file. So, at that time this type of functionality is required in web application. This functionality reduce lots of time to take data into excel file.
In this simple post we have learn something regarding how to export data to Excel in PHP. If you have developed any project then that project you have to required this functionality like Exporting Data to Excel Sheet. So we have developed this tutorial, in which we have make simple PHP Script for Export Data from Web to Excel.
Online Demo
Export MySQL data to Excel in PHP
Name | Address | City | Postal Code | Country |
---|---|---|---|---|
Maria Anders | Obere Str. 57 | Berlin | 12209 | Germany |
Ana Trujillo | Avda. de la Construction 2222 | Mexico D.F. | 5021 | Mexico |
Antonio Moreno | Mataderos 2312 | Mexico D.F. | 5023 | Mexico |
Thomas Hardy | 120 Hanover Sq. | London | WA1 1DP | UK |
Paula Parente | Rua do Mercado, 12 | Resende | 08737-363 | Brazil |
Wolski Zbyszek | ul. Filtrowa 68 | Walla | 01-012 | Poland |
Matti Karttunen | Keskuskatu 45 | Helsinki | 21240 | Finland |
Karl Jablonski | 305 - 14th Ave. S. Suite 3B | Seattle | 98128 | USA |
Paula Parente | Rua do Mercado, 12 | Resende | 08737-363 | Brazil |
Source Code
<?php
$connect = mysqli_connect("localhost", "root", "", "testing");
$sql = "SELECT * FROM tbl_customer";
$result = mysqli_query($connect, $sql);
?>
<html>
<head>
<title>Export MySQL data to Excel in PHP</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
</head>
<body>
<div class="container">
<br />
<br />
<br />
<div class="table-responsive">
<h2 align="center">Export MySQL data to Excel in PHP</h2><br />
<table class="table table-bordered">
<tr>
<th>Name</th>
<th>Address</th>
<th>City</th>
<th>Postal Code</th>
<th>Country</th>
</tr>
<?php
while($row = mysqli_fetch_array($result))
{
echo '
<tr>
<td>'.$row["CustomerName"].'</td>
<td>'.$row["Address"].'</td>
<td>'.$row["City"].'</td>
<td>'.$row["PostalCode"].'</td>
<td>'.$row["Country"].'</td>
</tr>
';
}
?>
</table>
<br />
<form method="post" action="export.php">
<input type="submit" name="export" class="btn btn-success" value="Export" />
</form>
</div>
</div>
</body>
</html>
export.php
<?php
//export.php
$connect = mysqli_connect("localhost", "root", "", "testing");
$output = '';
if(isset($_POST["export"]))
{
$query = "SELECT * FROM tbl_customer";
$result = mysqli_query($connect, $query);
if(mysqli_num_rows($result) > 0)
{
$output .= '
<table class="table" bordered="1">
<tr>
<th>Name</th>
<th>Address</th>
<th>City</th>
<th>Postal Code</th>
<th>Country</th>
</tr>
';
while($row = mysqli_fetch_array($result))
{
$output .= '
<tr>
<td>'.$row["CustomerName"].'</td>
<td>'.$row["Address"].'</td>
<td>'.$row["City"].'</td>
<td>'.$row["PostalCode"].'</td>
<td>'.$row["Country"].'</td>
</tr>
';
}
$output .= '</table>';
header('Content-Type: application/xls');
header('Content-Disposition: attachment; filename=download.xls');
echo $output;
}
}
?>
Hi!
ReplyDeletei'd like to THANK YOU, i've been looking for an answer like this a long time ago!!
i have a problem, hope you can help me...
i use the whole code, everything you posted, but when i am going to open it says that the format doesnt match the filetype... hope you can help me.
regards!
me too...
DeleteWarning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\alp_deneme\kontrol\excelindir.php:1) in C:\xampp\htdocs\alp_deneme\kontrol\excelindir.php on line 34
DeleteWarning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\alp_deneme\kontrol\excelindir.php:1) in C:\xampp\htdocs\alp_deneme\kontrol\excelindir.php on line 35
Superb !!!!!!
ReplyDeleteNice one. You can check more tutorials here
ReplyDeletehttp://skillinfinity.com/blog/topics/php/library
Thanks - just what i needed!
ReplyDeleteHi Thanks for the tutorial can you please tell me how we can format the table like changing color of the table
ReplyDeletegive color inside the table...its like inline style in css ex: <table style=" background-color: white>;
Deletehow can we change the extension of the file.
ReplyDeleteHey Thanks for sharing. When I export to excel.php I just get a blank page. It doesn't download, copied code just as you haveit on the page.
ReplyDeleteif your code goes like this ex. -> $row['example'] use this instead $row["example"].
Deletei have used your code but the data is not getting displayed.but in download file data is there
ReplyDeleteexcellent Code.... Thanks
ReplyDeleteI found it easy to understand and useful, thanks
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteGreat...Thankyou Very much
ReplyDeletethanks great. i want to know how to export in pdf
ReplyDelete<3
ReplyDeletethanks great code, one thing I am facing, if the address is in 2 or more line, can the exported data will come in single cell?
ReplyDeleteHi,
ReplyDeleteWhen i tried your code i get this Error : Allowed memory size of 134217728 bytes exhausted (tried to allocate 133693506 bytes).
Can someone help please ?
Nice Tutorial, Thank you
ReplyDeleteI also want know how to export in pdf
it works! but no border in excel :(
ReplyDeleteNot working!!!
ReplyDeleteThanks Sir, it helped me a lot, could you please tell me how to import excel data to database table
ReplyDeleteI have a problem,downloaded excel sheet has white background and no grid.could anyone suggest me how to ressolve this issue?
ReplyDeleteCould anyone suggest?
ReplyDeleteWhen i dowload excel then it shows white backgroud and no grid lines in excel.how can i remove those issue.
Hello how can i download excel file using ajax in code-igniter
ReplyDeletecool its working
ReplyDeleteThats great perfectly working for me thanks lot
ReplyDeleteGood Site,,Thanks guysm,you are doing a great job
ReplyDeleteWhen ever I am trying to open csv file I got a warning: "The file you are trying to open download.csv, is in different format than specified bt the file extension. verify that the file is not corrupted and is from a trusted source before opening the file." Please help me in this regard.
ReplyDeleteits very easy and useful
ReplyDeletegreat
thanks very helpfull
ReplyDeleteThe file type and its extension has a mismatch, pls tell us what is the file type which is downloaded
ReplyDeleteHi, script is not working for me, I get "header already send error.
ReplyDeleteWhat can I do?
This is the error:
Warning: Cannot modify header information - headers already sent by (output started at /customers/c/4/e/dansclubvarya.be/httpd.www/Ledenadministratie/export.php:1) in /customers/c/4/e/dansclubvarya.be/httpd.www/Ledenadministratie/export.php on line 61 Warning: Cannot modify header information - headers already sent by (output started at /customers/c/4/e/dansclubvarya.be/httpd.www/Ledenadministratie/export.php:1) in /customers/c/4/e/dansclubvarya.be/httpd.www/Ledenadministratie/export.php on line 62
Thanks
ReplyDeletethanks sir its perfectly working for me!!!! thanks
ReplyDeleteHello Good day, the code works perfectly thanks. I would just like to ask something; what if we try and add a search function and data reflected by the search would be the ones printed into the excel file. how do we do it?
ReplyDeleteHow can i make this auto? Using cronjob? any ideia?
ReplyDeleteWorked very well for me, thank you ;)
ReplyDeleteit's very useful, it works immediately, thanks so much
ReplyDeleteThis is quite wonderful. You saved a soul. As a developer, I just needed to be very sure of this:
ReplyDeleteheader('Content-Type: application/xls');
header('Content-Disposition: attachment; filename=download.xls');
echo $output;
and its workability. Wow, It worked. Bravo!
Also I wish to ask, is there a simpler way to do pdf exports of datatable contents?
ReplyDeletehow to export search data result in excel ?
ReplyDeletehow to export search data result in excel ?
ReplyDeleteHeeeeey very nice code but when it download the file into it show this message
ReplyDeleteNotice: Undefined index: telefono in public_html/qr/admin/export.php on line 26
Notice: Undefined index: telefono in public_html/qr/admin/export.php on line 26
Notice: Undefined index: telefono in public_html/qr/admin/export.php on line 26
Into the excel file... SO If can read it could be very helpful
SUPER AWESOME , Thank you
ReplyDeletei have a problem, hope you can help me...
ReplyDeletei use the whole code, everything you posted, but when i am going to open it says that the format doesnt match the filetype... hope you can help me.
Thanks :) It working fine. BTW, is there any opportunity to download the file as .xlsx format.
ReplyDeleteThanks
Thanks, more blessing !!!
ReplyDeletethanks it really good
ReplyDeleteit not working my code has below only display table not CREATE EXCEL FILE
ReplyDeleteif(isset($_POST["export"]))
{
if($results1 > 0)
{
$output .= '
foreach ($results1 as $key => $value) {
$output .= '
$value->rclient_name.$value->email.$value->health_condition.$value->changes.$value->image';
}
$output .= '';
header('Content-Type: application/xls');
header('Content-Disposition: attachment; filename=download.xls');
echo $output;
}
}
thank you so much
ReplyDeletethanks a lot.
ReplyDeletehi thanks a lot it works so good on localhost but when I run it on a real server , it didn't work
ReplyDeleteGreat, Thank you so much. it works 100%
ReplyDeleteCan I export 2 sql tables data in a single excel file ?
ReplyDeletethank you
ReplyDeleteIts great, it is working
ReplyDeleteNice one its working perfectly
ReplyDeleteThanks for this wonderful piece of code which is easy to understand. I am able to download the data but the excel sheet has semicolons in first 100 rows followed by the table and the grid view is missing
ReplyDeletethank's simple way....export excel
ReplyDeleteIt is working in XAMPP but not working in live deploy. Any changes to be made. Give me some idea.
ReplyDeleteThanks in advance
Hi, exported excel file and open file with message "the file format and extension of 'download.xls ' don't match.
ReplyDeletewhy can't open as *.xlsx?
ReplyDelete