This commit is contained in:
steven 2025-08-11 22:23:30 +02:00
commit 72a26edcff
22092 changed files with 2101903 additions and 0 deletions

View file

@ -0,0 +1,124 @@
<?php
namespace SimpleExcel\Writer;
/**
* SimpleExcel base class for writing spreadsheet
*
* @author Faisalman
* @package SimpleExcel
*/
abstract class BaseWriter implements IWriter
{
/**
* Holds tabular data
*
* @access protected
* @var array
*/
protected $tabl_data;
/**
* Defines content-type for HTTP header
*
* @access protected
* @var string
*/
protected $content_type = 'text';
/**
* Defines file extension to be used when saving file
*
* @access protected
* @var string
*/
protected $file_extension = 'txt';
/**
* @return void
*/
public function __construct(){
$this->tabl_data = array();
}
/**
* Adding row data to table
*
* @param array $values An array contains ordered value for every cell
* @param bool Check if row goes at the beginning or end of array
* @return void
*/
public function addRow($values, $end = TRUE)
{
if (!is_array($values)) {
$values = array($values);
}
if ($end) {
array_push($this->tabl_data, $values);
return;
}
array_unshift($this->tabl_data, $values);
}
/**
* Get document content as string
*
* @return string Content of document
*/
public function saveString(){
$content = '';
foreach ($this->tabl_data as $row) {
foreach ($row as $cell) {
$content .= $cell.'\t';
}
$content .= '\n';
}
return $content;
}
/**
* Export the document
*
* @param string $filename Name for the saved file (extension will be set automatically)
* @param string $target Save location
* @return void
*/
public function saveFile($filename, $target = NULL){
if (!isset($filename)) {
$filename = date('YmdHis');
}
if (!isset($target)) {
// write output to browser
$target = 'php://output';
// set HTTP response header
header('Content-Type: '.$this->content_type);
header('Content-Disposition: attachment; filename='.$filename.'.'.$this->file_extension);
}
$fp = fopen($target, 'w');
fwrite($fp, $this->saveString());
fclose($fp);
if ($target == 'php://output') {
// since there must be no data below
exit();
}
}
/**
* Set tabular data
*
* @param array $values An array contains ordered value of arrays for all fields
* @return void
*/
public function setData($values){
if(!is_array($values)){
$values = array($values);
}
$this->tabl_data = $values;
}
}
?>

View file

@ -0,0 +1,63 @@
<?php
namespace SimpleExcel\Writer;
/**
* SimpleExcel class for writing CSV Spreadsheet
*
* @author Faisalman
* @package SimpleExcel
*/
class CSVWriter extends BaseWriter implements IWriter
{
/**
* Defines content-type for HTTP header
*
* @access protected
* @var string
*/
protected $content_type = 'text/csv';
/**
* Defines delimiter char
*
* @access protected
* @var string
*/
protected $delimiter = ',';
/**
* Defines file extension to be used when saving file
*
* @access protected
* @var string
*/
protected $file_extension = 'csv';
/**
* Get document content as string
*
* @return string Content of document
*/
public function saveString(){
$fp = fopen('php://temp', 'r+');
foreach ($this->tabl_data as $row) {
fputcsv($fp, $row, $this->delimiter);
}
rewind($fp);
$content = stream_get_contents($fp);
fclose($fp);
return $content;
}
/**
* Set character for delimiter
*
* @param string $delimiter Commonly used character can be a comma, semicolon, tab, or space
* @return void
*/
public function setDelimiter($delimiter){
$this->delimiter = $delimiter;
}
}
?>

View file

@ -0,0 +1,46 @@
<?php
namespace SimpleExcel\Writer;
/**
* SimpleExcel class for writing HTML table
*
* @author Faisalman
* @package SimpleExcel
*/
class HTMLWriter extends BaseWriter implements IWriter
{
/**
* Defines content-type for HTTP header
*
* @access protected
* @var string
*/
protected $content_type = 'text/html';
/**
* Defines file extension to be used when saving file
*
* @access protected
* @var string
*/
protected $file_extension = 'html';
/**
* Get document content as string
*
* @return string Content of document
*/
public function saveString(){
$content = '<table>';
foreach ($this->tabl_data as $row) {
$content .= '<tr>';
foreach ($row as $cell) {
$content .= '<td>'.$cell.'</td>';
}
$content .= '</tr>';
}
return $content.'</table>';
}
}
?>

View file

@ -0,0 +1,21 @@
<?php
namespace SimpleExcel\Writer;
/**
* Defines SimpleExcel writer interface
*
* @author Faisalman
* @package SimpleExcel
*/
/** define writer interface */
interface IWriter
{
public function addRow($values);
public function saveString();
public function saveFile($filename, $target);
public function setData($values);
}
?>

View file

@ -0,0 +1,46 @@
<?php
namespace SimpleExcel\Writer;
/**
* SimpleExcel class for writing table as JSON
*
* @author Faisalman
* @package SimpleExcel
*/
class JSONWriter extends BaseWriter implements IWriter
{
/**
* Defines content-type for HTTP header
*
* @access protected
* @var string
*/
protected $content_type = 'application/json';
/**
* Defines file extension to be used when saving file
*
* @access protected
* @var string
*/
protected $file_extension = 'json';
/**
* Get document content as string
*
* @return string Content of document
*/
public function saveString(){
$json = array();
foreach ($this->tabl_data as $row) {
$row_array = array();
for ($i = 0; $i < count($row); $i++) {
$row_array[$i] = $row[$i];
}
array_push($json, (object)$row);
}
return json_encode($json);
}
}
?>

