大雅科/表格转换器

v1.0.7 2020-11-20 21:42 UTC

This package is auto-updated.

Last update: 2024-09-21 06:44:01 UTC


README

要将表格转换为另一种格式,您需要定义 3 个参数

  • 一个解码器,尝试解码原始格式
  • 一个编码器,将生成新的表格
  • 一个关联规则,帮助转换器进行适当的转换
Converter::convert(Decoder $decoder, AssociationRule $associationRule, Coder $coder);

##简单示例

它使用 mysqli 将 xlsx 文件插入 mysql 表格。它将使用 xls 标题作为 mysql 表格的列名

Converter::convert(new XlsxCodec("test_file.xlsx"), new SimpleAssociationRule(), new MysqliCodec($connection,"table_name"));

它使用 mysqli 将 php 数组插入 mysql 表格。它将使用数组键作为 mysql 表格的列名

$testArray = [
    ["col1" => 2, "col2" => 3, "col3" => "yes"],
    ["col1" => 1, "col2" => 222, "col3" => "apple"],
];
Converter::convert(new ArrayCodec($testArray), new SimpleAssociationRule(), new MysqliCodec($connection,"table_name"));

##编解码器

编解码器类包含对适当格式的编码器和解码器实现。

ArrayCodec

$testArray = [
    ["col1" => 2, "col2" => 3, "col3" => "yes"],
    ["col1" => 1, "col2" => 222, "col3" => "apple"],
];

//If you use ArrayCodec as Decoder you have to specify the input associative array.
$arrayDecoder = new ArrayCodec($testArray);

//If you use ArrayCodec as Coder you can leave the constructor blank.
$arrayCoder = new ArrayCodec();

CsvCodec, XlsCodec, XlsxCodec

//If you use CsvCodec, XlsCodec or XlsxCodec as Decoder you have to specify the input file name.
$csvDecoder = new CsvCodec("test_file.csv");
$xlsDecoder = new XlsCodec("test_file.xls");
$xlsxDecoder = new XlsxCodec("test_file.xlsx");


//If you use CsvCodec, XlsCodec or XlsxCodec as Coder you have two options.

//First option: You specify the file name, thus the coder will write out the table in that file.
Converter::convert(new CsvCodec("test_file.csv"), new SimpleAssociationRule(), new XlsxCodec("new_test_file.xlsx"));

//Second option: If you don't set the filename parameter, the coder will return with the file as a string.
$xls = Converter::convert(new CsvCodec("test_file.csv"), new SimpleAssociationRule(), new XlsxCodec());

MysqliCodec

//If you use MysqliCodec as Coder, you have to set the mysqli connection and the name of the table
$db = new mysqli('localhost', 'user', 'pass', 'demo');
if($db->connect_errno > 0){
    die('Unable to connect to database [' . $db->connect_error . ']');
}
$mysqliCoder = new MysqliCodec($db,"table_name");

//If you use MysqliCodec as Decoder, you have to more options. Firstly, you can use as same as in Coder. It will
//select the whole table with all column.
$mysqliDecoder = new MysqliCodec($db,"table_name");


//Another option is that you write an arbitrary sql query. In this case, you can leave the table name empty.
$mysqliDecoder = new MysqliCodec($db,"","SELECT * FROM table_name WHERE col1 > 5 OR col2 = 44");
$mysqliDecoder2 = new MysqliCodec($db,"","SELECT * FROM table_name JOIN table_name2 ON (table_name.id = table_name2.id)");

##关联规则