You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
1.9 KiB
59 lines
1.9 KiB
<?php
|
|
require 'vendor/autoload.php';
|
|
namespace PhpOffice\PhpSpreadsheet;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\IReader;
|
|
use PhpOffice\PhpSpreadsheet\Shared\File;
|
|
use PhpOffice\PhpSpreadsheet\Writer\IWriter;
|
|
|
|
/**
|
|
* Factory to create readers and writers easily.
|
|
*
|
|
* It is not required to use this class, but it should make it easier to read and write files.
|
|
* Especially for reading files with an unknown format.
|
|
*/
|
|
abstract class IOFactory
|
|
{
|
|
public const READER_XLSX = 'Xlsx';
|
|
public const READER_XLS = 'Xls';
|
|
public const READER_XML = 'Xml';
|
|
public const READER_ODS = 'Ods';
|
|
public const READER_SYLK = 'Slk';
|
|
public const READER_SLK = 'Slk';
|
|
public const READER_GNUMERIC = 'Gnumeric';
|
|
public const READER_HTML = 'Html';
|
|
public const READER_CSV = 'Csv';
|
|
|
|
public const WRITER_XLSX = 'Xlsx';
|
|
public const WRITER_XLS = 'Xls';
|
|
public const WRITER_ODS = 'Ods';
|
|
public const WRITER_CSV = 'Csv';
|
|
public const WRITER_HTML = 'Html';
|
|
|
|
/** @var string[] */
|
|
private static $readers = [
|
|
self::READER_XLSX => Reader\Xlsx::class,
|
|
self::READER_XLS => Reader\Xls::class,
|
|
self::READER_XML => Reader\Xml::class,
|
|
self::READER_ODS => Reader\Ods::class,
|
|
self::READER_SLK => Reader\Slk::class,
|
|
self::READER_GNUMERIC => Reader\Gnumeric::class,
|
|
self::READER_HTML => Reader\Html::class,
|
|
self::READER_CSV => Reader\Csv::class,
|
|
];
|
|
|
|
public static function createReader(string $readerType): IReader
|
|
{
|
|
if (!isset(self::$readers[$readerType])) {
|
|
throw new Reader\Exception("No reader found for type $readerType");
|
|
}
|
|
|
|
// Instantiate reader
|
|
/** @var IReader */
|
|
$className = self::$readers[$readerType];
|
|
|
|
return new $className();
|
|
}
|
|
$object = new IOFactory();
|
|
$spreadsheet = $object::createReader('Xlsx')->load('1.xlsx');
|
|
echo "success";
|