XLSWriter

目录

简介

Create xlsx files and set cells and output xlsx files

类摘要

Vtiful\Kernel\Excel

class Vtiful\Kernel\Excel {

/* 方法 */

public addSheet ( string $sheetName )

public autoFilter ( string $scope )

public constMemory ( string $fileName [, string $sheetName ] )

public __construct ( array $config )

public data ( array $data )

public fileName ( string $fileName [, string $sheetName ] )

public getHandle ( void )

public header ( array $headerData )

public insertFormula ( int $row , int $column , string $formula )

public insertImage ( int $row , int $column , string $localImagePath )

public insertText ( int $row , int $column , stringintdouble $data [, string $format ] )

public mergeCells ( string $scope , string $data )

public output ( void )

public setColumn ( string $range , float $width [, resource $format ] )

public setRow ( string $range , float $height [, resource $format ] )

}

Vtiful\Kernel\Excel::addSheet

Vtiful\Kernel\Excel addSheet

说明

public Vtiful\Kernel\Excel::addSheet ( string $sheetName )

Create a new worksheet in the xlsx file.

参数

sheetName
Worksheet name

返回值

Vtiful\Kernel\Excel instance

范例

示例 #1 example

<?php
$config = [
    'path' => './tests'
];

$fileObject  = new \Vtiful\Kernel\Excel($config);

$file = $fileObject->fileName('tutorial.xlsx', 'sheet_one')
    ->header(['name', 'age'])
    ->data([
        ['viest', 23],
        ['wjx', 23]
    ]);

$file->addSheet('sheet_two')
    ->header(['name', 'age'])
    ->data([
        ['james', 33],
        ['king', 33]
    ]);

$file->output();
?>

Vtiful\Kernel\Excel::autoFilter

Vtiful\Kernel\Excel autoFilter

说明

public Vtiful\Kernel\Excel::autoFilter ( string $scope )

Add autofilter to a worksheet.

参数

scope
Cell start and end coordinate string.

返回值

Vtiful\Kernel\Excel instance

范例

示例 #1 example

<?php
$config = [
    'path' => './tests'
];

$fileObject  = new \Vtiful\Kernel\Excel($config);

$file = $excel->fileName('test.xlsx')
        ->header(['name', 'age'])
        ->data($data)
        ->autoFilter('A1:B11')  // auto filter
        ->output();
?>

Vtiful\Kernel\Excel::constMemory

Vtiful\Kernel\Excel constMemory

说明

public Vtiful\Kernel\Excel::constMemory ( string $fileName [, string $sheetName ] )

Write a large file with constant memory usage.

参数

fileName
XLSX file name

sheetName
Worksheet name

返回值

Vtiful\Kernel\Excel instance

范例

示例 #1 example

<?php
$config = [
  'path' => '/home/viest'
];

$fileObject = new \Vtiful\Kernel\Excel($config);

$file = $instance->constMemory('tutorial.xlsx', 'sheet');
?>

Vtiful\Kernel\Excel::__construct

Vtiful\Kernel\Excel constructor

说明

public Vtiful\Kernel\Excel::__construct ( array $config )

Vtiful\Kernel\Excel constructor, create a class object.

参数

config
XLSX file export configuration

返回值

Vtiful\Kernel\Excel instance

范例

示例 #1 example

<?php
$config = [
  'path' => '/home/viest'
];

$excelObject = new \Vtiful\Kernel\Excel($config);
?>

Vtiful\Kernel\Excel::data

Vtiful\Kernel\Excel data

说明

public Vtiful\Kernel\Excel::data ( array $data )

Write a data in the worksheet.

参数

data
worksheet data

返回值

Vtiful\Kernel\Excel instance

范例

示例 #1 example

<?php
$config = [
    'path' => './tests'
];

$fileObject  = new \Vtiful\Kernel\Excel($config);

$file = $fileObject->fileName('tutorial.xlsx', 'sheet_one')
    ->header(['name', 'age'])
    ->data([
      ['viest', 23],
      ['wjx', 23],
    ]);
?>

Vtiful\Kernel\Excel::fileName

Vtiful\Kernel\Excel fileName

说明

public Vtiful\Kernel\Excel::fileName ( string $fileName [, string $sheetName ] )

Create a brand new xlsx file and create a worksheet.

参数

fileName
XLSX file name

sheetName
Worksheet name

返回值

Vtiful\Kernel\Excel instance

