PHP 操作ms word

Useful PHP MSWord class I created to do some simple file conversions.  This class could have a lot more to it but I am not familiar with all the COM MS Word function calls.
<?php
   
// msword.inc.php
   
   // NOTE: Using COM with windows NT/2000/XP with apache as a service
   // – Run dcomcnfg.exe
   // – Find word application and click properties
   // – Click the Security tab
   // – Use Custom Access Permissions
   //      – Add the user who runs the web server service
   // – Use Custom Launch permissions
   //      – Add the user who runs the web server service
   
   
$wdFormatDocument = 0
;
   
$wdFormatTemplate = 1
;
   
$wdFormatText = 2
;
   
$wdFormatTextLineBreaks = 3
;
   
$wdFormatDOSText = 4
;
   
$wdFormatDOSTextLineBreaks = 5
;
   
$wdFormatRTF = 6
;
   
$wdFormatUnicodeText = 7
;
   
$wdFormatHTML=8
;

   class MSWord
   
{
       
// Vars:
       
var $handle
;
       
       
// Create COM instance to word
       
function MSWord($Visible = false
)
       {
           
$this->handle = new COM("word.application") or die("Unable to instanciate Word"
);
           
$this->handle->Visible = $Visible
;
       }
       
       
// Open existing document
       
function Open($File
)
       {
           
$this->handle->Documents->Open($File
);
       }
       
       
// Create new document
       
function NewDocument
()
       {
           
$this->handle->Documents->Add
();
       }
       
       
// Write text to active document
       
function WriteText( $Text 
)
       {
           
$this->handle->Selection->Typetext( $Text 
);
       }
       
       
// Number of documents open
       
function DocumentCount
()
       {
           return 
$this->handle->Documents->Count
;
       }
       
       
// Save document as another file and/or format
       
function SaveAs($File, $Format = 0 
)
       {
           
$this->handle->ActiveDocument->SaveAs($File, $Format
);
       }
       
       
// Save active document
       
function Save
()
       {
           
$this->handle->ActiveDocument->Save
();
       }
       
       
// close active document.
       
function Close
()
       {
           
$this->handle->ActiveDocument->Close
();
       }
       
       
// Get word version
       
function GetVersion
()
       {
           return 
$this->handle->Version
;
       }
       
       
// get handle to word
       
function GetHandle
()
       {
           return 
$this->handle
;
       }
   
       
// Clean up instance with word
       
function Quit
()
       {
           if( 
$this->handle 
)
           {
               
// close word
               
$this->handle->Quit
();
   
               
// free the object
               
$this->handle->Release
();
               
$this->handle = null
;
           }
       }
   };

?>

Example 1, opens an html file, writes text to it, then saves it as a document:
<?php
   $input 
= "C:\test.htm"
;
   
$output = "C:\test.doc"
;

   $Word = new MSWord;
   
$Word->Open($input
);
   
$Word->WriteText("This is a test "
);
   
$Word->SaveAs($output
);
   
$Word->Quit
();
?>

Example 2, opens an html file, then saves it as a rtf file:
<?php
   $input 
= "C:\test.htm"
;
   
$output = "C:\test.rtf"
;

   $Word = new MSWord;
   
$Word->Open($input
);
   
$Word->SaveAs($output, $wdFormatRTF
);
   
$Word->Quit
();
?> 

留下评论

邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据。