renekorss/phpexcelformatter

PHPExcelFormatter 是一个类,用于简化从 Excel 文档中获取数据。

3.0.1 2020-08-10 10:30 UTC

This package is auto-updated.

Last update: 2024-09-16 20:34:31 UTC


README

Actions Status Coverage Status Latest Stable Version Total Downloads License

PHPExcelFormatter

PHPExcelFormatter 是一个类,用于简化从 Excel 文档中获取数据。

  • 读取您真正需要的列
  • 为没有第一行列名的文档设置列名
  • 为列设置您的数据库字段名
  • 以数组或 MySQL 查询格式检索数据
  • 非常适合导入文件,然后让用户将文档列与您的数据库字段连接起来 :) (示例即将到来)

安装

composer require renekorss/phpexcelformatter

用法

// Require needed files
require __DIR__ . '/vendor/autoload.php';

use RKD\PHPExcelFormatter\PHPExcelFormatter;
use RKD\PHPExcelFormatter\Exception\PHPExcelFormatterException;

try {
  // Load file
  $formatter = new PHPExcelFormatter('example1.xls');

  // Output columns array (document must have column names on first row)
  $formatterColumns = array(
    'username' => 'username',
    'phone'    => 'phone_no',
    'email'    => 'email_address'
  );

  // Output columns array (document dosen't have column names on first row)
  // Skip fourth column (age) (third in array), because we don't need that data
  // NOTE: if document dosen't have column names on first line, second parameter for PHPExcelFormatter should be $readColumns = false, otherwise it will skip first line of data
  $formatterColumns = array(
    'username',
    'email_address',
    'phone',
    4 => 'sex'
  );

  // Set our columns
  $formatter->setFormatterColumns($formatterColumns);

  // Output as array
  $output = $formatter->output('a');
  // OR
  // $output = $formatter->output('array');

  // Print array
  echo '<pre>'.print_r($output, true).'</pre>';

  // Set MySQL table
  $formatter->setMySQLTableName('users');

  // Output as mysql query
  $output = $formatter->output('m');
  // OR
  // $output = $formatter->output('mysql');

  // Print mysql query
  echo '<pre>'.print_r($output, true).'</pre>';

} catch (PHPExcelFormatterException $e) {
  echo 'Error: '.$e->getMessage();
}

查看 示例

许可证

PHPExcelFormatter 使用 MIT 许可证。