# quick-excel **Repository Path**: xiaoxiaoxiaobuding/quick-excel ## Basic Information - **Project Name**: quick-excel - **Description**: 快速处理excel数据 - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-05-18 - **Last Updated**: 2022-04-08 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## Quick start Install via composer: ``` composer require Pudding/fast-excel ``` Export a Model to `.xlsx` file: ```php use Pudding\FastExcel\FastExcel; use App\User; // Load users $users = User::all(); // Export all users (new FastExcel($users))->export('file.xlsx'); ``` ## Export Export a Model or a **Collection**: ```php $list = collect([ [ 'id' => 1, 'name' => 'Jane' ], [ 'id' => 2, 'name' => 'John' ], ]); (new FastExcel($list))->export('file.xlsx'); ``` Export `xlsx`, `ods` and `csv`: ```php $invoices = App\Invoice::orderBy('created_at', 'DESC')->get(); (new FastExcel($invoices))->export('invoices.csv'); ``` Export only some attributes specifying columns names: ```php (new FastExcel(User::all()))->export('users.csv', function ($user) { return [ 'Email' => $user->email, 'First Name' => $user->firstname, 'Last Name' => strtoupper($user->lastname), ]; }); ``` Download (from a controller method): ```php return (new FastExcel(User::all()))->download('file.xlsx'); ``` ## Import `import` returns a Collection: ```php $collection = (new FastExcel)->import('file.xlsx'); ``` Import a `csv` with specific delimiter, enclosure characters and "gbk" encoding: ```php $collection = (new FastExcel)->configureCsv(';', '#', '\n', 'gbk')->import('file.csv'); ``` Import and insert to database: ```php $users = (new FastExcel)->import('file.xlsx', function ($line) { return User::create([ 'name' => $line['Name'], 'email' => $line['Email'] ]); }); ``` ## Facades You may use FastExcel with the optional Facade. Add the following line to ``config/app.php`` under the ``aliases`` key. ````php 'FastExcel' => Pudding\FastExcel\Facades\FastExcel::class, ```` Using the Facade, you will not have access to the constructor. You may set your export data using the ``data`` method. ````php $list = collect([ [ 'id' => 1, 'name' => 'Jane' ], [ 'id' => 2, 'name' => 'John' ], ]); FastExcel::data($list)->export('file.xlsx'); ```` ## Global helper FastExcel provides a convenient global helper to quickly instantiate the FastExcel class anywhere in a Laravel application. ```php $collection = fastexcel()->import('file.xlsx'); fastexcel($collection)->export('file.xlsx'); ``` ## Advanced usage ### Export multiple sheets Export multiple sheets by creating a `SheetCollection`: ```php $sheets = new SheetCollection([ User::all(), Project::all() ]); (new FastExcel($sheets))->export('file.xlsx'); ``` Use index to specify sheet name: ```php $sheets = new SheetCollection([ 'Users' => User::all(), 'Second sheet' => Project::all() ]); ``` ### Import multiple sheets Import multiple sheets by using `importSheets`: ```php $sheets = (new FastExcel)->importSheets('file.xlsx'); ``` You can also import a specific sheet by its number: ```php $users = (new FastExcel)->sheet(3)->import('file.xlsx'); ``` ### Export large collections with chunk Export rows one by one to avoid `memory_limit` issues [using `yield`](https://www.php.net/manual/en/language.generators.syntax.php): ```php function usersGenerator() { foreach (User::cursor() as $user) { yield $user; } } // Export consumes only a few MB, even with 10M+ rows. (new FastExcel(usersGenerator()))->export('test.xlsx'); ``` ### Add header and rows style Add header and rows style with `headerStyle` and `rowsStyle` methods. ```php $header_style = (new StyleBuilder())->setFontBold()->build(); $rows_style = (new StyleBuilder()) ->setFontSize(15) ->setShouldWrapText() ->setBackgroundColor("EDEDED") ->build(); return (new FastExcel($list)) ->headerStyle($header_style) ->rowsStyle($rows_style) ->download('file.xlsx'); ```