Разбил файл exceltable на классы

This commit is contained in:
origami11 2017-02-17 17:08:18 +03:00
parent b26e521657
commit 7ce493414e
5 changed files with 135 additions and 132 deletions

View file

@ -1,7 +1,5 @@
<?php
require_once 'tools/string.php';
class Database_PDOStatement extends PDOStatement implements IteratorAggregate
{
protected $cursorPos = 0;
@ -88,7 +86,7 @@ class Database_PDOStatement extends PDOStatement implements IteratorAggregate
}
function getArray($name) {
return strToArray($this->fields[$name]);
return Tools_String::strToArray($this->fields[$name]);
}
function getRecordCount() {

16
src/Excel/DataTime.php Normal file
View file

@ -0,0 +1,16 @@
<?php
class Excel_DateTime
{
public $value;
function __construct($value)
{
$this->value = intval($value);
}
function getString()
{
return date('Y-m-d\TH:i:s.u', $this->value);
}
}

100
src/Excel/Document.php Normal file
View file

@ -0,0 +1,100 @@
<?php
/**
* Документ
*/
class Excel_Document {
static $ns = "urn:schemas-microsoft-com:office:spreadsheet";
private $table = array ();
protected $styles = array();
function addTable($table) {
$this->table [] = $table;
}
/**
* Добавление стиля к документу
* @param $name string Имя стиля
* @param $values array Параметры стиля
* @param $type Тип стиля
*/
function setStyle ($name, array $values, $type = 'Interior')
{
if(!isset($this->styles[$name])) {
$this->styles[$name] = array();
}
$this->styles[$name][$type] = $values;
}
/**
* Генерация стилей
*/
private function createStyles (XMLWriter $doc) {
$doc->startElement('Styles');
foreach ($this->styles as $name => $sn) {
$doc->startElement('Style');
$doc->writeAttribute('ss:ID', $name);
foreach ($sn as $type => $s) {
// Стиль Borders - составной
if ($type == 'Borders') {
$doc->startElement('Borders');
foreach ($s as $border) {
/*.array.*/$border = $border;
$doc->startElement('Border');
foreach ($border as $key => $value) {
$doc->writeAttribute('ss:' . $key, $value);
}
$doc->endElement();
}
$doc->endElement();
} else {
$doc->startElement($type);
foreach ($s as $key => $value) {
$doc->writeAttribute('ss:' . $key, $value);
}
$doc->endElement();
}
}
$doc->endElement();
}
$doc->endElement();
}
/**
* Преобразует переводы строки в спец символы
*/
function clean ($s) {
assert(is_string($s));
return strtr($s, array ("\n" => "&#10;"));
}
/**
* Сохраняет таблицу в формате Office 2003 XML
* http://en.wikipedia.org/wiki/Microsoft_Office_XML_formats
*/
function save($filename)
{
$doc = new XMLWriter();
$doc->openURI($filename);
$doc->setIndent(false);
$doc->startDocument('1.0','utf-8');
$doc->startElement('Workbook');
$doc->writeAttribute('xmlns', self::$ns);
$doc->writeAttribute('xmlns:ss', self::$ns);
$this->createStyles($doc);
foreach ($this->table as $table) {
if ($table instanceof Excel_Table) {
$table->createTable($doc);
} else {
$table_data = call_user_func($table);
$table_data->createTable($doc);
unset($table_data);
}
}
$doc->endElement();
}
}

17
src/Excel/Number.php Normal file
View file

@ -0,0 +1,17 @@
<?php
class Excel_Number
{
public $value;
function __construct($value)
{
$this->value = intval($value);
}
function getString()
{
return $this->value;
}
}

View file

@ -1,35 +1,5 @@
<?php
class Excel_Number
{
public $value;
function __construct($value)
{
$this->value = intval($value);
}
function getString()
{
return $this->value;
}
}
class Excel_DateTime
{
public $value;
function __construct($value)
{
$this->value = intval($value);
}
function getString()
{
return date('Y-m-d\TH:i:s.u', $this->value);
}
}
/**
* Клетка таблицы
*/
@ -68,7 +38,7 @@ class TableRow
/**
* Таблица
*/
class ExcelTable
class Excel_Table
{
static $index;
private $name;
@ -339,101 +309,3 @@ class ExcelTable
}
}
/**
* Документ
*/
class ExcelDocument {
static $ns = "urn:schemas-microsoft-com:office:spreadsheet";
private $table = array ();
protected $styles = array();
function addTable($table) {
$this->table [] = $table;
}
/**
* Добавление стиля к документу
* @param $name string Имя стиля
* @param $values array Параметры стиля
* @param $type Тип стиля
*/
function setStyle ($name, array $values, $type = 'Interior')
{
if(!isset($this->styles[$name])) {
$this->styles[$name] = array();
}
$this->styles[$name][$type] = $values;
}
/**
* Генерация стилей
*/
private function createStyles (XMLWriter $doc) {
$doc->startElement('Styles');
foreach ($this->styles as $name => $sn) {
$doc->startElement('Style');
$doc->writeAttribute('ss:ID', $name);
foreach ($sn as $type => $s) {
// Стиль Borders - составной
if ($type == 'Borders') {
$doc->startElement('Borders');
foreach ($s as $border) {
/*.array.*/$border = $border;
$doc->startElement('Border');
foreach ($border as $key => $value) {
$doc->writeAttribute('ss:' . $key, $value);
}
$doc->endElement();
}
$doc->endElement();
} else {
$doc->startElement($type);
foreach ($s as $key => $value) {
$doc->writeAttribute('ss:' . $key, $value);
}
$doc->endElement();
}
}
$doc->endElement();
}
$doc->endElement();
}
/**
* Преобразует переводы строки в спец символы
*/
function clean ($s) {
assert(is_string($s));
return strtr($s, array ("\n" => "&#10;"));
}
/**
* Сохраняет таблицу в формате Office 2003 XML
* http://en.wikipedia.org/wiki/Microsoft_Office_XML_formats
*/
function save($filename)
{
$doc = new XMLWriter();
$doc->openURI($filename);
$doc->setIndent(false);
$doc->startDocument('1.0','utf-8');
$doc->startElement('Workbook');
$doc->writeAttribute('xmlns', self::$ns);
$doc->writeAttribute('xmlns:ss', self::$ns);
$this->createStyles($doc);
foreach ($this->table as $table) {
if ($table instanceof ExcelTable) {
$table->createTable($doc);
} else {
$table_data = call_user_func($table);
$table_data->createTable($doc);
unset($table_data);
}
}
$doc->endElement();
}
}