范例

示例 #1 example

<?php
$config = [
  'path' => '/home/viest'
];

$fileObject = new \Vtiful\Kernel\Excel($config);

$file = $instance->fileName('tutorial.xlsx', 'sheet');
?>

Vtiful\Kernel\Excel::getHandle

Vtiful\Kernel\Excel getHandle

说明

public Vtiful\Kernel\Excel::getHandle ( void )

Get the xlsx text resource handle.

参数

此函数没有参数。

返回值

Resource

范例

示例 #1 example

<?php
$config = [
    'path' => './tests'
];

$fileObject  = new \Vtiful\Kernel\Excel($config);

$file = $fileObject->fileName('tutorial.xlsx', 'sheet_one')
    ->header(['name', 'age']);

$handle = $file->getHandle();
?>

Vtiful\Kernel\Excel::header

Vtiful\Kernel\Excel header

说明

public Vtiful\Kernel\Excel::header ( array $headerData )

Write a header in the worksheet.

参数

headerData
worksheet header data

返回值

Vtiful\Kernel\Excel instance

范例

示例 #1 example

<?php
$config = [
    'path' => './tests'
];

$fileObject  = new \Vtiful\Kernel\Excel($config);

$file = $fileObject->fileName('tutorial.xlsx', 'sheet_one')
    ->header(['name', 'age']);
?>

Vtiful\Kernel\Excel::insertFormula

Vtiful\Kernel\Excel insertFormula

说明

public Vtiful\Kernel\Excel::insertFormula ( int $row , int $column , string $formula )

Insert calculation formula.

参数

row
cell row

column
cell column

formula
formula string

返回值

Vtiful\Kernel\Excel instance

范例

示例 #1 example

<?php
$config = [
    'path' => './tests'
];

$excel = new \Vtiful\Kernel\Excel($config);

$file = $excel->fileName("free.xlsx")
    ->header(['name', 'money']);

for($index = 1; $index < 10; $index++) {
    $file->insertText($index, 0, 'viest');
    $file->insertText($index, 1, 10);
}

$file->insertText(12, 0, "Total");
$file->insertFormula(12, 1, '=SUM(B2:B11)'); // insert formula

$file->output();

Vtiful\Kernel\Excel::insertImage

Vtiful\Kernel\Excel insertImage

说明

public Vtiful\Kernel\Excel::insertImage ( int $row , int $column , string $localImagePath )

Insert a local image into the cell.

参数

row
cell row

column
cell column

localImagePath
local image path

返回值

Vtiful\Kernel\Excel instance

范例

示例 #1 example

<?php
$config = [
    'path' => './tests'
];

$excel = new \Vtiful\Kernel\Excel($config);

$file = $excel->fileName("free.xlsx");

$file->insertImage(5, 0, '/vagrant/ASW-G-66.jpg');

$file->output();

Vtiful\Kernel\Excel::insertText

Vtiful\Kernel\Excel insertText

说明

public Vtiful\Kernel\Excel::insertText ( int $row , int $column , stringintdouble $data [, string $format ] )

Write text in a cell.

参数

row
cell row

column
cell column

data
data to be written

format
String format

返回值

Vtiful\Kernel\Excel instance

范例

示例 #1 example

<?php
$config = [
    'path' => './tests'
];

$excel = new \Vtiful\Kernel\Excel($config);

$file = $excel->fileName("free.xlsx")
    ->header(['name', 'money']);

for ($index = 0; $index < 10; $index++) {
    $file->insertText($index+1, 0, 'viest');
    $file->insertText($index+1, 1, 10000, '#,##0');
}

$textFile->output();

Vtiful\Kernel\Excel::mergeCells

Vtiful\Kernel\Excel mergeCells

说明

public Vtiful\Kernel\Excel::mergeCells ( string $scope , string $data )

Merge Cells.

参数

scope
cell start and end coordinate strings

data
string data

返回值

Vtiful\Kernel\Excel instance

范例

示例 #1 example

<?php
$config = [
    'path' => './tests'
];

$excel = new \Vtiful\Kernel\Excel($config);

$excel->fileName("test.xlsx")
        ->mergeCells('A1:C1', 'Merge cells')
        ->output();

Vtiful\Kernel\Excel::output

Vtiful\Kernel\Excel output

说明

public Vtiful\Kernel\Excel::output ( void )

Output xlsx file to disk.

参数

此函数没有参数。

