yzh před 4 roky
rodič
revize
9653a22a9a

+ 16 - 0
examples/constMemory_demo.php

@@ -0,0 +1,16 @@
+<?php
+/**
+ * 固定内存模式demo
+ */
+require_once(__DIR__ . '/../vendor/autoload.php');
+use Pxlswrite\Pxlswrite;
+
+$fileObj = new Pxlswrite(['path' => __DIR__ . '/uploads']);
+
+$fileObj->constMemory('constMemory.xlsx');
+
+$fileObj
+    ->setRow('A1', 50, ['bold'=>true,'align'=>[Pxlswrite::FORMAT_ALIGN_CENTER,Pxlswrite::FORMAT_ALIGN_VERTICAL_CENTER]]) // 写入数据前设置行样式
+    ->header(['name', 'age'])
+    ->data([['viest', 21]])
+    ->output();

+ 27 - 0
examples/export_general_demo.php

@@ -0,0 +1,27 @@
+<?php
+/**
+ * 一般通用数据自动合并导出demo
+ */
+require_once(__DIR__ . '/../vendor/autoload.php');
+use Pxlswrite\Pxlswrite;
+//定义字段
+$field = [
+    'id' => ['name' => 'title'],
+    'c1' => ['name' => 'age'],
+    'c2' => ['name' => 'year'],
+    'c3' => ['name' => 'kk'],
+];
+$fileObj = new Pxlswrite(['path' => __DIR__  . '/uploads' ]);
+$filePath = $fileObj->fileName('general.xlsx')
+    ->field($field)//设置字段&表格头
+    ->setGeneralData('generateData', ['c1', 'c2'], ['align' => [Pxlswrite::FORMAT_ALIGN_CENTER, Pxlswrite::FORMAT_ALIGN_VERTICAL_CENTER]])//设置数据 并自动合并单元格
+    ->output();//输出excel文件到磁盘
+//数据生成器
+function generateData(){
+    for($i=0;$i<10000;$i++){
+        yield [
+            ['id'=>$i,'c1'=>$i+1,'c2'=>$i+2,'c3'=>$i+3],
+            ['id'=>$i,'c1'=>$i+1,'c2'=>$i+2,'c3'=>$i+3],
+        ];
+    }
+}

+ 30 - 0
examples/field_demo.php

@@ -0,0 +1,30 @@
+<?php
+/**
+ * 字段定义demo
+ */
+require_once(__DIR__ . '/../vendor/autoload.php');
+use Pxlswrite\Pxlswrite;
+//定义字段
+$field = [
+    'name' => ['name' => '姓名'],
+    'year' => ['name' => '出生年份'],
+    'age' => ['name' => '年龄','callback'=>'ageFormat']//callback 回调方法处理格式化值 可以是函数/对象方法
+];
+$fileObj = new Pxlswrite(['path' => __DIR__ . '/uploads' ]);
+$fileObj->fileName('field.xlsx')
+    ->field($field)//设置字段&表格头
+    ->setGeneralData(function(){
+       yield [
+            ['name'=>'Rent', 'year'=>1999,'age'=>0],
+            ['name'=>'Gas',  'year'=>1996,'age'=>0],
+            ['name'=>'Food', 'year'=>1998,'age'=>0],
+            ['name'=>'Gym',  'year'=>1995,'age'=>0],
+        ];
+    }
+    )
+    ->output();//输出excel文件到磁盘
+//格式化字段值
+function ageFormat($v, $values)
+{
+    return date('Y') - $values['year'];
+}

+ 13 - 0
examples/fileName_demo.php

@@ -0,0 +1,13 @@
+<?php
+require_once(__DIR__ . '/../vendor/autoload.php');
+use Pxlswrite\Pxlswrite;
+
+$fileObj = new Pxlswrite(['path' => __DIR__ . '/uploads']);
+
+$fileObj->fileName('filename.xlsx');
+
+$fileObj
+    ->header(['name', 'age'])
+    ->data([['viest', 21]])
+    ->setRow('A1', 50, ['bold'=>true,'align'=>[Pxlswrite::FORMAT_ALIGN_CENTER,Pxlswrite::FORMAT_ALIGN_VERTICAL_CENTER]]) // 可以在写入数据后设置行样式
+    ->output();

+ 18 - 0
examples/insertText_demo.php

