Scripts

Excelsheet

Een simpel PHP script om een .XLS bestand te kunnen genereren. Handig voor o.a. exporteren van gegevens van uit je database. Voor situaties waar CSV bestanden te ingewikkeld zijn voor de eindgebruikers ;) Compatible met o.a. Microsoft Office Excel 1997-2007 en Open Office Calc. Voorbeeld:

excelsheet.cls.php
<?php

/*
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

	class Excelsheet
	{
		private $aData = array();
		private $iRowNumber = 0;

		public function __construct()
		{
		}

		// Insert cell value
		public function setCell($iRow, $iCol, $sValue = '')
		{
			// Add rows when required
			for($i = 0; $i <= $iRow; $i++)
			{
				if(isset($this->aData[$i]) == false)
				{
					$this->aData[$i] = array();
				}
			}

			// Add columns when required
			for($i = 0; $i <= $iCol; $i++)
			{
				if(isset($this->aData[$iRow][$i]) == false)
				{
					$this->aData[$iRow][$i] = '';
				}
			}

			// Assign value
			$this->aData[$iRow][$iCol] = $sValue;
		}

		// Insert row
		public function setRow($iRow, $aRow = array(''))
		{
			// Add rows when required
			for($i = 0; $i <= $iRow; $i++)
			{
				if(isset($this->aData[$i]) == false)
				{
					$this->aData[$i] = array();
				}
			}

			// Assign row
			$this->aData[$iRow] = $aRow;
		}

		// Append row (1d array)
		public function addRow($aRow)
		{
			if(is_array($aRow))
			{
				$this->aData[] = $aRow;
			}
			else
			{
				$this->aData[][0] = $aRow;
			}
		}

		// Append multiple rows (2d array)
		public function addData($aData)
		{
			if(is_array($aData) && sizeof($aData))
			{
				foreach($aData as $k => $v)
				{
					$this->addRow($v);
				}
			}
		}
		
		public function saveFile($sFilePath)
		{
			$sData = $this->buildSheet();

			@touch($sFilePath);
			@chmod($sFilePath, 0777);

			if(@file_put_contents($sFilePath, $sData))
			{
				return true;
			}

			return false;
		}

		public function sendFile($sFileName)
		{
			$sData = $this->buildSheet();

			header('Pragma: public');
			header('Expires: 0');
			header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
			header('Content-Type: application/force-download');
			header('Content-Type: application/octet-stream');
			header('Content-Type: application/download');
			header('Content-Disposition: attachment; filename=' . $sFileName);
			header('Content-Transfer-Encoding: binary');
			echo $sData;
			exit;
		}

		private function buildSheet()
		{
			$sData = pack('ssssss', 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);

			if(is_array($this->aData) && sizeof($this->aData))
			{
				$sData .= $this->buildData($this->aData);
			}

			$sData .= pack('ss', 0x0A, 0x00);

			return $sData;
		}

		private function buildData($aData)
		{
			$sData = '';
			$this->iRowNumber = 0;

			foreach($aData as $aCells)
			{
				$iColumnNumber = 0;

				foreach($aCells as $mCell)
				{
					if(is_array($mCell))
					{
						$sData .= $this->textFormat($this->iRowNumber, $iColumnNumber, json_encode($mCell));
					}
					elseif(is_numeric($mCell))
					{
						$sData .= $this->numberFormat($this->iRowNumber, $iColumnNumber, $mCell);
					}
					else
					{
						$sData .= $this->textFormat($this->iRowNumber, $iColumnNumber, $mCell);
					}

					$iColumnNumber++;
				}

				$this->iRowNumber++;
			}

			return $sData;
		}

		private function textFormat($row, $col, $data)
		{
			$data = utf8_decode($data);
			$length = strlen($data);
			$cell = pack('ssssss', 0x204, 8 + $length, $row, $col, 0x0, $length);
			$cell .= $data;

			return $cell;
		}

		private function numberFormat($row, $col, $data)
		{
			$cell = pack('sssss', 0x203, 14, $row, $col, 0x0);
			$cell .= pack('d', $data);

			return $cell;
		}
	}

	function arrayToExcel($aData, $sFileName)
	{
		$oExcel = new Excelsheet();

		foreach($aData as $aLine)
		{
			$oExcel->addRow($aLine);
		}

		if((strpos($sFileName, '/') === false) && (strpos($sFileName, '\\') === false))
		{
			$oExcel->sendFile($sFileName);
		}
		else
		{
			return $oExcel->saveFile($sFileName);
		}
	}

?>

Reacties

0
Nog geen reacties.