返回值

XLSX file path;

范例

示例 #1 example

<?php
$config = [
    'path' => './tests'
];

$fileObject  = new \Vtiful\Kernel\Excel($config);

$file = $fileObject->fileName('tutorial.xlsx', 'sheet_one')
    ->header(['name', 'age'])
    ->data([
      ['viest', 23],
      ['wjx', 23],
    ]);
    
$path = $file->output();
?>

Vtiful\Kernel\Excel::setColumn

Vtiful\Kernel\Excel setColumn

说明

public Vtiful\Kernel\Excel::setColumn ( string $range , float $width [, resource $format ] )

Set the format of the column.

参数

range
cell start and end coordinate strings

width
column width

format
cell format resource

返回值

Vtiful\Kernel\Excel instance

范例

示例 #1 example

<?php
$config = [
    'path' => './tests'
];

$excel  = new \Vtiful\Kernel\Excel($config);

$fileObject = $excel->fileName('tutorial01.xlsx');
$fileHandle = $fileObject->getHandle();

$boldStyle = \Vtiful\Kernel\Format::bold($fileHandle);

$fileObject->header(['name', 'age'])
    ->data([['viest', 21]])
    ->setColumn('A:A', 200, $boldStyle)
    ->output();

Vtiful\Kernel\Excel::setRow

Vtiful\Kernel\Excel setRow

说明

public Vtiful\Kernel\Excel::setRow ( string $range , float $height [, resource $format ] )

Set the format of the column.

参数

range
cell start and end coordinate strings

height
row height

format
cell format resource

返回值

Vtiful\Kernel\Excel instance

范例

示例 #1 example

<?php
$config = [
    'path' => './tests'
];

$excel  = new \Vtiful\Kernel\Excel($config);

$fileObject = $excel->fileName('tutorial01.xlsx');
$fileHandle = $fileObject->getHandle();

$boldStyle = \Vtiful\Kernel\Format::bold($fileHandle);

$fileObject->header(['name', 'age'])
    ->data([['viest', 21]])
    ->setRow('A1', 20, $boldStyle,)
    ->output();

简介

Create a cell format object

类摘要

Vtiful\Kernel\Format

class Vtiful\Kernel\Format {

/* Constants */

const int Vtiful\Kernel\Format::FORMAT_ALIGN_LEFT = 1 ;

const int Vtiful\Kernel\Format::FORMAT_ALIGN_CENTER = 2 ;

const int Vtiful\Kernel\Format::FORMAT_ALIGN_RIGHT = 3 ;

const int Vtiful\Kernel\Format::FORMAT_ALIGN_FILL = 4 ;

const int Vtiful\Kernel\Format::FORMAT_ALIGN_JUSTIFY = 5 ;

const int Vtiful\Kernel\Format::FORMAT_ALIGN_CENTER_ACROSS = 6 ;

const int Vtiful\Kernel\Format::FORMAT_ALIGN_DISTRIBUTED = 7 ;

const int Vtiful\Kernel\Format::FORMAT_ALIGN_VERTICAL_TOP = 8 ;

const int Vtiful\Kernel\Format::FORMAT_ALIGN_VERTICAL_BOTTOM = 9 ;

const int Vtiful\Kernel\Format::FORMAT_ALIGN_VERTICAL_CENTER = 10 ;

const int Vtiful\Kernel\Format::FORMAT_ALIGN_VERTICAL_JUSTIFY = 11 ;

const int Vtiful\Kernel\Format::FORMAT_ALIGN_VERTICAL_DISTRIBUTED = 12 ;

const int Vtiful\Kernel\Format::UNDERLINE_SINGLE = 1 ;

const int Vtiful\Kernel\Format::UNDERLINE_DOUBLE = 2 ;

const int Vtiful\Kernel\Format::UNDERLINE_SINGLE_ACCOUNTING = 3 ;

const int Vtiful\Kernel\Format::UNDERLINE_DOUBLE_ACCOUNTING = 4 ;

/* 方法 */

public align ( resource $handle , int $style )

public bold ( resource $handle )

public italic ( resource $handle )

public underline ( resource $handle , int $style )

}

预定义常量

Vtiful\Kernel\Format::FORMAT_ALIGN_LEFT

Vtiful\Kernel\Format::FORMAT_ALIGN_CENTER

Vtiful\Kernel\Format::FORMAT_ALIGN_RIGHT

