ADDED : Documents Properties Writing in Excel5Writer

git-svn-id: https://phpexcel.svn.codeplex.com/svn/trunk@85916 2327b42d-5241-43d6-9e2a-de5ac946f064
This commit is contained in:
Progi1984 2012-01-31 23:53:21 +00:00
parent 42fa6f10a1
commit 44b3fa2c94
3 changed files with 515 additions and 6 deletions

View File

@ -1311,6 +1311,11 @@ class PHPExcel_Reader_Excel5 implements PHPExcel_Reader_IReader
$value = self::_GetInt4d($this->_documentSummaryInformation, $secOffset + 4 + $offset);
break;
case 0x0B: // Boolean
$value = self::_GetInt2d($this->_documentSummaryInformation, $secOffset + 4 + $offset);
$value = ($value == 0 ? false : true);
break;
case 0x13: // 4 byte unsigned integer
// not needed yet, fix later if necessary
break;

View File

@ -91,6 +91,9 @@ class PHPExcel_Writer_Excel5 implements PHPExcel_Writer_IWriter
*/
private $_IDCLs;
private $_summaryInformation;
private $_documentSummaryInformation;
/**
* Create a new PHPExcel_Writer_Excel5
@ -173,7 +176,32 @@ class PHPExcel_Writer_Excel5 implements PHPExcel_Writer_IWriter
$OLE->append($this->_writerWorksheets[$i]->getData());
}
$root = new PHPExcel_Shared_OLE_PPS_Root(time(), time(), array($OLE));
$this->_documentSummaryInformation = $this->_writeDocumentSummaryInformation();
// initialize OLE Document Summary Information
if(isset($this->_documentSummaryInformation) && !empty($this->_documentSummaryInformation)){
$OLE_DocumentSummaryInformation = new PHPExcel_Shared_OLE_PPS_File(PHPExcel_Shared_OLE::Asc2Ucs(chr(5) . 'DocumentSummaryInformation'));
$OLE_DocumentSummaryInformation->append($this->_documentSummaryInformation);
}
$this->_summaryInformation = $this->_writeSummaryInformation();
// initialize OLE Summary Information
if(isset($this->_summaryInformation) && !empty($this->_summaryInformation)){
$OLE_SummaryInformation = new PHPExcel_Shared_OLE_PPS_File(PHPExcel_Shared_OLE::Asc2Ucs(chr(5) . 'SummaryInformation'));
$OLE_SummaryInformation->append($this->_summaryInformation);
}
// define OLE Parts
$arrRootData = array($OLE);
// initialize OLE Properties file
if(isset($OLE_SummaryInformation)){
$arrRootData[] = $OLE_SummaryInformation;
}
// initialize OLE Extended Properties file
if(isset($OLE_DocumentSummaryInformation)){
$arrRootData[] = $OLE_DocumentSummaryInformation;
}
$root = new PHPExcel_Shared_OLE_PPS_Root(time(), time(), $arrRootData);
// save the OLE file
$res = $root->save($pFilename);
@ -459,4 +487,381 @@ class PHPExcel_Writer_Excel5 implements PHPExcel_Writer_IWriter
$this->_writerWorkbook->setEscher($escher);
}
/**
* Build the OLE Part for DocumentSummary Information
* @return string
*/
private function _writeDocumentSummaryInformation(){
// offset: 0; size: 2; must be 0xFE 0xFF (UTF-16 LE byte order mark)
$data = pack('v', 0xFFFE);
// offset: 2; size: 2;
$data .= pack('v', 0x0000);
// offset: 4; size: 2; OS version
$data .= pack('v', 0x0106);
// offset: 6; size: 2; OS indicator
$data .= pack('v', 0x0002);
// offset: 8; size: 16
$data .= pack('VVVV', 0x00, 0x00, 0x00, 0x00);
// offset: 24; size: 4; section count
$data .= pack('V', 0x0001);
// offset: 28; size: 16; first section's class id: 02 d5 cd d5 9c 2e 1b 10 93 97 08 00 2b 2c f9 ae
$data .= pack('vvvvvvvv', 0xD502, 0xD5CD, 0x2E9C, 0x101B, 0x9793, 0x0008, 0x2C2B, 0xAEF9);
// offset: 44; size: 4; offset of the start
$data .= pack('V', 0x30);
// SECTION
$dataSection = array();
$dataSection_NumProps = 0;
$dataSection_Summary = '';
$dataSection_Content = '';
// GKPIDDSI_CODEPAGE: CodePage
$dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x01),
'offset' => array('pack' => 'V'),
'type' => array('pack' => 'V', 'data' => 0x02), // 2 byte signed integer
'data' => array('data' => 1252));
$dataSection_NumProps++;
// GKPIDDSI_CATEGORY : Category
if($this->_phpExcel->getProperties()->getCategory()){
$dataProp = $this->_phpExcel->getProperties()->getCategory();
$dataProp = 'Test result file';
$dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x02),
'offset' => array('pack' => 'V'),
'type' => array('pack' => 'V', 'data' => 0x1E),
'data' => array('data' => $dataProp, 'length' => strlen($dataProp)));
$dataSection_NumProps++;
}
// GKPIDDSI_VERSION :Version of the application that wrote the property storage
$dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x17),
'offset' => array('pack' => 'V'),
'type' => array('pack' => 'V', 'data' => 0x03),
'data' => array('pack' => 'V', 'data' => 0x000C0000));
$dataSection_NumProps++;
// GKPIDDSI_SCALE : FALSE
$dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x0B),
'offset' => array('pack' => 'V'),
'type' => array('pack' => 'V', 'data' => 0x0B),
'data' => array('data' => false));
$dataSection_NumProps++;
// GKPIDDSI_LINKSDIRTY : True if any of the values for the linked properties have changed outside of the application
$dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x10),
'offset' => array('pack' => 'V'),
'type' => array('pack' => 'V', 'data' => 0x0B),
'data' => array('data' => false));
$dataSection_NumProps++;
// GKPIDDSI_SHAREDOC : FALSE
$dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x13),
'offset' => array('pack' => 'V'),
'type' => array('pack' => 'V', 'data' => 0x0B),
'data' => array('data' => false));
$dataSection_NumProps++;
// GKPIDDSI_HYPERLINKSCHANGED : True if any of the values for the _PID_LINKS (hyperlink text) have changed outside of the application
$dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x16),
'offset' => array('pack' => 'V'),
'type' => array('pack' => 'V', 'data' => 0x0B),
'data' => array('data' => false));
$dataSection_NumProps++;
// GKPIDDSI_DOCSPARTS
// MS-OSHARED p75 (2.3.3.2.2.1)
// Structure is VtVecUnalignedLpstrValue (2.3.3.1.9)
// cElements
$dataProp = pack('v', 0x0001);
$dataProp .= pack('v', 0x0000);
// array of UnalignedLpstr
// cch
$dataProp .= pack('v', 0x000A);
$dataProp .= pack('v', 0x0000);
// value
$dataProp .= 'Worksheet'.chr(0);
$dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x0D),
'offset' => array('pack' => 'V'),
'type' => array('pack' => 'V', 'data' => 0x101E),
'data' => array('data' => $dataProp, 'length' => strlen($dataProp)));
$dataSection_NumProps++;
// GKPIDDSI_HEADINGPAIR
// VtVecHeadingPairValue
// cElements
$dataProp = pack('v', 0x0002);
$dataProp .= pack('v', 0x0000);
// Array of vtHeadingPair
// vtUnalignedString - headingString
// stringType
$dataProp .= pack('v', 0x001E);
// padding
$dataProp .= pack('v', 0x0000);
// UnalignedLpstr
// cch
$dataProp .= pack('v', 0x0013);
$dataProp .= pack('v', 0x0000);
// value
$dataProp .= 'Feuilles de calcul';
// vtUnalignedString - headingParts
// wType : 0x0003 = 32 bit signed integer
$dataProp .= pack('v', 0x0300);
// padding
$dataProp .= pack('v', 0x0000);
// value
$dataProp .= pack('v', 0x0100);
$dataProp .= pack('v', 0x0000);
$dataProp .= pack('v', 0x0000);
$dataProp .= pack('v', 0x0000);
$dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x0C),
'offset' => array('pack' => 'V'),
'type' => array('pack' => 'V', 'data' => 0x100C),
'data' => array('data' => $dataProp, 'length' => strlen($dataProp)));
$dataSection_NumProps++;
// 4 Section Length
// 4 Property count
// 8 * $dataSection_NumProps (8 = ID (4) + OffSet(4))
$dataSection_Content_Offset = 8 + $dataSection_NumProps * 8;
foreach ($dataSection as $dataProp){
// Summary
$dataSection_Summary .= pack($dataProp['summary']['pack'], $dataProp['summary']['data']);
// Offset
$dataSection_Summary .= pack($dataProp['offset']['pack'], $dataSection_Content_Offset);
// DataType
$dataSection_Content .= pack($dataProp['type']['pack'], $dataProp['type']['data']);
// Data
if($dataProp['type']['data'] == 0x02){ // 2 byte signed integer
$dataSection_Content .= pack('V', $dataProp['data']['data']);
$dataSection_Content_Offset += 4 + 4;
}
elseif($dataProp['type']['data'] == 0x03){ // 4 byte signed integer
$dataSection_Content .= pack('V', $dataProp['data']['data']);
$dataSection_Content_Offset += 4 + 4;
}
elseif($dataProp['type']['data'] == 0x0B){ // Boolean
if($dataProp['data']['data'] == false){
$dataSection_Content .= pack('V', 0x0000);
} else {
$dataSection_Content .= pack('V', 0x0001);
}
$dataSection_Content_Offset += 4 + 4;
}
elseif($dataProp['type']['data'] == 0x1E){ // null-terminated string prepended by dword string length
// Null-terminated string
$dataProp['data']['data'] .= chr(0);
$dataProp['data']['length'] += 1;
// Complete the string with null string for being a %4
$dataProp['data']['length'] = $dataProp['data']['length'] + ((4 - $dataProp['data']['length'] % 4)==4 ? 0 : (4 - $dataProp['data']['length'] % 4));
$dataProp['data']['data'] = str_pad($dataProp['data']['data'], $dataProp['data']['length'], chr(0), STR_PAD_RIGHT);
$dataSection_Content .= pack('V', $dataProp['data']['length']);
$dataSection_Content .= $dataProp['data']['data'];
$dataSection_Content_Offset += 4 + 4 + strlen($dataProp['data']['data']);
}
elseif($dataProp['type']['data'] == 0x40){ // Filetime (64-bit value representing the number of 100-nanosecond intervals since January 1, 1601)
$dataSection_Content .= $dataProp['data']['data'];
$dataSection_Content_Offset += 4 + 8;
}
else {
// Data Type Not Used at the moment
$dataSection_Content .= $dataProp['data']['data'];
$dataSection_Content_Offset += 4 + $dataProp['data']['length'];
}
}
// Now $dataSection_Content_Offset contains the size of the content
// section header
// offset: $secOffset; size: 4; section length
// + x Size of the content (summary + content)
$data .= pack('V', $dataSection_Content_Offset);
// offset: $secOffset+4; size: 4; property count
$data .= pack('V', $dataSection_NumProps);
// Section Summary
$data .= $dataSection_Summary;
// Section Content
$data .= $dataSection_Content;
return $data;
}
/**
* Build the OLE Part for Summary Information
* @return string
*/
private function _writeSummaryInformation(){
// offset: 0; size: 2; must be 0xFE 0xFF (UTF-16 LE byte order mark)
$data = pack('v', 0xFFFE);
// offset: 2; size: 2;
$data .= pack('v', 0x0000);
// offset: 4; size: 2; OS version
$data .= pack('v', 0x0106);
// offset: 6; size: 2; OS indicator
$data .= pack('v', 0x0002);
// offset: 8; size: 16
$data .= pack('VVVV', 0x00, 0x00, 0x00, 0x00);
// offset: 24; size: 4; section count
$data .= pack('V', 0x0001);
// offset: 28; size: 16; first section's class id: e0 85 9f f2 f9 4f 68 10 ab 91 08 00 2b 27 b3 d9
$data .= pack('vvvvvvvv', 0x85E0, 0xF29F, 0x4FF9, 0x1068, 0x91AB, 0x0008, 0x272B, 0xD9B3);
// offset: 44; size: 4; offset of the start
$data .= pack('V', 0x30);
// SECTION
$dataSection = array();
$dataSection_NumProps = 0;
$dataSection_Summary = '';
$dataSection_Content = '';
// CodePage : CP-1252
$dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x01),
'offset' => array('pack' => 'V'),
'type' => array('pack' => 'V', 'data' => 0x02), // 2 byte signed integer
'data' => array('data' => 1252));
$dataSection_NumProps++;
// Title
if($this->_phpExcel->getProperties()->getTitle()){
$dataProp = $this->_phpExcel->getProperties()->getTitle();
$dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x02),
'offset' => array('pack' => 'V'),
'type' => array('pack' => 'V', 'data' => 0x1E), // null-terminated string prepended by dword string length
'data' => array('data' => $dataProp, 'length' => strlen($dataProp)));
$dataSection_NumProps++;
}
// Subject
if($this->_phpExcel->getProperties()->getSubject()){
$dataProp = $this->_phpExcel->getProperties()->getSubject();
$dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x03),
'offset' => array('pack' => 'V'),
'type' => array('pack' => 'V', 'data' => 0x1E), // null-terminated string prepended by dword string length
'data' => array('data' => $dataProp, 'length' => strlen($dataProp)));
$dataSection_NumProps++;
}
// Author (Creator)
if($this->_phpExcel->getProperties()->getCreator()){
$dataProp = $this->_phpExcel->getProperties()->getCreator();
$dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x04),
'offset' => array('pack' => 'V'),
'type' => array('pack' => 'V', 'data' => 0x1E), // null-terminated string prepended by dword string length
'data' => array('data' => $dataProp, 'length' => strlen($dataProp)));
$dataSection_NumProps++;
}
// Keywords
if($this->_phpExcel->getProperties()->getKeywords()){
$dataProp = $this->_phpExcel->getProperties()->getKeywords();
$dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x05),
'offset' => array('pack' => 'V'),
'type' => array('pack' => 'V', 'data' => 0x1E), // null-terminated string prepended by dword string length
'data' => array('data' => $dataProp, 'length' => strlen($dataProp)));
$dataSection_NumProps++;
}
// Comments (Description)
if($this->_phpExcel->getProperties()->getDescription()){
$dataProp = $this->_phpExcel->getProperties()->getDescription();
$dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x06),
'offset' => array('pack' => 'V'),
'type' => array('pack' => 'V', 'data' => 0x1E), // null-terminated string prepended by dword string length
'data' => array('data' => $dataProp, 'length' => strlen($dataProp)));
$dataSection_NumProps++;
}
// Last Saved By (LastModifiedBy)
if($this->_phpExcel->getProperties()->getLastModifiedBy()){
$dataProp = $this->_phpExcel->getProperties()->getLastModifiedBy();
$dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x08),
'offset' => array('pack' => 'V'),
'type' => array('pack' => 'V', 'data' => 0x1E), // null-terminated string prepended by dword string length
'data' => array('data' => $dataProp, 'length' => strlen($dataProp)));
$dataSection_NumProps++;
}
// Created Date/Time
if($this->_phpExcel->getProperties()->getCreated()){
$dataProp = $this->_phpExcel->getProperties()->getCreated();
$dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x0C),
'offset' => array('pack' => 'V'),
'type' => array('pack' => 'V', 'data' => 0x40), // Filetime (64-bit value representing the number of 100-nanosecond intervals since January 1, 1601)
'data' => array('data' => PHPExcel_Shared_OLE::LocalDate2OLE($dataProp)));
$dataSection_NumProps++;
}
// Modified Date/Time
if($this->_phpExcel->getProperties()->getModified()){
$dataProp = $this->_phpExcel->getProperties()->getModified();
$dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x0D),
'offset' => array('pack' => 'V'),
'type' => array('pack' => 'V', 'data' => 0x40), // Filetime (64-bit value representing the number of 100-nanosecond intervals since January 1, 1601)
'data' => array('data' => PHPExcel_Shared_OLE::LocalDate2OLE($dataProp)));
$dataSection_NumProps++;
}
// Security
$dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x13),
'offset' => array('pack' => 'V'),
'type' => array('pack' => 'V', 'data' => 0x03), // 4 byte signed integer
'data' => array('data' => 0x00));
$dataSection_NumProps++;
// 4 Section Length
// 4 Property count
// 8 * $dataSection_NumProps (8 = ID (4) + OffSet(4))
$dataSection_Content_Offset = 8 + $dataSection_NumProps * 8;
foreach ($dataSection as $dataProp){
// Summary
$dataSection_Summary .= pack($dataProp['summary']['pack'], $dataProp['summary']['data']);
// Offset
$dataSection_Summary .= pack($dataProp['offset']['pack'], $dataSection_Content_Offset);
// DataType
$dataSection_Content .= pack($dataProp['type']['pack'], $dataProp['type']['data']);
// Data
if($dataProp['type']['data'] == 0x02){ // 2 byte signed integer
$dataSection_Content .= pack('V', $dataProp['data']['data']);
$dataSection_Content_Offset += 4 + 4;
}
elseif($dataProp['type']['data'] == 0x03){ // 4 byte signed integer
$dataSection_Content .= pack('V', $dataProp['data']['data']);
$dataSection_Content_Offset += 4 + 4;
}
elseif($dataProp['type']['data'] == 0x1E){ // null-terminated string prepended by dword string length
// Null-terminated string
$dataProp['data']['data'] .= chr(0);
$dataProp['data']['length'] += 1;
// Complete the string with null string for being a %4
$dataProp['data']['length'] = $dataProp['data']['length'] + ((4 - $dataProp['data']['length'] % 4)==4 ? 0 : (4 - $dataProp['data']['length'] % 4));
$dataProp['data']['data'] = str_pad($dataProp['data']['data'], $dataProp['data']['length'], chr(0), STR_PAD_RIGHT);
$dataSection_Content .= pack('V', $dataProp['data']['length']);
$dataSection_Content .= $dataProp['data']['data'];
$dataSection_Content_Offset += 4 + 4 + strlen($dataProp['data']['data']);
}
elseif($dataProp['type']['data'] == 0x40){ // Filetime (64-bit value representing the number of 100-nanosecond intervals since January 1, 1601)
$dataSection_Content .= $dataProp['data']['data'];
$dataSection_Content_Offset += 4 + 8;
}
else {
// Data Type Not Used at the moment
}
}
// Now $dataSection_Content_Offset contains the size of the content
// section header
// offset: $secOffset; size: 4; section length
// + x Size of the content (summary + content)
$data .= pack('V', $dataSection_Content_Offset);
// offset: $secOffset+4; size: 4; property count
$data .= pack('V', $dataSection_NumProps);
// Section Summary
$data .= $dataSection_Summary;
// Section Content
$data .= $dataSection_Content;
return $data;
}
}

