12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- <?php
- require_once(__DIR__.'/../vendor/autoload.php');
- use Pxlswrite\DB\DB;
- use Pxlswrite\Pxlswrite;
- use Pxlswrite\WebSocket\WebSocketClient;
- $time = time();
- $fileObj = new Pxlswrite(['path' => __DIR__ . '/uploads']);
- $pushHandle = new WebSocketClient('ws://192.168.18.192:9502',$_GET['fd']);
- $fileObj->fileName('123.xlsx');
- $leftStyle = $fileObj->styleFormat()
- ->bold()
- ->align(Pxlswrite::FORMAT_ALIGN_LEFT, Pxlswrite::FORMAT_ALIGN_VERTICAL_CENTER)
- ->toResource();
- $borderStyle = $fileObj->styleFormat()
- ->align(Pxlswrite::FORMAT_ALIGN_RIGHT, Pxlswrite::FORMAT_ALIGN_VERTICAL_CENTER)
- ->border(Pxlswrite::BORDER_SLANT_DASH_DOT)
- ->toResource();
- $colorStyle = $fileObj->styleFormat()
- ->fontColor(Pxlswrite::COLOR_BLUE)
- ->toResource();
- $backgroundStyle = $fileObj->styleFormat()
- ->background(Pxlswrite::COLOR_RED)
- ->toResource();
- $numberStyle = $fileObj->styleFormat()
- ->number('#,##0')
- ->toResource();
- $defaultStyle = $fileObj->styleFormat()
- ->fontColor(Pxlswrite::COLOR_ORANGE)
- ->border(Pxlswrite::BORDER_DASH_DOT)
- ->align(Pxlswrite::FORMAT_ALIGN_CENTER,Pxlswrite::FORMAT_ALIGN_VERTICAL_CENTER)
- ->toResource();
- $field = [
- 'id'=>['name'=>'title'],
- 'c1'=>['name'=>'age'],
- 'c2'=>['name'=>'year'],
- 'c3'=>['name'=>'kk'],
- 'c4'=>['name'=>'ll'],
- 'c5'=>['name'=>'aa','callback'=>'myFormat']
- ];
- $filePath = $fileObj->field($field)
- ->defaultFormat($defaultStyle)
- ->setDataByGenerator('generateData',$pushHandle)
- ->setRow('A1:A3', 80, $leftStyle)
- ->setRow('A2',50,$borderStyle)
- ->setRow('A3',50,$colorStyle)
- ->setRow('A4',40,$backgroundStyle)
- ->setColumn('F:F',40,$numberStyle)
- ->mergeCells('A1:C1', 'Merge cells',$fileObj->styleFormat()->align(Pxlswrite::FORMAT_ALIGN_CENTER,Pxlswrite::FORMAT_ALIGN_VERTICAL_CENTER)->toResource())
- ->output();
- $memory = floor((memory_get_peak_usage()) / 1024 / 1024) . "MB";
- $execute_time = time() - $time . 's';
- echo json_encode(['code' => 1, 'msg' => '导出完毕', 'url' => '/download.php?file=' . $filePath, 'data' => ['memory' => $memory, 'excute_time' => $execute_time]]);
- function generateData(){
- $db = DB::getInstance();
- $step = 10000;
- for ($i = 0; $i < 1000000; $i = $i + $step) {
- yield $db->get_records_sql("select * from sheet1 limit {$i},{$step}", null, PDO::FETCH_ASSOC);
- }
- }
- function myFormat($v,$values){
- return $v.'自定义格式化-'.$values['id'];
- }
|