banner



How We Ca Download Data In Excel From Php Table

Exporting Table Information To Excel in PHP Tutorial

In this tutorial, nosotros will create a Export Tabular array Information As Excel using PHP. This code volition export your MySQLi data into a Microsoft Excel document. The lawmaking itself uses the header content office to translate the MySQLi data, then to be able to download equally an excel format. This is a user-friendly program experience costless to modify and apply it in your system.

We will exist using PHP equally a scripting linguistic communication that interprets in the web server such as XAMPP, WAMP, etc. Information technology is widely used past modernistic website applications to handle and protect user confidential data.

Getting Started:

Start you have to download & install XAMPP or any local server that run PHP scripts. Here'southward the link for XAMPP server https://www.apachefriends.org/alphabetize.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 blueprint https://getbootstrap.com/.

Creating Database

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

tut1

Or, you tin 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 up your any kind of text editor(notepad++, etc..). And 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 grade for our application. To create the forms only copy and write information technology into your text editor, then save it every bit alphabetize.php.

              
  1. <!DOCTYPE html>

  2. <html lang="en">

  3. <head>

  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-calibration=one"/>

  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />

  6. </head>

  7. <body>

  8. <nav form="navbar navbar-default">

  9. <div course="container-fluid">

  10. <a grade="navbar-make" href="https://sourcecodester.com">Sourcecodester</a>

  11. </div>

  12. </nav>

  13. <div course="col-dr.-3"></div>

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

  15. <h3 form="text-primary">PHP - Consign Tabular array Information Equally Excel</h3>

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

  17. <button type="button" grade="btn btn-success" data-toggle="modal" data-target="#form_modal"><bridge class="glyphicon glyphicon-plus"></bridge> 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</thursday>

  24. <thursday>Year</thursday>

  25. <th>Section</thursday>

  26. </tr>

  27. </thead>

  28. <tbody>

  29. <?php

  30. crave 'conn.php' ;

  31. ?>

  32. <tr>

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

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

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

  36. <td><?php repeat $fetch [ 'department' ] ?></td>

  37. </tr>

  38. <?php

  39. }

  40. ?>

  41. </tbody>

  42. <tfoot>

  43. <tr>

  44. <td><a class="btn btn-info" href="export_excel.php">Salve 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="truthful">

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

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

  55. <form method="Mail service" activeness="save_student.php">

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

  57. <h3 course="modal-title">Add Educatee</h3>

  58. </div>

  59. <div course="modal-trunk">

  60. <div class="col-dr.-two"></div>

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

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

  63. <label>Firstname</label>

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

  65. </div>

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

  67. <label>Lastname</label>

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

  69. </div>

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

  71. <label>Year</label>

  72. <select proper noun="year" course="form-command" required="required">

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

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

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

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

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

  78. </select>

  79. </div>

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

  81. <label>Department</label>

  82. <select name="section" course="form-command" required="required">

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

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

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

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

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

  88. </select>

  89. </div>

  90. </div>

  91. </div>

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

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

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

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

  96. </div>

  97. </form>

  98. </div>

  99. </div>

  100. </div>

  101. <script src="js/jquery-three.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 awarding. This lawmaking volition shop the student information to the MySQLi database server. To practise that only copy and write this block of codes inside the text editor, then save information technology every bit 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', '$yr', '$section')" ) or die ( mysqli_error ( ) ) ;

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

  10. }

  11. ?>

Creating the Main Function

This code contains the master function of the awarding. This code will convert your html tabular array into a readable excel file when the button is clicked. To make this only copy and write these block of codes beneath inside the text editor, and then relieve 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>Beginning Name</th>

  13. <th>Last Name</th>

  14. <th>Year</th>

  15. <th>Section</thursday>

  16. </tr>

  17. <tbody>

  18. " ;

  19. $query = $conn -> query ( "SELECT * FROM `student`" ) or dice ( 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 promise that this simple tutorial helps you to what you are looking for. For more than updates and tutorials just kindly visit this site.

Relish Coding!

How We Ca Download Data In Excel From Php Table,

Source: https://www.sourcecodester.com/tutorials/php/13102/php-export-table-data-excel.html

Posted by: morrishisems.blogspot.com

0 Response to "How We Ca Download Data In Excel From Php Table"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel