How would you export data in PHP to excel?

Submitted by razormist on Tuesday, September 21, 2021 - 16:50.

How would you export data in PHP to excel?

In this tutorial, we will create a Export Table Data As Excel using PHP. This code will export your MySQLi data into a Microsoft Excel document. The code itself uses the header content function to translate the MySQLi data, then to be able to download as an excel format. This is a user-friendly program feel free to modify and use it in your system.

We will be using PHP as a scripting language that interprets in the web server such as XAMPP, WAMP, etc. It is widely used by modern website applications to handle and protect user confidential information.

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html.

And this is the link for the jquery that i used in this tutorial https://jquery.com/.

Lastly, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating Database

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

How would you export data in PHP to excel?

Or, you can simply copy/paste the SQL script below in your PHPMyAdmin SQL Page to create our database table and its column. To do this, navigate your database in PHpMyadmin to the SQL Tab. Then, paste SQL script in the provided text area and click the "Go" button.

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_excel");

  3. if(!$conn){

  4. die("Error: Failed to connect to database!");

  5. }

  6. ?>

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="https://sourcecodester.com">Sourcecodester</a>

  11. </div>

  12. </nav>

  13. <div class="col-md-3"></div>

  14. <div class="col-md-6 well">

  15. <h3 class="text-primary">PHP - Export Table Data As Excel</h3>

  16. <hr style="border-top:1px dotted #ccc;"/>

  17. <button type="button" class="btn btn-success" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add student</button>

  18. <br /><br />

  19. <table class="table table-bordered">

  20. <thead class="alert-info">

  21. <tr>

  22. <th>Firstname</th>

  23. <th>Lastname</th>

  24. <th>Year</th>

  25. <th>Section</th>

  26. </tr>

  27. </thead>

  28. <tbody>

  29. <?php

  30. require 'conn.php';

  31. ?>

  32. <tr>

  33. <td><?php echo $fetch['firstname']?></td>

  34. <td><?php echo $fetch['lastname']?></td>

  35. <td><?php echo $fetch['year']?></td>

  36. <td><?php echo $fetch['section']?></td>

  37. </tr>

  38. <?php

  39. }

  40. ?>

  41. </tbody>

  42. <tfoot>

  43. <tr>

  44. <td><a class="btn btn-info" href="export_excel.php">Save as Excel</a></td>

  45. <td></td>

  46. <td></td>

  47. <td></td>

  48. </tr>

  49. </tfoot>

  50. </table>

  51. </div>

  52. <div class="modal fade" id="form_modal" aria-hidden="true">

  53. <div class="modal-dialog">

  54. <div class="modal-content">

  55. <form method="POST" action="save_student.php">

  56. <div class="modal-header">

  57. <h3 class="modal-title">Add Student</h3>

  58. </div>

  59. <div class="modal-body">

  60. <div class="col-md-2"></div>

  61. <div class="col-md-8">

  62. <div class="form-group">

  63. <label>Firstname</label>

  64. <input type="text" name="firstname" class="form-control" required="required"/>

  65. </div>

  66. <div class="form-group">

  67. <label>Lastname</label>

  68. <input type="text" name="lastname" class="form-control" required="required"/>

  69. </div>

  70. <div class="form-group">

  71. <label>Year</label>

  72. <select name="year" class="form-control" required="required">

  73. <option value=""></option>

  74. <option value="I">I</option>

  75. <option value="II">II</option>

  76. <option value="III">III</option>

  77. <option value="IV">IV</option>

  78. </select>

  79. </div>

  80. <div class="form-group">

  81. <label>Section</label>

  82. <select name="section" class="form-control" required="required">

  83. <option value=""></option>

  84. <option value="A">A</option>

  85. <option value="B">B</option>

  86. <option value="C">C</option>

  87. <option value="D">D</option>

  88. </select>

  89. </div>

  90. </div>

  91. </div>

  92. <br style="clear:both;"/>

  93. <div class="modal-footer">

  94. <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>

  95. <button class="btn btn-primary" name="save"><span class="glyphicon glyphicon-save"></span> Save</button>

  96. </div>

  97. </form>

  98. </div>

  99. </div>

  100. </div>

  101. <script src="js/jquery-3.2.1.min.js"></script>

  102. <script src="js/bootstrap.js"></script>

  103. </body>

  104. </html>

Creating PHP Query

This code contains the php query of the application. This code will store the student information to the MySQLi database 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';

  3. if(ISSET($_POST['save'])){

  4. $firstname = $_POST['firstname'];

  5. $lastname = $_POST['lastname'];

  6. $year = $_POST['year'];

  7. $section = $_POST['section'];

  8. mysqli_query($conn, "INSERT INTO `student` VALUES('', '$firstname', '$lastname', '$year', '$section')") or die(mysqli_error());

  9. header("location: index.php");

  10. }

  11. ?>

Creating the Main Function

This code contains the main function of the application. This code will convert your html table into a readable excel file 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 export_excel.php

  1. <?php

  2. header("Content-Type: application/xls");

  3. header("Content-Disposition: attachment; filename=student_list.xls");

  4. header("Pragma: no-cache");

  5. require_once 'conn.php';

  6. $output = "";

  7. $output .="

  8. <table>

  9. <thead>

  10. <tr>

  11. <th>Student ID</th>

  12. <th>First Name</th>

  13. <th>Last Name</th>

  14. <th>Year</th>

  15. <th>Section</th>

  16. </tr>

  17. <tbody>

  18. ";

  19. $query = $conn->query("SELECT * FROM `student`") or die(mysqli_errno());

  20. while($fetch = $query->fetch_array()){

  21. $output .= "

  22. <tr>

  23. <td>".$fetch['stud_id']."</td>

  24. <td>".$fetch['firstname']."</td>

  25. <td>".$fetch['lastname']."</td>

  26. <td>".$fetch['year']."</td>

  27. <td>".$fetch['section']."</td>

  28. </tr>

  29. ";

  30. }

  31. $output .="

  32. </tbody>

  33. </table>

  34. ";

  35. echo $output;

  36. ?>

DEMO Video

There you have it we successfully created Export Table Data As Excel using PHP. I hope that this simple tutorial helps you to what you are looking for. For more updates and tutorials just kindly visit this site.

Enjoy Coding!

  • 10719 views

How do I export Xlsx from PHP?

EasyXLS on Linux, Mac, Windows using Java with PHP.
Step 1: Download and install EasyXLS Excel Library for Java. To download the trial version of EasyXLS Excel Library, press the below button: ... .
Step 2: Install PHP/Java Bridge. ... .
Step 3: Setup EasyXLS library in Tomcat. ... .
Step 4: Run PHP code that exports data to Excel XLSX file..

How do I export a PHP file?

php file handles the data export and CSV file download process using PHP and MySQL..
Retrieve data from the MySQL database..
Create a file pointer using fopen() function..
Specify the header columns and put data into the CSV file..
Output each row of the data, format line as CSV, and write to file pointer..

Can PHP write to an Excel file?

PHP provides a library to deal with Excel files. It is called PHP Excel library. It enables you to read and write spreadsheets in various formats including csv, xls, ods, and xlsx. You will need to ensure that you have PHP's upgraded version not older than PHP 5.2 .

How does PHP store form data in Excel?

Create a PHP script code to collect information and convert into CSV(Excel) file.
First of set form fields on the button using if condition. ... .
Set fields for excel file(CSV format) ..
User header function and define the content type..
At the last fill the form and click on the button..