Export csv

This commit is contained in:
Edward Yang 2023-07-04 18:25:09 +08:00
parent a531cc0173
commit 7e3ee84096
3 changed files with 118 additions and 1 deletions

View File

@ -5,8 +5,34 @@ namespace Beike\Shop\Http\Controllers;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use League\Csv\CannotInsertRecord;
class Controller extends \App\Http\Controllers\Controller
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
/**
* @param $filename
* @param $header
* @param $records
* @return mixed
* @throws CannotInsertRecord
*/
public function downloadCsv($filename, $header, $records): mixed
{
if (!str_contains($filename, '.csv')) {
$filename = $filename . '-' . date('YmdHis') . '.csv';
}
$headers = [
'Content-Type' => 'application/octet-stream',
'Content-Transfer-Encoding' => 'binary',
'Content-Disposition' => 'attachment; filename=' . $filename,
];
$csv = \League\Csv\Writer::createFromString('');
$csv->insertOne($header);
$csv->insertAll($records);
return response($csv, 200, $headers);
}
}

View File

@ -22,6 +22,7 @@
"laravel/horizon": "^5.10",
"laravel/socialite": "^5.5",
"laravel/tinker": "^2.7",
"league/csv": "^9.8",
"phpoffice/phpspreadsheet": "^1.28",
"spatie/laravel-permission": "^5.5",
"srmklive/paypal": "^3.0",

92
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "38c9df2659b7e86eb82aa7be8552c013",
"content-hash": "1c71b8ebdb3694859ad0a09bc3798d35",
"packages": [
{
"name": "brick/math",
@ -2463,6 +2463,96 @@
],
"time": "2022-12-11T20:36:23+00:00"
},
{
"name": "league/csv",
"version": "9.8.0",
"source": {
"type": "git",
"url": "https://github.com/thephpleague/csv.git",
"reference": "9d2e0265c5d90f5dd601bc65ff717e05cec19b47"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/thephpleague/csv/zipball/9d2e0265c5d90f5dd601bc65ff717e05cec19b47",
"reference": "9d2e0265c5d90f5dd601bc65ff717e05cec19b47",
"shasum": "",
"mirrors": [
{
"url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%",
"preferred": true
}
]
},
"require": {
"ext-json": "*",
"ext-mbstring": "*",
"php": "^7.4 || ^8.0"
},
"require-dev": {
"ext-curl": "*",
"ext-dom": "*",
"friendsofphp/php-cs-fixer": "^v3.4.0",
"phpstan/phpstan": "^1.3.0",
"phpstan/phpstan-phpunit": "^1.0.0",
"phpstan/phpstan-strict-rules": "^1.1.0",
"phpunit/phpunit": "^9.5.11"
},
"suggest": {
"ext-dom": "Required to use the XMLConverter and or the HTMLConverter classes",
"ext-iconv": "Needed to ease transcoding CSV using iconv stream filters"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "9.x-dev"
}
},
"autoload": {
"files": [
"src/functions_include.php"
],
"psr-4": {
"League\\Csv\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Ignace Nyamagana Butera",
"email": "nyamsprod@gmail.com",
"homepage": "https://github.com/nyamsprod/",
"role": "Developer"
}
],
"description": "CSV data manipulation made easy in PHP",
"homepage": "https://csv.thephpleague.com",
"keywords": [
"convert",
"csv",
"export",
"filter",
"import",
"read",
"transform",
"write"
],
"support": {
"docs": "https://csv.thephpleague.com",
"issues": "https://github.com/thephpleague/csv/issues",
"rss": "https://github.com/thephpleague/csv/releases.atom",
"source": "https://github.com/thephpleague/csv"
},
"funding": [
{
"url": "https://github.com/sponsors/nyamsprod",
"type": "github"
}
],
"time": "2022-01-04T00:13:07+00:00"
},
{
"name": "league/flysystem",
"version": "3.15.1",