All Projects → sujancse → php-csv-exporter

sujancse / php-csv-exporter

Licence: MIT license
A fast and tiny PHP library to export data to CSV based on Generator. Export millions of data seamlessly without memory exception.

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to php-csv-exporter

Exporttools.bundle
Export tools for Plex
Stars: ✭ 294 (+1860%)
Mutual labels:  export, csv, xlsx
Sonar Cnes Report
Generates analysis reports from SonarQube web API.
Stars: ✭ 145 (+866.67%)
Mutual labels:  export, csv, xlsx
Tableexport
The simple, easy-to-implement library to export HTML tables to xlsx, xls, csv, and txt files.
Stars: ✭ 781 (+5106.67%)
Mutual labels:  export, csv, xlsx
sheet2dict
Simple XLSX and CSV to dictionary converter
Stars: ✭ 206 (+1273.33%)
Mutual labels:  export, csv, xlsx
Spreadsheet architect
Spreadsheet Architect is a library that allows you to create XLSX, ODS, or CSV spreadsheets super easily from ActiveRecord relations, plain Ruby objects, or tabular data.
Stars: ✭ 1,160 (+7633.33%)
Mutual labels:  export, csv, xlsx
Laravel Translatable String Exporter
Translatable String Exporter for Laravel
Stars: ✭ 149 (+893.33%)
Mutual labels:  export, exporter
Rats
Movie Ratings Synchronization with Python
Stars: ✭ 156 (+940%)
Mutual labels:  export, exporter
Jsonexport
{} → 📄 it's easy to convert JSON to CSV
Stars: ✭ 208 (+1286.67%)
Mutual labels:  export, csv
convey
CSV processing and web related data types mutual conversion
Stars: ✭ 16 (+6.67%)
Mutual labels:  csv, xlsx
Csv Grid
Yii2 extension for CSV export
Stars: ✭ 83 (+453.33%)
Mutual labels:  export, csv
Portphp
Data import/export framework for PHP
Stars: ✭ 225 (+1400%)
Mutual labels:  export, csv
eec
A fast and lower memory excel write/read tool.一个非POI底层,支持流式处理的高效且超低内存的Excel读写工具
Stars: ✭ 93 (+520%)
Mutual labels:  csv, xlsx
Criterion
Microbenchmarking for Modern C++
Stars: ✭ 140 (+833.33%)
Mutual labels:  export, csv
Csvreader
csvreader library / gem - read tabular data in the comma-separated values (csv) format the right way (uses best practices out-of-the-box with zero-configuration)
Stars: ✭ 169 (+1026.67%)
Mutual labels:  export, csv
Couchimport
CouchDB import tool to allow data to be bulk inserted
Stars: ✭ 125 (+733.33%)
Mutual labels:  export, csv
genshin-audio-exporter
Export audio files from Genshin Impact game data into different audio formats.
Stars: ✭ 83 (+453.33%)
Mutual labels:  export, exporter
csv2xlsx
Fast and simple opensource command line tool to convert CSV do XLSX
Stars: ✭ 38 (+153.33%)
Mutual labels:  csv, xlsx
elastic-query-export
🚚 Export Data from ElasticSearch to CSV/JSON using a Lucene Query (e.g. from Kibana) or a raw JSON Query string
Stars: ✭ 56 (+273.33%)
Mutual labels:  export, csv
tabular-stream
Detects tabular data (spreadsheets, dsv or json, 20+ different formats) and emits normalized objects.
Stars: ✭ 34 (+126.67%)
Mutual labels:  csv, xlsx
Excellentexport
Javascript export to Excel
Stars: ✭ 1,018 (+6686.67%)
Mutual labels:  export, csv

Packagist Version Packagist GitHub

Overview

A fast and tiny PHP library to export data to CSV. The library is based on a PHP generator.

Why Use

It took me 5 seconds to export 5M data so you can call it fast enough. And because of the use of Generator it uses less memory and never get caught by memory exception.

Installation

composer require sujan/php-csv-exporter

Basic Usage

$columns = [ 'id', 'name', 'email' ];

$queryBuilder = User::limit(10); // Query Builder

$exporter = new Exporter();
$exporter->build($queryBuilder, $columns, 'users.csv')
         ->export();

Build and export, that much simple.

Documentation

Build CSV

CSV build takes three parameters. First one is the model which could be Array, PDOStatement, Eloquent Query Builder and Collection, seconds one takes the field names you want to export, third one is CSV filename.

$exporter->build($queryBuilder, $columns, 'users.csv');

Export CSV

$exporter->export();

Usage Examples

Laravel

You can export data from Eloquent Query Builder, Collection and Array whereas Eloquent Query Builder is highly recommended.

From Eloquent Query Builder (RECOMMENDED)

$columns = [ 'id', 'name', 'email' ];

$queryBuilder = User::latest()->whereNotNull('email_verified_at'); // Query Builder

$exporter = new Exporter();
$exporter->build($queryBuilder, $columns, 'users.csv')
         ->export();

From Collection

$columns = [ 'id', 'name', 'email' ];

$collection = User::latest()->get(); // Collection

$exporter = new Exporter();
$exporter->build($collection, $columns, 'users.csv')
         ->export();

From Array

$columns = [ 'id', 'name', 'email' ];

$usersArray = User::latest()->get()->toArray(); // Array of Users

$exporter = new Exporter();
$exporter->build($usersArray, $columns, 'users.csv')
         ->export();

Eloquent Relation

$columns = [
    'id',
    'title',
    'user' => [ // user is a relation
        'name'
    ]
];

$queryBuilder = Post::with('user'); // Query builder

$exporter = new Exporter();
$exporter->build($queryBuilder, $columns, 'users.csv')
         ->export();

Raw PHP

The library supports Laravel as well as raw PHP. You can easily export data from PDOStatement and Array.

From Plain Array

$array = [
    ['id' => 1, 'name' => 'John Doe', 'email' => '[email protected]'],
    ['id' => 2, 'name' => 'Jane Doe', 'email' => '[email protected]']
];

$columns = ['id', 'name', 'email'];

$exporter = new Exporter();
$exporter->build($array, $columns, 'users.csv')
         ->export();

From PDOStatement (RECOMMENDED)

    $servername = "localhost";
    $username = "root";
    $password = "password";
    $dbname = "laravel";

    $columns = [
        'id',
        'name',
        'email'
    ];

    try {
        $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $stmt = $conn->prepare("SELECT id, name, email FROM users");
        $stmt->execute();

        // set the resulting array to associative
        $stmt->setFetchMode(PDO::FETCH_ASSOC);

        $exporter = new Exporter();
        $exporter->build($stmt, $columns, 'users.csv)
                 ->export();
    }
    catch(PDOException $e) {
        echo "Error: " . $e->getMessage();
    }
    $conn = null;

You are always welcome to contribute

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].