View File

@ -0,0 +1,99 @@
<?php
/**
* PHPExcel
*
* Copyright (C) 2006 - 2011 PHPExcel
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category PHPExcel
* @package PHPExcel
* @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
* @version ##VERSION##, ##DATE##
*/
/** Error reporting */
error_reporting(E_ALL);
date_default_timezone_set('Europe/London');
/** PHPExcel */
require_once '../Classes/PHPExcel.php';
// Create new PHPExcel object
echo date('H:i:s') . " Create new PHPExcel object<br />";
$objPHPExcel = new PHPExcel();
// Set properties
echo date('H:i:s') . " Set properties<br />";
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
->setLastModifiedBy("Franklin")
->setTitle("Office 2007 XLSX Test Document")
->setSubject("Office 2007 XLSX Test Document")
->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
->setKeywords("office 2007 openxml php")
->setCategory("Test result file");
// Add some data
echo date('H:i:s') . " Add some data<br />";
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'Hello')
->setCellValue('B2', 'world!');
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
// Save Excel 2007 file
echo date('H:i:s') . " Write to Excel2007 format<br />";
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save(str_replace('.php', '.xlsx', __FILE__));
// Save Excel5 file
echo date('H:i:s') . " Write to Excel5 format<br />";
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save(str_replace('.php', '.xls', __FILE__));
// Echo memory peak usage
echo date('H:i:s') . " Peak memory usage: " . (memory_get_peak_usage(true) / 1024 / 1024) . " MB\r<br />";
// Echo done
echo date('H:i:s') . " Done writing file.\r<br />";
unset($objWriter);
echo '<br />';
// Create new PHPExcel object
echo date('H:i:s') . " Create new PHPExcel object<br />";
$objPHPExcelRead = PHPExcel_IOFactory::load('31docproperties_write.xls');
// Set properties
echo date('H:i:s') . " Get properties<br />";
echo 'Creator : '.$objPHPExcelRead->getProperties()->getCreator().'<br />';
echo 'LastModifiedBy : '.$objPHPExcelRead->getProperties()->getLastModifiedBy().'<br />';
echo 'Title : '.$objPHPExcelRead->getProperties()->getTitle().'<br />';
echo 'Subject : '.$objPHPExcelRead->getProperties()->getSubject().'<br />';
echo 'Description : '.$objPHPExcelRead->getProperties()->getDescription().'<br />';
echo 'Keywords : '.$objPHPExcelRead->getProperties()->getKeywords().'<br />';
echo 'Category : '.$objPHPExcelRead->getProperties()->getCategory().'<br />';
// Echo memory peak usage
echo date('H:i:s') . " Peak memory usage: " . (memory_get_peak_usage(true) / 1024 / 1024) . " MB\r<br />";
?>