fdmsantos/exceltofiles

一个简单的库,用于通过Excel行创建文件

v1.1 2018-01-08 17:31 UTC

This package is auto-updated.

Last update: 2024-09-11 00:16:45 UTC


README

fdmsantos/exceltofiles 是一个简单的库,可以根据模板通过Excel行创建文件。

如何使用

我们需要提供一个模板文件和Excel文件。

模板文件示例

Hello my name is [name].
I'm from [country] and have [age] years old!

Excel文件示例

代码示例

require_once('vendor/autoload.php');

use ExcelToFiles\ExcelToFiles;

$exceltoFiles = new ExcelToFiles([
  'template' => 'template.txt',
  'excel'    => 'excel.xls',
  'mapping'  => [
    '[name]' => 'A'
    '[country]' => 'B'
    '[age]' => 'C'
]);

$exceltoFiles->generate();

上述代码的结果将是

文件 1

Hello my name is Fabio.
I'm from Portugal and have 31 years old!

文件 2

Hello my name is John.
I'm from Spain and have 23 years old!

文件 3

Hello my name is Peter.
I'm from England and have 65 years old!

可选参数

require_once('vendor/autoload.php');

use ExcelToFiles\ExcelToFiles;

$exceltoFiles = new ExcelToFiles([
  'template' => '{template.txt}',
  'excel'    => '{excel.xls}',
  'excludeRows' => [1,2,13], // To exclude Rows. This example wil exclude row 1, 2 and 13
  'filesname' => 'person_{A}.txt', // To define filename. The name can depends from excel row. For this it's necessary use {column}.
  'outputdir' => 'src/', // To choose files path. The Default is current path.
  'mapping'  => [
    '[name]' => 'A'
    '[country]' => 'B'
    '[age]' => 'C'
]);

闭包

如果模板变量具有相同的逻辑或依赖于两个或更多列,我们可以使用闭包。

// One Clousure for Variable
$exceltoFiles->mapWithClosure('[name]',function($columns) {
	return $columns['A'].' => '.$columns['B'];
});

$exceltoFiles->generate();