Skip to content

Convert database table to excel sheet. 🐘

License

Notifications You must be signed in to change notification settings

91ahmed/DbtoExcel

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

17 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

DbtoExcel

PHP script to simplify the process of converting database table to excel sheet.

# Requirements

  • PHP 7.0 or higher
  • Composer for installation

# Install

via composer

composer require exceldb/dbtoexcel

# Example

// don't forget to import vendor/autoload.php
require('vendor/autoload.php'); 

// Database information
$extract = new Database\Excel\ExtractExcel([
      'driver' => 'mysql',
      'host' => '127.0.0.1',
      'username' => 'root',
      'password' => '',
      'database' => 'test',
      'port' => '3306',
      'charset' => 'utf8',
      'sslmode' => 'disable'
]);

$extract->table('users'); // Specify the database table.
$extract->columns('*'); // Specify the columns you need to extract.
$extract->execute(); // Execute the script.

# Description

The TableToExcel class constructor takes one parameter, which is an (array) of the database information.

table()

This method used to specify the database table you need to extract, it takes one parameter (string) which is the name of the table.

columns()

Used to specify the columns you need to extract from the database table, it takes one parameter (string) which is the column's names.

To extract all columns just add asterisk (*) or leave the parameter empty while if you want to extract specific columns you can write the columns names following by comma separator.. see the example below.

use ExtractDatabaseToExcel\TableToExcel;

$extract = new TableToExcel([
      'driver' => 'mysql',
      'host' => '127.0.0.1',
      'username' => 'root',
      'password' => '',
      'database' => 'test',
      'port' => '3306',
      'charset' => 'utf8'
]);

$extract->table('users');
$extract->columns('username, email, age'); // Extract specific columns
$extract->execute();

execute()

This is the last method you should invoke to execute the script, the method will convert the data and download the excel file.