PHP - Create Excel File From MySQLi Table

Submitted by: 
Visitors have accessed this post 327 times.

In this tutorial we will create a Create Excel File From MySQLi Table using PHP. This code can export your MySQLi table as excel file when user click the export button. The code use header() a php built-in function that can control data sent to the browser by the Web server by customizing the output content. This a user-friendly program feel free to modify and use it to your system.

We will be using PHP as a scripting language and interpreter that is used primarily on any webserver including xamp, wamp, etc. It is being use to any famous websites because of modern approach as its today.

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server

And, this is the link for the bootstrap that i used for the layout design

Creating Database

Open your database web server then create a database name in it db_create_excel, after that click Import then locate the database file inside the folder of the application then click ok.

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.

  1. <?php
  2. $conn = mysqli_connect('localhost', 'root', '', 'db_create_excel');
  4. if(!$conn){
  5. die("Error: Failed to connect to database!");
  6. }
  7. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="<a href="">Sourcecodester</a>
  11. " rel="nofollow">">Sourcecodester</a>
  12. </a> </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">PHP - Create Excel File From MySQLi Table</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <div class="col-md-4">
  19. <form action="save.php" method="POST">
  20. <div class="form-group">
  21. <label>Firstname</label>
  22. <input type="text" class="form-control" name="firstname" required="required"/>
  23. </div>
  24. <div class="form-group">
  25. <label>Lastname</label>
  26. <input type="text" class="form-control" name="lastname" required="required"/>
  27. </div>
  28. <div class="form-group">
  29. <label>Address</label>
  30. <input type="text" class="form-control" name="address" required="required"/>
  31. </div>
  32. <center><button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button></center>
  33. </form>
  34. </div>
  35. <div class="col-md-8">
  36. <form method="POST" action="create_excel.php">
  37. <button class="btn btn-success pull-right" name="export"><span class="glyphicon glyphicon-print"></span> Export Excel</button>
  38. </form>
  39. <br /><br />
  40. <table class="table table-bordered">
  41. <thead class="alert-info">
  42. <tr>
  43. <th>Firstname</th>
  44. <th>Lastname</th>
  45. <th>Address</th>
  46. </tr>
  47. </thead>
  48. <tbody>
  49. <?php
  50. require 'conn.php';
  52. $query = mysqli_query($conn, "SELECT * FROM `member`");
  53. while($fetch = mysqli_fetch_array($query)){
  54. ?>
  55. <tr>
  56. <td><?php echo $fetch['firstname']?></td>
  57. <td><?php echo $fetch['lastname']?></td>
  58. <td><?php echo $fetch['address']?></td>
  59. </tr>
  60. <?php
  61. }
  62. ?>
  63. </tbody>
  64. </table>
  65. </div>
  66. </div>
  67. </body>
  68. </html>

Creating PHP Query

This code contains the php query of the application. This code will store the form inputs to the MySQLi server. To do that just copy and write this block of codes inside the text editor, then save it as save.php.

  1. <?php
  2. require_once 'conn.php';
  4. if(ISSET($_POST['save'])){
  5. $firstname = $_POST['firstname'];
  6. $lastname = $_POST['lastname'];
  7. $address = $_POST['address'];
  9. mysqli_query($conn, "INSERT INTO `member` VALUE('', '$firstname', '$lastname', '$address')") or die(mysqli_errno());
  10. header('location: index.php');
  12. }
  13. ?>

Creating the Main Function

This code contains the main function of the application. This code will export your MySQLi table as an excel when the button is clicked. To make this just copy and write these block of codes below inside the text editor, then save it as create_excel.php

  1. <?php
  2. header("Content-Type: application/xls");
  3. header("Content-Disposition: attachment; filename=excel.xls");
  4. header("Pragma: no-cache");
  5. header("Expires: 0");
  7. require_once 'conn.php';
  9. $output = "";
  11. if(ISSET($_POST['export'])){
  12. $output .="
  13. <table>
  14. <thead>
  15. <tr>
  16. <th>ID</th>
  17. <th>Firstname</th>
  18. <th>Lastname</th>
  19. <th>Address</th>
  20. </tr>
  21. <tbody>
  22. ";
  24. $query = mysqli_query($conn, "SELECT * FROM `member`") or die(mysqli_errno());
  25. while($fetch = mysqli_fetch_array($query)){
  27. $output .= "
  28. <tr>
  29. <td>".$fetch['mem_id']."</td>
  30. <td>".$fetch['firstname']."</td>
  31. <td>".$fetch['lastname']."</td>
  32. <td>".$fetch['address']."</td>
  33. </tr>
  34. ";
  35. }
  37. $output .="
  38. </tbody>
  40. </table>
  41. ";
  43. echo $output;
  44. }
  46. ?>

There you have it we successfully created Create Excel File From MySQLi Table using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.