datatable / core
DataTable 核心和命令行工具
v1.1.0
2018-02-17 12:15 UTC
Requires (Dev)
- symfony/console: ~3.0
This package is auto-updated.
Last update: 2024-08-29 03:58:00 UTC
README
表示内存中的表数据,如
- 数据库表
- 数据库结果集
- CSV 文件
- Excel 文件
- 数据网格
- ...等等
为什么使用 DataTable 库?
- 您需要将来自不同格式的最终用户数据导入到您的系统吗?
- 您需要将数据从您的系统导出到不同的格式吗?
- 您曾经将 CSV 数据导入到数据库,然后再从 Excel、yaml、xml 等导入过吗?
- 您曾经将数据从数据库导出到 CSV,然后再导出到 Excel、yaml、xml、pdf 等格式吗?
这就是 "DataTable" 的用武之地。它位于您的导入器、导出器和应用程序数据之间。
它允许您编写真正出色的
- 基于 DataTables 的导入器
- 基于 DataTables 的导出器
如果您基于 DataTable 编写导入器(而不是直接基于 .csv、.xls、.yaml 等),那么您可以一次编写导入器,并支持 DataTable 支持的所有导入格式。
如果您基于 DataTable 编写导出器(而不是直接基于 .csv、.xls、.yaml 等),那么您可以一次编写导出器,并支持 DataTable 支持的所有导出格式。
如何从代码中加载数据到表格
use DataTable\Core\Table; use DataTable\Core\Writer\Csv as CsvWriter; $table = new Table(); $table->setName("My data"); // Give it a user-friendly name $namecolumn = $table->getColumnByName("name"); $emailcolumn = $table->getColumnByName("email"); // get the first row (index 0) $row = $table->getRowByIndex(0); // get a cell by columnname: $cell = $row->getCellByColumnName("name"); // assign value to the cell $cell->setValue("Joe Johnson"); // do the same for the second cell (email) $cell = $row->getCellByColumnName("email"); $cell->setValue("joe@johnson.web"); // do the same for a second row (index 1) $row = $table->getRowByIndex(1); $cell = $row->getCellByColumnName("name"); $cell->setValue("John Jackson"); $cell = $row->getCellByColumnName("email"); $cell->setValue("john@jackson.web"); // use a writer to export the datatable to a .csv file $writer = new CsvWriter(); $output = $writer->write($table); echo $output;
如何从代码中读取数据
use DataTable\Core\Table; use DataTable\Core\Reader\Csv as CsvReader; // Create the DataTable\Core\Table object $table = new Table(); $table->setName("My user data"); // Give it a user-friendly name $reader->loadFile($table, "users.csv"); // Loop through all the rows in $table foreach($table->getRows() as $row) { // Read field contents from the row by columnname $name = $row->getValueByColumnName("name"); $email = $row->getValueByColumnName("email"); // use the data, for example: // ensure database record for user with name+email }
如何使用读取器和写入器进行导入/导出数据
use DataTable\Core\Table; use DataTable\Core\Reader\Csv as CsvReader; use DataTable\Core\Writer\AsciiTable as AsciiTableWriter; // Create the DataTable\Core\Table object $table = new Table(); $table->setName(basename($inputfile)); // Give it a user-friendly name // Instantiate a Reader, in this case a .csv file reader $reader = new CsvReader(); $reader->setSeperator(','); $reader->loadFile($table, $inputfile); // The $table now contains data from the .csv file // Instantiate a Writer, in this case an Ascii table writer $writer = new AsciiTableWriter(); $output = $writer->write($table); echo $output;
如您所见,DataTable\Core\Table
对象用于加载数据,然后导出数据。
如果您需要将数据从您的系统导出到不同的格式,那么您需要做的只是
- 将数据从您的系统加载到 DataTable 中
- 使用众多写入器之一将数据导出到不同的格式
如果您需要将来自不同格式的数据导入到您的系统,那么您需要做的只是
- 使用众多读取器之一将数据从任何支持的格式导入到 DataTable 中
- 编写从 DataTable 到您的系统的导入器