marshung / io
导入导出构建器
Requires
- dev-master
- 1.0.0
- 0.9.11
- 0.9.10
- 0.9.9
- 0.9.7
- 0.9.6
- 0.9.5
- 0.9.4
- 0.9.3
- 0.9.2
- 0.9.0
- 0.8.1
- 0.8.0
- 0.7.5
- 0.7.4
- 0.7.3
- 0.7.2
- 0.6.0
- 0.5.3
- 0.5.2
- 0.5.1
- 0.5.0
- 0.4.2
- 0.4.1
- 0.4.0
- 0.3.2
- 0.3.1
- 0.3.0
- 0.2.0
- 0.1.0
- dev-dev
- dev-dependabot/composer/phpoffice/phpspreadsheet-1.17.1
- dev-tmp
- dev-issue_16
- dev-development
- dev-release
This package is auto-updated.
Last update: 2024-09-28 10:35:34 UTC
README
目录
说明
为简化导出导入用法,编写此模块
安装
$ composer require marshung/io
用法
有数据的结构定义对象
需要先定义好结构对象,导出时,直接指定定义好的结构对象即可
结构对象参考:src/config/ComplexExampleConfig.php src/config/EmptyConfig.php src/config/SimpleExampleConfig.php
导出
// 取得原始資料 $data = [ [ 'u_no' => 'A001', 'c_name' => 'Mars', 'id_no' => 'A234567890', 'birthday' => '2000-01-01', 'gender' => '1', ], [ 'u_no' => 'A001', 'c_name' => 'Jack', 'id_no' => 'A123456789', 'birthday' => '20001-01-01', 'gender' => '1', ] ];
// IO物件建構 $io = new \marshung\io\IO(); // 匯出處理 - 建構匯出資料 - 簡易模式結構定義物件-範本 $io->export($data, $config = 'SimpleExample', $builder = 'Excel', $style = 'Io');
// IO物件建構 $io = new \marshung\io\IO(); // 匯出處理 - 建構匯出資料 - 複雜模式結構定義物件-範本 $io->export($data, $config = 'ComplexExample', $builder = 'Excel', $style = 'Io');
// IO物件建構 $io = new \marshung\io\IO(); // 匯出處理 - 物件注入方式 $config = new \marshung\io\config\SimpleExampleConfig(); // 必要欄位設定 - 提供讀取資料時驗証用 - 有設定,且必要欄位有無資料者,跳出 - 因各版本excel對空列定義不同,可能編輯過列,就會產生沒有結尾的空列,導致在讀取excel時有讀不完狀況。 $config->setOption([ 'u_no' ], 'requiredField'); $builder = new \marshung\io\builder\ExcelBuilder(); $style = new \marshung\io\style\IoStyle(); // 欄位B凍結 $style->setFreeze('B'); $io->export($data, $config, $builder, $style);
请注意设置"必填字段设置",因为不同版本的Excel对空列的定义不同,可能会编辑过列,就可能会产生没有结尾的空列,导致在读取Excel时出现读不完的情况。
导入
// IO物件建構 $io = new \marshung\io\IO(); // 匯入處理 - 取得匯入資料 $data = $io->import($builder = 'Excel', $fileArgu = 'fileupload'); // 取得匯入config名子 $configName = $io->getConfig()->getOption('configName'); // 取得有異常有下拉選單內容 $mismatch = $io->getMismatch(); // $mismatch = $io->getConfig()->getMismatch(); echo 'Config Name = ' . $configName . "<br>\n"; echo 'Data = '; var_export($data); echo "\n"; echo 'Exception content = '; var_export($mismatch);
当有下拉菜单的列时,其输入的内容不在下拉菜单中,$data中的数据内容会是空字符串,但原始内容会记录在$mismatch中
空的结构定义对象
如果不需要格式、样式等设置,只需将数据数组纯输出,可以使用空结构定义
导出
// 取得原始資料 $data = [ [ 'u_no' => 'A001', 'c_name' => 'Mars', 'id_no' => 'A234567890', 'birthday' => '2000-01-01', 'gender' => '1', ], [ 'u_no' => 'A001', 'c_name' => 'Jack', 'id_no' => 'A123456789', 'birthday' => '20001-01-01', 'gender' => '1', ] ]; // IO物件建構 $io = new \marshung\io\IO(); // 匯出處理 - 建構匯出資料 - 空的結構定義物件 $io->export($data, $config = 'Empty', $builder = 'Excel', $style = 'Io');
导入
// IO物件建構 $io = new \marshung\io\IO(); // 匯入處理 - 取得匯入資料 $data = $io->import($builder = 'Excel', $fileArgu = 'fileupload'); // 取得匯入config名子 $configName = $io->getConfig()->getOption('configName'); echo 'Config Name = ' . $configName . "<br>\n"; echo 'Data = '; var_export($data);
手动处理-简单模式
如果数据列为可变长度时,将无法定义完善的结构定义对象,此时可用手动模式
当然,在这种情况下,可以定义好可预期的字段结构,然后出现额外的字段时,使用$config的getTitle(),getContent()取出数据并修改,再利用setTitle(),setContent()回写,并用setList()补充映射表数据即可
导出
// 取得原始資料 $data = [ [ 'u_no' => 'A001', 'c_name' => 'Mars', 'id_no' => 'A234567890', 'birthday' => '2000-01-01', 'gender' => '1', ], [ 'u_no' => 'A001', 'c_name' => 'Jack', 'id_no' => 'A123456789', 'birthday' => '20001-01-01', 'gender' => '1', ] ]; // 結構定義-簡易模式 $defined = array( 'u_no' => '員工編號', 'c_name' => '姓名', 'id_no' => '身分證字號', 'birthday' => '出生年月日', 'gender' => '性別' ); // IO物件建構 $io = new \marshung\io\IO(); // 手動建構相關物件 $io->setConfig() ->setBuilder() ->setStyle(); // 載入外部定義 $conf = $io->getConfig() ->setTitle($defined) ->setContent($defined); // 建構外部對映表 $listMap = array( 'gender' => array( array( 'value' => '1', 'text' => '男' ), array( 'value' => '0', 'text' => '女' ) ) ); // 載入外部對映表 $conf->setList($listMap); // 必要欄位設定 - 提供讀取資料時驗証用 - 有設定,且必要欄位有無資料者,跳出 - 因各版本excel對空列定義不同,可能編輯過列,就會產生沒有結尾的空列,導致在讀取excel時有讀不完狀況。 $conf->setOption([ 'u_no' ], 'requiredField'); // 匯出處理 - 建構匯出資料 - 手動處理 $io->setData($data)->exportBuilder();
导入
// IO物件建構 $io = new \marshung\io\IO(); // 匯入處理 - 取得匯入資料 $data = $io->import($builder = 'Excel', $fileArgu = 'fileupload'); // 取得匯入config名子 $configName = $io->getConfig()->getOption('configName'); echo 'Config Name = ' . $configName . "<br>\n"; echo 'Data = '; var_export($data);
手动处理-复杂模式
如果数据列为可变长度时,将无法定义完善的结构定义对象,此时可用手动模式
当然,在这种情况下,可以定义好可预期的字段结构,然后出现额外的字段时,使用$config的getTitle(),getContent()取出数据并修改,再利用setTitle(),setContent()回写,并用setList()补充映射表数据即可
导出
// 取得原始資料 $data = [ [ 'u_no' => 'A001', 'c_name' => 'Mars', 'id_no' => 'A234567890', 'birthday' => '2000-01-01', 'gender' => '1', ], [ 'u_no' => 'A001', 'c_name' => 'Jack', 'id_no' => 'A123456789', 'birthday' => '20001-01-01', 'gender' => '1', ] ]; // 結構定義-複雜模式 // 標題1 $title1 = array( 'config' => array( 'type' => 'title', 'name' => 'title1', 'style' => array( 'font-size' => '16' ), 'class' => '' ), 'defined' => array( 't1' => array( 'key' => 't1', 'value' => '帳號', 'col' => '1', 'row' => '1', 'style' => array(), 'class' => '', 'default' => '', 'list' => '' ), 't2' => array( 'key' => 't2', 'value' => '姓名', 'col' => '1', 'row' => '1', 'style' => array(), 'class' => '', 'default' => '', 'list' => '' ), 't3' => array( 'key' => 't3', 'value' => '身分證字號', 'col' => '1', 'row' => '1', 'style' => array(), 'class' => '', 'default' => '', 'list' => '' ), 't4' => array( 'key' => 't4', 'value' => '生日', 'col' => '1', 'row' => '1', 'style' => array(), 'class' => '', 'default' => '', 'list' => '' ), 't5' => array( 'key' => 't4', 'value' => '性別', 'col' => '2', 'row' => '1', 'style' => array(), 'class' => '', 'default' => '', 'list' => '' ) ) ); // 標題2 $title2 = array( 'config' => array( 'type' => 'title', 'name' => 'example', 'style' => array(), 'class' => 'example' ), 'defined' => array( 't1' => array( 'key' => 't1', 'value' => 'A001', 'col' => '1', 'row' => '1', 'style' => array(), 'class' => '', 'default' => '', 'list' => '' ), 't2' => array( 'key' => 't2', 'value' => '派大星', 'col' => '1', 'row' => '1', 'style' => array(), 'class' => '', 'default' => '', 'list' => '' ), 't3' => array( 'key' => 't3', 'value' => 'ET9000001', 'col' => '1', 'row' => '1', 'style' => array(), 'class' => '', 'default' => '', 'list' => '' ), 't4' => array( 'key' => 't4', 'value' => '2000-01-01', 'col' => '1', 'row' => '1', 'style' => array(), 'class' => '', 'default' => '', 'list' => '' ), 't5' => array( 'key' => 't4', 'value' => '男', 'col' => '2', 'row' => '1', 'style' => array(), 'class' => '', 'default' => '', 'list' => '' ) ) ); // 內容 $content = array( 'config' => array( 'type' => 'content', 'name' => 'content', 'style' => array(), 'class' => '' ), 'defined' => array( 'u_no' => array( 'key' => 'u_no', 'value' => '帳號', 'col' => '1', 'row' => '1', 'style' => array(), 'class' => '', 'default' => '', 'list' => '' ), 'c_name' => array( 'key' => 'c_name', 'value' => '姓名', 'col' => '1', 'row' => '1', 'style' => array(), 'class' => '', 'default' => '', 'list' => '' ), 'id_no' => array( 'key' => 'id_no', 'value' => '身分證字號', 'col' => '1', 'row' => '1', 'style' => array(), 'class' => '', 'default' => '', 'list' => '' ), 'birthday' => array( 'key' => 'birthday', 'value' => '生日', 'col' => '1', 'row' => '1', 'style' => array(), 'class' => '', 'default' => '', 'list' => '' ), 'gender' => array( 'key' => 'gender', 'value' => '性別', 'col' => '2', 'row' => '1', 'style' => array(), 'class' => '', 'default' => '1', 'list' => '' ) ) ); // IO物件建構 $io = new \marshung\io\IO(); // 手動建構相關物件 $io->setConfig() ->setBuilder() ->setStyle(); // 載入外部定義 $conf = $io->getConfig() ->setTitle($title1) ->setTitle($title2) ->setContent($content); // 建構外部對映表 $listMap = array( 'gender' => array( array( 'value' => '1', 'text' => '男' ), array( 'value' => '0', 'text' => '女' ) ) ); // 載入外部對映表 $conf->setList($listMap); // 必要欄位設定 - 提供讀取資料時驗証用 - 有設定,且必要欄位有無資料者,跳出 - 因各版本excel對空列定義不同,可能編輯過列,就會產生沒有結尾的空列,導致在讀取excel時有讀不完狀況。 $conf->setOption([ 'u_no' ], 'requiredField'); // 匯出處理 - 建構匯出資料 - 手動處理 $io->setData($data)->exportBuilder();
导入
// IO物件建構 $io = new \marshung\io\IO(); // 匯入處理 - 取得匯入資料 $data = $io->import($builder = 'Excel', $fileArgu = 'fileupload'); // 取得匯入config名子 $configName = $io->getConfig()->getOption('configName'); // 取得有異常有下拉選單內容 $mismatch = $io->getMismatch(); // $mismatch = $io->getConfig()->getMismatch(); echo 'Config Name = ' . $configName . "<br>\n"; echo 'Data = '; var_export($data); echo "\n"; echo 'Exception content = '; var_export($mismatch);
当有下拉菜单的列时,其输入的内容不在下拉菜单中,$data中的数据内容会是空字符串,但原始内容会记录在$mismatch中
样式
可用列表
颜色
水平对齐
垂直对齐
边框
表格格式
设置方式
设置文件
- 自定义样式文件
class MyStyle extends marshung\io\style\IoStyle { public function __construct() { parent::__construct(); // 初始化 - 預設樣式 $this->run(); } /** * 初始化 - 預設樣式 * 'width' => 20.71,//儲存格欄寬 * 'height' => -1,//儲存格欄高(-1為自動高度) * 'wraptext' => true,//儲存格自動換行 * 'font-name' => '微軟正黑體',//字體字型 * 'font-size' => 11,//字體大小 * 'font-bold' => false,//字體粗體 * 'font-underline' => false,//字體底線 * 'font-color' => 'black',//字體顏色 * 'align-horizontal' => 'left',//水平對齊 * 'align-vertical' => 'center',//垂直對齊 * 'border-all-style' => 'thin',//欄線樣式-全部 * 'border-all-color' => 'FF9F9FA0',//欄線顏色-全部 * 'border-top-style' => 'thin',//上欄線樣式 * 'border-left-style' => 'thin',//左欄線樣式 * 'border-right-style' => 'thin',//右欄線樣式 * 'border-bottom-style' => 'thin',//下欄線樣式 * 'border-outline-style' => 'thin',//外圈線樣式 * 'border-inside-style' => 'thin',//內部線樣式 * 'border-top-color' => 'FFDADCDD',//上欄線顏色 * 'border-left-color' => 'FFDADCDD',//左欄線顏色 * 'border-right-color' => 'FFDADCDD',//右欄線顏色 * 'border-bottom-color' => 'FFDADCDD',//下欄線顏色 * 'border-outline-color' => 'FFDADCDD',//外圈欄線顏色 * 'border-inside-color' => 'FFDADCDD',//內部欄線顏色 * 'background-color' => 'white'//儲存格背景顏色 * 'row-odd-background-color' => 'F7F7F7',//內容奇數列背景顏色 * 'row-even-background-color' => 'white'//內容偶數列背景顏色 */ protected function initDefault() { // 自定樣式集 $this->_classMap['mystyle'] = array( 'width' => 20.71, // 儲存格欄寬 'height' => - 1, // 儲存格欄高(-1為自動高度) 'format' => 'text', // 儲存格格式-文字 'wraptext' => true, // 儲存格自動換行 'font-name' => '微軟正黑體', // 字體字型 'font-size' => 11, // 字體大小 'font-color' => 'black', // 字體顏色 'align-vertical' => 'center', // 垂直對齊 'border-all-style' => 'thin', // 欄線樣式-全部 'border-all-color' => 'FF9F9FA0' // 欄線顏色-全部 ); } }
- 设置结构配置文件 config (摘录) 如标题要用自定义样式集
$this->_title[] = array( 'config' => array( 'type' => 'title', 'name' => 'title1', // 自定樣式 'style' => array( 'font-size' => '16' ), // 樣式集名稱 'class' => 'mystyle' ), 'defined' => array( 't1' => array( 'key' => 't1', 'value' => '日期', 'col' => '1', 'row' => '1', // 自定樣式 'style' => array('format' => 'date'), 'class' => '', 'default' => '', 'list' => '' ), );
手动-复杂模式-样式
$style = new \marshung\io\style\IoStyle(); $style->setClass(array( 'width' => 20.71, // 儲存格欄寬 'height' => - 1, // 儲存格欄高(-1為自動高度) 'format' => 'text', // 儲存格格式-文字 'wraptext' => true, // 儲存格自動換行 'font-name' => '微軟正黑體', // 字體字型 'font-size' => 11, // 字體大小 'font-color' => 'black', // 字體顏色 'align-vertical' => 'center', // 垂直對齊 'border-all-style' => 'thin', // 欄線樣式-全部 'border-all-color' => 'FF9F9FA0' // 欄線顏色-全部 ), 'mystyle'); // 標題1 $title1 = array( 'config' => array( 'type' => 'title', 'name' => 'title1', // 自定樣式 'style' => array( 'font-size' => '16' ), // 樣式集名稱 'class' => 'mystyle' ), 'defined' => array( 't1' => array( 'key' => 't1', 'value' => '日期', 'col' => '1', 'row' => '1', // 自定樣式 'style' => array('format' => 'date'), 'class' => '', 'default' => '', 'list' => '' ), ); // IO物件建構 $io = new \marshung\io\IO(); // 手動建構相關物件 $io->setConfig() ->setBuilder() ->setStyle(); // 載入外部定義 $conf = $io->getConfig() ->setTitle($title1) ->setTitle($title2) ->setContent($content); // 略,參照: 手動 - 複雜模式
样式后处理
为应对过于特殊的样式设置,开放样式后处理
自动模式-样式后处理
// 取得原始資料 $data = getData('3'); // IO物件建構 $io = new \marshung\io\IO(); // 匯出處理 - 物件注入方式 $config = new \marshung\io\config\ComplexExampleConfig(); $builder = new \marshung\io\builder\ExcelBuilder(); $style = new \marshung\io\style\IoStyle(); // Output format: file, phpSpreadsheet(src/object/sheet/spreadsheet/phpspreadsheet) $builder->setOption('object', 'outputFormat'); // 建構輸出物件 $spreadsheet = $io->export($data, $config, $builder, $style); // 自定樣式 - style後處理 $titleStyle = ['background-color' => 'FF0094D8']; $titleRange = 'A4:C4'; \marshung\io\builder\ExcelStyleBuilder::setExcelRangeStyle($titleStyle, $spreadsheet, $titleRange); // 重新輸出 $builder->output('my_file_name', 'file');
手动模式-样式后处理
// 取得原始資料 $data = getData('7'); // 結構定義-簡易模式 $defined = array('....'); // IO物件建構 $io = new \marshung\io\IO(); // 手動建構相關物件 $io->setConfig()->setBuilder()->setStyle(); // 載入外部定義 $conf = $io->getConfig()->setTitle($defined)->setContent($defined); // 建構外部對映表 $listMap = array('...'); // 取得建構物件 $builder = $io->getBuilder(); // Output format: file, phpSpreadsheet(src/object/sheet/spreadsheet/phpspreadsheet) $builder->setOption('object', 'outputFormat'); // 載入外部對映表 $conf->setList($listMap); // 匯出處理 - 建構匯出資料 $spreadsheet = $io->setData($data)->exportBuilder(); // 自定樣式 - style後處理 $titleStyle = ['background-color' => 'FF0094D8']; $titleRange = 'B4:D4'; \marshung\io\builder\ExcelStyleBuilder::setExcelRangeStyle($titleStyle, $spreadsheet, $titleRange); // 重新輸出 $builder->output('export7', 'file');
多页工作表
// 取得原始資料 $data = getData('10'); $deptData = getDeptData('10'); // 匯出處理 - 物件注入方式 $config1 = new \marshung\io\config\ComplexExampleConfig(); $config2 = new \marshung\io\config\ComplexExampleDeptConfig(); $builder = new \marshung\io\builder\ExcelBuilder(); $style = new \marshung\io\style\IoStyle(); // 欄位B凍結 $style->setFreeze('B'); // 手動建構相關物件 $spreadsheet = $builder->setData($data) ->setConfig($config1) ->setStyle($style) ->build() ->setConfig($config2) ->setData($deptData) ->build() ->output('export-10', 'file');