Vtiful\Kernel\Format::FORMAT_ALIGN_FILL

Vtiful\Kernel\Format::FORMAT_ALIGN_JUSTIFY

Vtiful\Kernel\Format::FORMAT_ALIGN_CENTER_ACROSS

Vtiful\Kernel\Format::FORMAT_ALIGN_DISTRIBUTED

Vtiful\Kernel\Format::FORMAT_ALIGN_VERTICAL_TOP

Vtiful\Kernel\Format::FORMAT_ALIGN_VERTICAL_BOTTOM

Vtiful\Kernel\Format::FORMAT_ALIGN_VERTICAL_CENTER

Vtiful\Kernel\Format::FORMAT_ALIGN_VERTICAL_JUSTIFY

Vtiful\Kernel\Format::FORMAT_ALIGN_VERTICAL_DISTRIBUTED

Vtiful\Kernel\Format::UNDERLINE_SINGLE

Vtiful\Kernel\Format::UNDERLINE_DOUBLE

Vtiful\Kernel\Format::UNDERLINE_SINGLE_ACCOUNTING

Vtiful\Kernel\Format::UNDERLINE_DOUBLE_ACCOUNTING

Vtiful\Kernel\Format::align

Vtiful\Kernel\Format align

说明

public Vtiful\Kernel\Format::align ( resource $handle , int $style )

set cell align

参数

handle
xlsx file handle

style
Vtiful\Kernel\Format constant

返回值

Resource

范例

示例 #1 example

<?php
$config = [
    'path' => './tests'
];

$excel  = new \Vtiful\Kernel\Excel($config);

$fileObject = $excel->fileName('tutorial01.xlsx');
$fileHandle = $fileObject->getHandle();

$alignStyle = \Vtiful\Kernel\Format::align($fileHandle, \Vtiful\Kernel\Format::FORMAT_ALIGN_LEFT);

$fileObject->header(['name', 'age'])
    ->data([['viest', 21]])
    ->setColumn('A:A', 200, $align)
    ->output();
?>

Vtiful\Kernel\Format::bold

Vtiful\Kernel\Format bold

说明

public Vtiful\Kernel\Format::bold ( resource $handle )

Vtiful\Kernel\Format bold format

参数

handle
xlsx file handle

返回值

Resource

范例

示例 #1 example

<?php
$config = [
    'path' => './tests'
];

$excel  = new \Vtiful\Kernel\Excel($config);

$fileObject = $excel->fileName('tutorial01.xlsx');
$fileHandle = $fileObject->getHandle();

$boldStyle = \Vtiful\Kernel\Format::bold($fileHandle);

$fileObject->header(['name', 'age'])
    ->data([['viest', 21]])
    ->setColumn('A:A', 200, $boldStyle)
    ->output();
?>

Vtiful\Kernel\Format::italic

Vtiful\Kernel\Format italic

说明

public Vtiful\Kernel\Format::italic ( resource $handle )

Vtiful\Kernel\Format italic format

参数

handle
xlsx file handle

返回值

Resource

范例

示例 #1 example

<?php
$config = [
    'path' => './tests'
];

$excel  = new \Vtiful\Kernel\Excel($config);

$fileObject = $excel->fileName('tutorial01.xlsx');
$fileHandle = $fileObject->getHandle();

$italicStyle = \Vtiful\Kernel\Format::italic($fileHandle);

$fileObject->header(['name', 'age'])
    ->data([['viest', 21]])
    ->setColumn('A:A', 200, $italicStyle)
    ->output();
?>

Vtiful\Kernel\Format::underline

Vtiful\Kernel\Format underline

说明

public Vtiful\Kernel\Format::underline ( resource $handle , int $style )

Vtiful\Kernel\Format underline format

参数

handle
xlsx file handle

style
Vtiful\Kernel\Format constant

返回值

Resource

范例

示例 #1 example

<?php
$config = [
    'path' => './tests'
];

$excel  = new \Vtiful\Kernel\Excel($config);

$fileObject = $excel->fileName('tutorial01.xlsx');
$fileHandle = $fileObject->getHandle();

$underlineStyle = \Vtiful\Kernel\Format::underline($fileHandle, \Vtiful\Kernel\Format::UNDERLINE_SINGLE);

$fileObject->header(['name', 'age'])
    ->data([['viest', 21]])
    ->setColumn('A:A', 200, $underlineStyle)
    ->output();
?>