定制 jarir-ahmed/data-converter 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

jarir-ahmed/data-converter

Composer 安装命令:

composer require jarir-ahmed/data-converter

包简介

6-way converter: CSV ↔ XLSX ↔ JSON ↔ MySQL. Import/export data between any format, generate SQL INSERTs, or connect directly via PDO.

README 文档

README

6-way converter — CSV ↔ XLSX ↔ JSON ↔ MySQL. Import/export data between any format, generate SQL INSERT statements, or connect directly via PDO.

composer require jarir-ahmed/data-converter

Matrix

From \ To CSV XLSX JSON MySQL
CSV
XLSX
JSON
MySQL

Quick Start

Import: any file → SQL / direct DB insert

use JarirAhmed\DataConverter\ImportManager;

$import = new ImportManager();

$sql = $import->csvToSql('users.csv', 'users');    // CSV → SQL
$sql = $import->xlsxToSql('orders.xlsx', 'orders'); // XLSX → SQL
$sql = $import->jsonToSql('data.json', 'products'); // JSON → SQL

// Auto-detect by extension
$sql = $import->fileToSql('data.csv',  'my_table');
$sql = $import->fileToSql('data.xlsx', 'my_table');
$sql = $import->fileToSql('data.json', 'my_table');

// Direct DB insert via PDO
$pdo = new PDO('mysql:host=127.0.0.1;dbname=test-excel;charset=utf8mb4',
    'root', 'your_secure_password');
$import->csvToSql('data.csv', 'users', ['pdo' => $pdo]);

Export: SQL / array → any format

use JarirAhmed\DataConverter\ExportManager;

$pdo    = new PDO('mysql:host=127.0.0.1;dbname=test-excel', 'root', 'your_secure_password');
$export = new ExportManager();

$export->sqlToCsv('SELECT * FROM users', $pdo, 'users.csv');
$export->sqlToXlsx('SELECT * FROM orders', $pdo, 'orders.xlsx');
$export->sqlToJson('SELECT * FROM products', $pdo, 'products.json');

// Or from arrays:
$export->dataToCsv([...], 'out.csv');
$export->dataToXlsx([...], 'out.xlsx');
$export->dataToJson([...], 'out.json');

Convert: format ↔ format (no database)

use JarirAhmed\DataConverter\Converter;

$conv = new Converter();

$conv->csvToXlsx('data.csv', 'data.xlsx');
$conv->xlsxToCsv('data.xlsx', 'data.csv');
$conv->csvToJson('data.csv', 'data.json');
$conv->jsonToCsv('data.json', 'data.csv');
$conv->xlsxToJson('report.xlsx', 'report.json');
$conv->jsonToXlsx('data.json', 'data.xlsx');

JSON Formats

Import supports two formats:

Array of objects (default):

[{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}]

Structured {columns, rows}:

{"columns": ["name", "age"], "rows": [["Alice", 30], ["Bob", 25]]}

Export supports both — pass 'structured' => true for the structured format.

Options

ImportManager

Option Type Default Description
pdo PDO|null null PDO for direct DB insert
skipExecute bool false Return SQL even with PDO
batchSize int 50 Rows per INSERT block
hasHeader bool true First row is column names
columns string[] [] Force column names
delimiter string , CSV delimiter
enclosure string " CSV enclosure
ignore bool false Use INSERT IGNORE
emptyAsNull bool true Empty cells → NULL
preview int 0 Only process N rows
onDuplicateKey string null ON DUPLICATE KEY UPDATE
sheetName string first Sheet name (xlsxToSql)
formatDates bool true Excel serial → YYYY-MM-DD
rootPath string null JSON nested path like data.items

ExportManager

Option Type Default Description
delimiter string , CSV delimiter
enclosure string " CSV enclosure
includeHeaders bool true Include CSV header row
utf8Bom bool false UTF-8 BOM for Excel
sheetName string Data XLSX sheet name
styled bool true Blue header styling
pretty bool true Pretty-print JSON
structured bool false {columns, rows} format

Local Testing

composer install
composer test

Test database (manual verification):

Host Database User Password Port
localhost test-excel root your_secure_password 3306
CREATE DATABASE IF NOT EXISTS `test-excel` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

License

MIT

6-way-data-converter

统计信息

  • 总下载量: 1
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-07-09

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固