| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- <?php
- namespace App\Console\Commands\Export;
- use Carbon\Carbon;
- use function EasyWeChat\Kernel\Support\str_random;
- trait ExportTrait
- {
- public $filename;
- public $path;
- public $fd;
- public function createdFile($directory, $name){
- //$this->filename = Carbon::today()->toDateString().'-'.str_random('5').'-'.$name.'.xlsx';
- $this->filename = $name.'.xlsx';
- //$this->path = storage_path('exports/' . $this->filename);
- $this->path = $directory . '/' . $this->filename;// storage_path('exports/' . $this->filename);
- //$this->path = $path;
- $dirPath = dirname($this->path);
- // 确保目录存在
- // if (!is_dir($dirPath)) {
- // mkdir($dirPath, 0777, true);
- // }
- if (is_dir($dirPath)) {
- $this->fd = fopen($this->path, 'w+');
- if ($this->fd) {
- //add BOM to fix UTF-8 in Excel
- fputs($this->fd, $bom = (chr(0xEF) . chr(0xBB) . chr(0xBF)));
- /* $table = $data;
- foreach ($table as $fields) {
- fputcsv($fd, $fields);
- }*/
- //fclose($fd);
- }
- }
- }
- public function putRow($row)
- {
- if (!$this->fd) {
- $this->fd = fopen($this->path, 'a+');
- }
- return fputcsv($this->fd, $row);
- }
- public function closeFile()
- {
- if ($this->fd) {
- fclose($this->fd);
- $this->fd = null;
- }
- }
- }
|