Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // MySQL to XLS (Excel)
- // Author : Marvic R. Macalintal
- // September 14, 2018
- // Basic Usage
- mysql_to_xls( "reports.xls" );
- function mysql_to_xls( $filename ) {
- // Replace with your database credential
- $db_host = 'localhost';
- $db_username = 'db username';
- $db_password = 'db password';
- $db_name = 'db name';
- $tbl = 'table name';
- $con = mysqli_connect( "$db_host", "$db_username", "$db_password", "$db_name" );
- $sql = mysqli_query( $con, "SELECT * FROM $tbl" );
- while( $row = mysqli_fetch_assoc( $sql ) ) {
- $result[] = $row;
- }
- // ------------------------------------
- // Process starts here... do not modify
- $headers = '';
- $content = '';
- header("Content-Type: application/xls");
- header("Content-Disposition: attachment; filename=$filename");
- // Headers
- foreach( $result[0] as $key => $value ) {
- $headers .= $key . "\t";
- }
- // Content
- for( $i = 0; $i <= count( $result ) -1; $i++ ) {
- $content .= "\n";
- foreach( $result[$i] as $key => $value ) {
- $content .= $value . "\t";
- }
- }
- echo $headers;
- echo $content;
- }
- ?>
Add Comment
Please, Sign In to add comment