View file

@ -0,0 +1,44 @@
<?php
namespace SimpleExcel\Writer;
/**
* SimpleExcel class for writing TSV Spreadsheet
*
* @author Faisalman
* @package SimpleExcel
*/
class TSVWriter extends CSVWriter implements IWriter
{
/**
* Defines content-type for HTTP header
*
* @access protected
* @var string
*/
protected $content_type = 'text/tab-separated-values';
/**
* Defines delimiter char
*
* @access protected
* @var string
*/
protected $delimiter = "\t";
/**
* Defines file extension to be used when saving file
*
* @access protected
* @var string
*/
protected $file_extension = 'tsv';
/**
* Override parent class, this method is ignored in TSV
*/
public function setDelimiter($delimiter){
// do nothing
}
}
?>

View file

@ -0,0 +1,76 @@
<?php
namespace SimpleExcel\Writer;
/**
* SimpleExcel class for writing Microsoft Excel XLSX Spreadsheet
*
* @author Faisalman
* @package SimpleExcel
*/
class XLSXWriter extends BaseWriter implements IWriter
{
/**
* Defines content-type for HTTP header
*
* @access protected
* @var string
*/
protected $content_type = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
/**
* Defines file extension to be used when saving file
*
* @access protected
* @var string
*/
protected $file_extension = 'xlsx';
/**
* Array containing document properties
*
* @access private
* @var array
*/
private $doc_prop;
/**
* @return void
*/
public function __construct() { }
/**
* Adding row data to XLSX
*
* @param array $values An array contains ordered value for every cell
* @return void
*/
public function addRow($values) { }
/**
* Get document content as string
*
* @return string Content of document
*/
public function saveString() { }
/**
* Set XLSX data
*
* @param array $values An array contains ordered value of arrays for all fields
* @return void
*/
public function setData($values) { }
/**
* Set a document property of the XLSX
*
* @param string $prop Document property to be set
* @param string $val Value of the document property
* @return void
*/
public function setDocProp($prop, $val) {
$this->doc_prop[$prop] = $val;
}
}
?>

View file

@ -0,0 +1,146 @@
<?php
namespace SimpleExcel\Writer;
/**
* SimpleExcel class for writing Microsoft Excel 2003 XML Spreadsheet
*
* @author Faisalman
* @package SimpleExcel
*/
class XMLWriter extends BaseWriter implements IWriter
{
/**
* Defines content-type for HTTP header
*
* @access protected
* @var string
*/
protected $content_type = 'application/xml';
/**
* Defines file extension to be used when saving file
*
* @access protected
* @var string
*/
protected $file_extension = 'xml';
/**
* Array containing document properties
*
* @access private
* @var array
*/
private $doc_prop;
/**
* @return void
*/
public function __construct(){
$this->doc_prop = array(
'Author' => 'SimpleExcel',
'Company' => 'SimpleExcel',
'Created' => gmdate("Y-m-d\TH:i:s\Z"),
'Keywords' => 'SimpleExcel',
'LastAuthor' => 'SimpleExcel',
'Version' => '12.00'
);
}
/**
* Adding row data to XML
*
* @param array $values An array contains ordered value for every cell
* @return void
*/
public function addRow($values){
$row = &$this->tabl_data;
$row .= '
<Row ss:AutoFitHeight="0">';
foreach($values as $val){
$value = '';
$datatype = 'String';
// check if given variable contains array
if(is_array($val)){
$value = $val[0];
$datatype = $val[1];
} else {
$value = $val;
$datatype = is_numeric($val) ? 'Number' : 'String';
}
// escape value from HTML tags
$value = filter_var($value, FILTER_SANITIZE_SPECIAL_CHARS);
$row .= '
<Cell><Data ss:Type="'.$datatype.'">'.$value.'</Data></Cell>';
}
$row .= '
</Row>';
}
/**
* Get document content as string
*
* @return string Content of document
*/
public function saveString(){
$content = '<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">';
foreach($this->doc_prop as $propname => $propval){
$content .= '
<'.$propname.'>'.$propval.'</'.$propname.'>';
}
$content .= '
</DocumentProperties>
<Worksheet ss:Name="Sheet1">
<Table>'.$this->tabl_data.'
</Table>
</Worksheet>
</Workbook>';
return $content;
}
/**
* Set XML data
*
* @param array $values An array contains ordered value of arrays for all fields
* @return void
*/
public function setData($values){
if(!is_array($values)){
$values = array($values);
}
$this->tabl_data = ""; // reset the xml data.
// append values as rows
foreach ($values as $value) {
$this->addRow($value);
}
}
/**
* Set a document property of the XML
*
* @param string $prop Document property to be set
* @param string $val Value of the document property
* @return void
*/
public function setDocProp($prop, $val){
$this->doc_prop[$prop] = $val;
}
}
?>