PHP code example of aoding9 / laravel-xlswriter-export
1. Go to this page and download the library: Download aoding9/laravel-xlswriter-export library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once ('vendor/autoload.php' );
aoding9 / laravel-xlswriter-export example snippets
namespace Aoding9 \Laravel \Xlswriter \Export \Demo ;
use Aoding9 \Laravel \Xlswriter \Export \BaseExport ;
class UserExport extends BaseExport {
public $header = [
['column' => 'a' , 'width' => 8 , 'name' => '序号' ],
['column' => 'b' , 'width' => 8 , 'name' => 'id' ],
['column' => 'c' , 'width' => 10 , 'name' => '姓名' ],
['column' => 'd' , 'width' => 10 , 'name' => '性别' ],
['column' => 'e' , 'width' => 20 , 'name' => '注册时间' ],
];
public $fileName = '用户导出表' ;
public $tableTitle = '用户导出表' ;
public function eachRow ($row) {
return [
$this ->index,
$row->id,
\Faker\Factory::create('zh_CN' )->name,
random_int(0 , 1 ) ? '男' : '女' ,
$row->created_at->toDateTimeString(),
];
}
}
$data = [
['id' => 1 , 'name' => '小白' , 'created_at' => now()->toDateString()],
['id' => 2 , 'name' => '小红' , 'created_at' => now()->toDateString()],
];
\Aoding9\Laravel\Xlswriter\Export\Demo\UserExportFromCollection::make($data)->export();
\Aoding9\Laravel\Xlswriter\Export\Demo\AreaExportFromCollection::make(\App\Models\Area::query()->limit(500000 )->get())->export();
\Aoding9\Laravel\Xlswriter\Export\Demo\UserExportFromCollection::make()->export();
namespace Aoding9 \Laravel \Xlswriter \Export \Demo ;
use Aoding9 \Laravel \Xlswriter \Export \BaseExport ;
class UserExportFromCollection extends BaseExport {
public $header = [
['column' => 'a' , 'width' => 8 , 'name' => '序号' ],
['column' => 'b' , 'width' => 8 , 'name' => 'id' ],
['column' => 'c' , 'width' => 10 , 'name' => '姓名' ],
['column' => 'd' , 'width' => 10 , 'name' => '性别' ],
['column' => 'e' , 'width' => 20 , 'name' => '注册时间' ],
];
public $fileName = '用户导出表' ;
public $tableTitle = '用户导出表' ;
public function eachRow ($row) {
return [
$this ->index,
$row['id' ],
$row['name' ],
random_int(0 , 1 ) ? '男' : '女' ,
$row['created_at' ],
];
}
public function buildData (?int $page = null, ?int $perPage = null) {
return collect([
['id' => 1 , 'name' => '小白' , 'created_at' => now()->toDateString()],
['id' => 2 , 'name' => '小红' , 'created_at' => now()->toDateString()],
]);
}
}
namespace Aoding9 \Laravel \Xlswriter \Export \Demo ;
use Aoding9 \Laravel \Xlswriter \Export \BaseExport ;
use Illuminate \Support \Carbon ;
use Vtiful \Kernel \Format ;
class UserMergeExport extends BaseExport {
public $header = [
['column' => 'a' , 'width' => 10 , 'name' => '序号' ],
['column' => 'b' , 'width' => 10 , 'name' => 'id' ],
['column' => 'c' , 'width' => 10 , 'name' => '姓名' ],
['column' => 'd' , 'width' => 10 , 'name' => '性别' ],
['column' => 'e' , 'width' => 20 , 'name' => '注册时间' ],
];
public function getGender () {
return random_int(0 , 1 ) ? '男' : '女' ;
}
public function eachRow ($row) {
return [
$this ->index,
$row->id,
\Faker\Factory::create('zh_CN' )->name,
$this ->getGender(),
$row->created_at,
];
}
public $fileName = '用户导出表' ;
public $tableTitle = '用户导出表' ;
public $useFreezePanes = false ;
public $fontFamily = '宋体' ;
public $rowHeight = 30 ;
public $titleRowHeight = 40 ;
public $headerRowHeight = 50 ;
public $useGlobalStyle=false ;
public function afterInsertEachRowInEachChunk ($row) {
if ($this ->index % 2 === 1 && $this ->getCurrentLine() < $this ->completed + $this ->startDataRow) {
$range1 = "B" . $this ->getCurrentLine() . ":B" . ($this ->getCurrentLine() + 1 );
$nextRow = $this ->getRowInChunkByIndex($this ->index + 1 );
$ids = $row->id . '---' . ($nextRow ? $nextRow->id : null );
$this ->excel->mergeCells($range1, $ids, $this ->getSpecialStyle());
$range2 = "C" . $this ->getCurrentLine() . ":D" . $this ->getCurrentLine();
$nameAndGender = $row->name . "---" . $this ->getGender();
$this ->excel->mergeCells($range2, $nameAndGender);
}
}
public function setHeaderData () {
parent ::setHeaderData();
$this ->headerData->put(2 , $this ->headerData->get(1 ));
$this ->headerData->put(1 , []);
return $this ;
}
public function mergeCellsAfterInsertData () {
return [
['range' => "A1:{$this->end}1" , 'value' => $this ->getTableTitle(), 'formatHandle' => $this ->titleStyle],
['range' => "A2:A3" , 'value' => '序号' , 'formatHandle' => $this ->getSpecialStyle()],
['range' => "B2:B3" , 'value' => 'id' , 'formatHandle' => $this ->headerStyle],
['range' => "C2:E2" , 'value' => '基本资料' , 'formatHandle' => $this ->getSpecialStyle()],
];
}
public $specialStyle;
public function getSpecialStyle () {
return $this ->specialStyle ?: $this ->specialStyle = (new Format($this ->fileHandle))
->background(Format::COLOR_YELLOW)
->fontSize(10 )
->border(Format::BORDER_THIN)
->italic()
->font('微软雅黑' )
->align(Format::FORMAT_ALIGN_CENTER, Format::FORMAT_ALIGN_VERTICAL_CENTER)
->wrap()
->toResource();
}
public function insertCellHandle ($currentLine, $column, $data, $format, $formatHandle) {
if ($this ->getColumn($column) === 'E' && $data instanceof Carbon) {
if ($data->second % 2 === 0 ) {
$formatHandle = $this ->getSpecialStyle();
}
$data = $data->toDateTimeString();
}
return $this ->excel->insertText($currentLine, $column, $data, $format, $formatHandle);
}
}
public function exportModels () {
$query=\App\Models\User::query();
\Aoding9\Laravel\Xlswriter\Export\Demo\UserExport::make($query)->export();
\Aoding9\Laravel\Xlswriter\Export\Demo\UserMergeExport::make($query)->export();
$data = [
['id' => 1 , 'name' => '小白' , 'created_at' => now()->toDateString()],
['id' => 2 , 'name' => '小红' , 'created_at' => now()->toDateString()],
];
\Aoding9\Laravel\Xlswriter\Export\Demo\UserExportFromCollection::make($data)->export();
\Aoding9\Laravel\Xlswriter\Export\Demo\UserExportByCollection::make()->export();
$time =microtime(true );
$query=\App\Models\Area::where('parent_code' ,0 );
\Aoding9\Laravel\Xlswriter\Export\Demo\AreaExport::make($query,$time)->export();
ini_set('memory_limit' , '2048M' );
set_time_limit(0 );
$data =\App\Models\Area::query()->limit(500000 )->get();
\Aoding9\Laravel\Xlswriter\Export\Demo\AreaExportFromCollection::make($data,$time)->export();
}
public $useSwoole = true ;
return UserExport::make()->export();