Source for file ExcelWriterXML.php
Documentation is available at ExcelWriterXML.php
* Used the schema documentation from Microsoft
* @link http://msdn.microsoft.com/en-us/library/aa140066(office.10).aspx
* @package ExcelWriterXML
* Includes the other class file to create Sheets
include('ExcelWriterXML_Sheet.php');
* Includes the other class file to create Styles
include('ExcelWriterXML_Style.php');
* Class for generating the initial Excel XML document
* $xml = new ExcelWriterXML;
* $format = $xml->addStyle('StyleHeader');
* $sheet = $xml->addSheet('Test Sheet');
* $sheet->writeString (1,1,'Header1','StyleHeader');
* $sheet->writeString(2,1,'My String');
* @link http://msdn.microsoft.com/en-us/library/aa140066(office.10).aspx
* @package ExcelWriterXML
private $styles = array();
private $sheets = array();
private $showErrorSheet = false;
private $overwriteFile = false;
private $docVersion = 11.9999;
* Constructor for the ExcelWriterXML class.
* A default style is created, a filename is generated (if not supplied) and
* the create time of the document is stored.
* @param string $fileName This is the filename that will be passed to the
* browser. If not present it will default to "file.xml"
* @return ExcelWriterXML Instance of the class
$style->alignVertical('Bottom');
$this->addError(__FUNCTION__ ,'File name was blank, default to "file.xml"');
$this->docFileName = $fileName;
$this->docCreated = date('Y-m-d'). 'T'. date('H:i:s'). 'Z';
* Whether or not to overwrite a file (when writing to disk)
* @param boolean $overwrite True or False
$this->overwriteFile = false;
$this->overwriteFile = $overwrite;
* Whether or not to show the sheet containing the Formatting Errors
$this->showErrorSheet = true;
$this->showErrorSheet = $show;
* Adds a format error. When the document is generated if there are any
* errors they will be listed on a seperate sheet.
@param string $function The name of the function that was called
* @param string $message Details of the error
public function addError($function, $message){
* Sends the HTML headers to the client.
* This is only necessary if the XML doc is to be delivered from the server
header('content-type: text/xml');
header('Content-Disposition: attachment; filename="'. $this->docFileName. '"');
header('Cache-Control: must-revalidate, post-check=0,pre-check=0');
* Gets the default style that was created by the contructor.
* This is used when modifications to the default style are required.
* @return ExcelWriterXML_Style Reference to a style class
return($this->styles[0]);
* Creates a new style within the spreadsheet.
* Styles cannot have the same name as any other style. If a style has the
* same name as another style then it will follow the default naming
* convention as if $id was null
* @param string $id The name of the style. If left blank then the style
* will default to "CustomStyle" + n (e.g. "CustomStyle1")
* @return ExcelWriterXML_Style Reference to a new style class
if (trim($id) == '') $id = null;
$id = 'CustomStyle'. $styleNum;
//$this->addError(__FUNCTION__,'Style name was blank, renamed to "'.$id.'"');
while (!$this->checkStyleID($id)){
$id = 'CustomStyle'. $styleNum;
$this->addError(__FUNCTION__ ,'Style name was duplicate ("'. $old_id. '"), renamed to "'. $id. '"');
$this->styles[] = $style;
* Creates a new sheet within the spreadsheet
* At least one sheet is required.
* Additional sheets cannot have the same name as any other sheet.
* If a sheet has the same name as another sheet then it will follow the
* default naming convention as if $id was null
* @param string $id The name of the sheet. If left blank then the sheet
* will default to "Sheet" + n (e.g. "Sheet1")
* @return ExcelWriterXML_Sheet Reference to a new sheet class
if (trim($id) == '') $id = null;
$this->addError(__FUNCTION__ ,'Sheet name was blank, renamed to "'. $id. '"');
while (!$this->checkSheetID($id)){
$this->addError(__FUNCTION__ ,'Sheet name was duplicate ("'. $old_id. '"), renamed to "'. $id. '"');
$this->sheets[] = $sheet;
* Checks whether a proposed Sheet ID has already been used
* @param string $id The sheet id to be checked
* @return boolean True if the id is unique, false otherwise
private function checkSheetID($id){
foreach($this->sheets as $sheet){
$sheetID = $sheet->getID();
* Checks whether a proposed Style ID has already been used
* @param string $id The style id to be checked
* @return boolean True if the id is unique, false otherwise
foreach($this->styles as $style){
$styleID = $style->getID();
* @param string $target If left null the function will output to STD OUT
* (e. g. browser or console)
$format = $this->addStyle('formatErrorsHeader');
if (!empty($this->docCreated)) $docCreated = '<Created>'. htmlspecialchars($this->docCreated). '</Created>'. "\r";
$xml = '<?xml version="1.0"?>'. "\r";
$xml .= '<?mso-application progid="Excel.Sheet"?>'. "\r";
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">'. "\r";
$xml .= '<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">'. "\r";
if (!empty($this->docTitle)) $xml .= ' '. $docTitle;
if (!empty($this->docSubject)) $xml .= ' '. $docSubject;
if (!empty($this->docAuthor)) $xml .= ' '. $docAuthor;
if (!empty($this->docCreated)) $xml .= ' '. $docCreated;
if (!empty($this->docManager)) $xml .= ' '. $docManager;
if (!empty($this->docCompany)) $xml .= ' '. $docCompany;
$xml .= ' <Version>'. $this->docVersion. '</Version>'. "\r";
$xml .= '</DocumentProperties>'. "\r";
$xml .= '<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel" />'. "\r";
foreach($this->styles as $style){
$xml .= $style->getStyleXML();
if (count($style->getErrors()) > 0){
$xml .= '</Styles>'. "\r";
if (count($this->sheets) == 0){
foreach($this->sheets as $sheet){
$xml .= $sheet->getSheetXML();
if (count($sheet->getErrors()) > 0){
$sheet = $this->addSheet('formatErrors');
$sheet->cellMerge(1,1,3,0); // Merge the first three cells across in row 1
$sheet->writeString(1,1,'Formatting Errors');
$sheet->writeString(2,1,'Type','formatErrorsHeader');
$sheet->writeString(2,2,'Function','formatErrorsHeader');
$sheet->cellWidth(2,1,200);
$sheet->cellWidth(2,2,200);
$sheet->cellWidth(2,3,400);
$sheet->writeString(2,3,'Error Message','formatErrorsHeader');
$function = $error['function'];
$message = $error['message'];
$sheet->writeString($row,1,'Document');
$sheet->writeString($row,2,$function);
$sheet->writeString($row,3,$message);
foreach($this->styles as $styleObject){
$formatErrors = $styleObject->getErrors();
$styleID = 'Style='. $styleObject->getID();
foreach($formatErrors as $error){
$function = $error['function'];
$message = $error['message'];
$sheet->writeString($row,1,$styleID);
$sheet->writeString($row,2,$function);
$sheet->writeString($row,3,$message);
foreach($this->sheets as $sheetObject){
$formatErrors = $sheetObject->getErrors();
$sheetID = 'Sheet='. $sheetObject->getID();
foreach($formatErrors as $error){
$function = $error['function'];
$message = $error['message'];
$sheet->writeString($row,1,$sheetID);
$sheet->writeString($row,2,$function);
$sheet->writeString($row,3,$message);
$xml .= $sheet->getSheetXML();
// We aren't writing this file to disk, so echo back to the client.
die('"'. $target. '" exists and "overwriteFile" is set to "false"');
$handle = fopen($target, 'w');
echo ('<br/>Not able to open "'. $target. '" for writing');
* Sets the Title of the document
* @param string $title Part of the properties of the document.
* Sets the Subject of the document
* @param string $subject Part of the properties of the document.
* Sets the Author of the document
* @param string $author Part of the properties of the document.
* Sets the Manager of the document
* @param string $manager Part of the properties of the document.
* Sets the Company of the document
* @param string $company Part of the properties of the document.
* Outputs a MYSQL table or list of tables to an Excel doc
* @param string $host MySQL host to connect to
* @param string $username Username to connect with
* @param string $password Password to connect with
* @param string $db Database to use
* @param mixed $table If string, out specific table. If array, each table will have it's own sheet
* @param mixed $alternateName For multiple tables this does nothing. For table, overrides auto naming of the sheet (table name)
public function mysqlTableDump($host,$username,$password,$db,$table,$alternateName = null){
if (empty($host)) $this->addError('Database','HOSTNAME is empty');
if (empty($username)) $this->addError('Database','USERNAME is empty');
if (empty($db)) $this->addError('Database','DB is empty');
if (empty($table)) $this->addError('Database','TABLE(S) is empty');
if (!$db_selected) $this->addError('Database','DB "'. $db. '" does not exist');
foreach($table as $table2){
$query = 'SELECT * FROM `'. $db. '`.`'. $table2. '` ';
if ($alternateName == null || empty($alternateName)) $sheet = $this->addSheet($table);
else $sheet = $this->addSheet($alternateName);
$query = 'SELECT * FROM `'. $db. '`.`'. $table. '` ';
$sheet->writeString(1,1,'No data');
if ($headersWritten == false){
for($x= 0; $x< $numFields;$x++ ){
$sheet->writeString($row,$col++ ,$name,'db_header');
foreach($data as $offset => $value){
$sheet->writeNumber($row,$col++ ,$value);
else if ($type == 'datetime'){
$value = $sheet->convertMysqlDateTime($value);
$sheet->writeDateTime($row,$col++ ,$value,'db_datetime');
else if ($type == 'date'){
$value = $sheet->convertMysqlDate($value);
$sheet->writeDateTime($row,$col++ ,$value,'db_date');
else if ($type == 'time'){
$value = $sheet->convertMysqlTime($value);
$sheet->writeDateTime($row,$col++ ,$value,'db_time');
$sheet->writeNumber($row,$col++ ,$value);
$sheet->writeString($row,$col++ ,$value);
$this->addError('Database','DB driver "'. $driver. '" could not be loaded');
$sHeader->fontFamily('Swiss');
$sHeader->fontColor('0000FF');
$hDateTime = $xml->addStyle('db_datetime');
$hDateTime->numberFormatDateTime();
$hDate->numberFormatDate();
$hTime->numberFormatTime();
|