@@ -0,0 +1,18 @@
+<?php
+/**
+ * 文本插入demo
+ */
+require_once(__DIR__ . '/../vendor/autoload.php');
+use Pxlswrite\Pxlswrite;
+
+$fileObj = new Pxlswrite(['path' => __DIR__ . '/uploads']);
+
+$fileObj->fileName("insertText.xlsx")
+    ->header(['name', 'money']);
+
+for ($index = 0; $index < 10; $index++) {
+    $fileObj->insertText($index+1, 0, 'viest');
+    $fileObj->insertText($index+1, 1, 10000, '#,##0'); // #,##0 为单元格数据样式
+}
+
+$fileObj->output();

+ 14 - 0
examples/insertUrl_demo.php

@@ -0,0 +1,14 @@
+<?php
+/**
+ * 插入超链接demo
+ */
+require_once(__DIR__ . '/../vendor/autoload.php');
+use Pxlswrite\Pxlswrite;
+
+$fileObj = new Pxlswrite(['path' => __DIR__ . '/uploads']);
+
+$fileObj->fileName("insertUrl.xlsx");
+$fileObj->insertUrl(1, 0, 'https://github.com', ['underline'=>Pxlswrite::UNDERLINE_SINGLE,'fontColor'=>Pxlswrite::COLOR_GREEN]);
+$fileObj->insertText(1, 0, 'View');
+
+$fileObj->output();

+ 11 - 0
examples/mergeCells_demo.php

@@ -0,0 +1,11 @@
+<?php
+/**
+ * 合并单元格demo
+ */
+require_once(__DIR__ . '/../vendor/autoload.php');
+use Pxlswrite\Pxlswrite;
+
+$fileObj = new Pxlswrite(['path' => __DIR__ . '/uploads']);
+$fileObj->fileName("mergeCells.xlsx")
+    ->mergeCells('A1:C1', 'Merge cells')
+    ->output();

+ 29 - 0
examples/simple_export_demo.php

@@ -0,0 +1,29 @@
+<?php
+/**
+ * 简单导出demo
+ */
+require_once(__DIR__ . '/../vendor/autoload.php');
+use Pxlswrite\Pxlswrite;
+$fileObj = new Pxlswrite(['path' => __DIR__. '/uploads']);
+$filePath = $fileObj
+    ->fileName('simple.xlsx','sheet1') //fileName 会自动创建一个工作表,你可以自定义该工作表名称,工作表名称为可选参数
+    ->field([
+        'username'=>['name'=>'用户名'],
+        'age'=>['name'=>'年龄']
+    ]) //设置字段&表格头
+    ->setGeneralData('generateData') //通过回调生成器方法向excel填充数据
+    ->output(); //输出excel文件到磁盘,返回文件路径
+$fileObj->download($filePath); //下载excel文件
+/**
+ * 数据生成器方法 封装数据获取逻辑 通过yield返回 节省内存
+ */
+function generateData(){
+    for($i=0;$i<10000;$i++){
+        yield [
+            [
+                'username' => '匿名用户'.rand(1,9999),
+                'age' => rand(1,100),
+            ]
+        ];
+    }
+}

+ 42 - 0
examples/style_demo.php

@@ -0,0 +1,42 @@
+<?php
+/**
+ * 样式设置demo
+ */
+require_once(__DIR__ . '/../vendor/autoload.php');
+
+use Pxlswrite\DB\DB;
+use Pxlswrite\Pxlswrite;
+
+$fileObj = new Pxlswrite(['path' => __DIR__ . '/uploads']);
+$field = [
+    'id' => ['name' => 'title'],
+    'c1' => ['name' => 'age'],
+    'c2' => ['name' => 'year'],
+    'c3' => ['name' => 'kk'],
+    'c4' => ['name' => 'll'],
+    'c5' => ['name' => 'aa']//callback 回调处理格式化值 可以是函数/对象方法
+];
+$filePath = $fileObj->fileName('style.xlsx', 'sheet1')
+    ->field($field)//设置字段&表格头
+    ->setGeneralData('generateData')//设置数据
+    ->setRow('A1:A3', 80, ['bold' => true]) //设置单元行样式 A1:A3 单元格范围 80行高 ['blod'=>true] 加粗
+    ->setColumn('A:F', 20, ['background' => Pxlswrite::COLOR_GRAY, 'align' => [Pxlswrite::FORMAT_ALIGN_CENTER, Pxlswrite::FORMAT_ALIGN_VERTICAL_CENTER]]) //设置单元列样式
+    ->output();//输出excel文件到磁盘
+
+function generateData()
+{
+    $db = DB::getInstance();
+    $step = 10000;
+    for ($i = 0; $i < 10000; $i = $i + 1) {
+        yield [
+            [
+                'id' => $i + 1,
+                'c1' => $i,
+                'c2' => $i,
+                'c3' => $i,
+                'c4' => $i,
+                'c5' => $i
+            ]
+        ];
+    }
+}