轉(zhuǎn)帖:PHP4(windows版本)中的COM函數(shù)
Jun 21, 2016 am 09:14 AMwindow|函數(shù)
這幾天一直在寫excel轉(zhuǎn)化成mysql,發(fā)現(xiàn)一篇文章,搜一下phpx論壇,沒有這個帖子,把它轉(zhuǎn)帖如下:
PHP4(windows版本)中的COM函數(shù)
介紹
內(nèi)置于PHP4里的COM函數(shù)對于我們在win32環(huán)境下開發(fā)程序是相當(dāng)有吸引力的,但是至今仍沒有多少相關(guān)的技術(shù)文檔。本文將以三個例子分
別處理 MS office 2000 Word 、 Excel 、 Adobe Distiller 來說明如何在PHP中使用COM函數(shù)。
COM技術(shù)是由Microsoft在幾年前提出并開發(fā)的,本文中提到的相關(guān)名詞有OLE, OLE Automation, ActiveX, COM ,這些詞的意思都基本一
樣,都表示用一段封裝的代碼(對象)來完成一個windows 應(yīng)用程序的一些功能。 PHP4 COM 函數(shù)可以連接一個對象實例,并使用它的方法與
屬性。
如果你想使用下面的例子源碼,請參考一下我的配置。
Windows 98 - MS Office 2000
Apache 1.3.9 Windows
PHP4.02 Dev (08-20-00) Running as CGI
PHP4中的COM標(biāo)記
現(xiàn)在讓我們開始吧,用PHP4的COM來實例化一個組件,需要 new 操作符和對象的 "OLE 程序標(biāo)識":
$instance = new COM("$identifier");
?>
因為COM是一個PHP4的保留字,它傳送這個對象的標(biāo)識給一個構(gòu)造函數(shù),現(xiàn)在得到了這個組件的一個實例,根據(jù)OOP類的性質(zhì),我們可以很容易
地訪問它的方法與屬性。
例如:
$instance->[Object]->[method1]->[method2]->..->[property];
?>
就是這么簡單!
OOP的結(jié)構(gòu)在PHP下不能工作,(由于PHP語法的問題,屬性的名字.值是非法字符,如點和圓括號等),所以PHP4提供了兩個相應(yīng)的函數(shù):
bool com_set(class com_object, string property name, string property_value);
mixed com_get(class com_object, string property_name);
?>
最后,PHP4也支持DCOM技術(shù),可以在遠(yuǎn)程計算機創(chuàng)建一個對象實例。
$Instance = new COM(string "Component name", string "remote_server_address");
?>
注意:這是用DCOM指令來設(shè)置PHP。在將來,PHP開發(fā)者提供Unix下對DCOM的支持。
標(biāo)識、方法和屬性
標(biāo)識是一個如下的字串:
MS Word: "Word.Application" or "Word.Application.9"
MS Excel: "Excel.Application" or "Excel.Sheet"
ADOBE Acrobat: "Exch.application" or "PdfDistiller.PdfDistiller"
對于最后一個標(biāo)識,我要指明的是,獲得正確的對象標(biāo)識名不是一件容易的事。如果你不能訪問VBA文檔,你可以查找一下windows的注冊
表,在 HKEY_CLASSES_ROOT 中尋找一下,你就可以得到一些應(yīng)用程序的名字。在你的機器上有效的對象標(biāo)識放在 CLSID 子文件夾下。
應(yīng)用程序一般會提供文檔說明它的COM方法和屬性。在office2000中,你可以運行程序,打開VBA編輯器 ,選擇對象編輯器。輸入應(yīng)用程序
庫中的一個方法名或?qū)傩悦?,然后,在下面的窗口中用鼠?biāo)右鍵選擇一個類或成員名稱,點幫助,你就會得到關(guān)于這個類或成員的描述。你也
可以參考 MSDN。一個 Excel 的例子如下: http://msdn.microsoft.com/library/officedev/off2000/xltocobjectmodelapplication.htm
用COM函數(shù)操作 MS Word
現(xiàn)在,我們開始第一個例子吧:
#*********************************************************
# 本例來自Zend站點,略有改動
# 打開一個word實例,并新建一個文檔Useless test.doc
# 輸入一行文字 "This is a test2..."
#*********************************************************
#實例化一個對象
$word = new COM("word.application") or die("Unable to instantiate Word");
#取得并顯示版本
print "Loaded Word, version {$word->Version}
";
#另一種方法去取得版本
$testversion = com_get($word->application,version);
print "Version using Com_get(): $testversion
";
#使其可見
$word->Visible = 1;
#創(chuàng)建新文件
$word->Documents->Add();
#寫字符
$word->Selection->TypeText("This is a test...");
#保存
$word->Documents[1]->SaveAs("Useless test.doc");
#關(guān)閉
$word->Quit();
?>
你只要花幾分鐘來讀這個程序,并參考Word的OLE 技術(shù)文檔,你將學(xué)到幾乎是你在自己程序中所需的全部的操作。
MS Excel在使用PHP的COM函數(shù)
如同上面的Word的例子一樣,你應(yīng)學(xué)習(xí)這個例子的同時參考Excel的Visual Basic 編輯器中的對象瀏覽器的幫助文檔。
#打開workbook和它的sheet,
#本例使用一個電子表格是Excel安裝時自帶的SOLVSAMP.XLS
$workbook = "C:Program FilesMicrosoft officeOfficeSamplesSOLVSAMP.XLS";
$sheet = "Quick Tour";
#實例化一個組件的對象
$ex = new COM("Excel.sheet") or Die ("Did not connect");
#取程序名稱和版本
print "Application name:{$ex->Application->value}
" ;
print "Loaded version: {$ex->Application->version}
";
#打開工作本使我們可使用它
$wkb = $ex->application->Workbooks->Open($workbook) or Die ("Did not open");
#預(yù)保存原來的工作本,創(chuàng)建一個工作本的復(fù)本
$ex->Application->ActiveWorkbook->SaveAs("Ourtest");
#$ex->Application->Visible = 1; #本句去注釋讓Excel可見
# 讀寫一個單元格在一個新的工作表中
# 我們可以讀到這個單元格 E11 (Advertising in the 4th. Quarter)
$sheets = $wkb->Worksheets($sheet); #Select the sheet
$sheets->activate; #Activate it
$cell = $sheets->Cells(11,5) ; #Select the cell (Row Column number)
$cell->activate; #Activate the cell
print "Old Value = {$cell->value}
"; #Print the value of the cell:10000
$cell->value = 15000; #Change it to 15000
print "New value = {$cell->value}
";#Print the new value=15000
#最后,用新值重新計算這個單元格
$sheets->Calculate;
#必須的如果要計算,手動則是可選的
#可看到效果總價值(E13單元格)
$cell = $sheets->Cells(13,5) ; #Select the cell (Row Column number)
$number = Number_format($cell->value);
print "New Total cost =$$number - was $47,732 before.
";
#根據(jù)計算公式,廣告影響了公司的開銷,這里將顯示 $57,809
#使用Excel內(nèi)建的函數(shù)
# PMT(percent/12 months,Number of payments,Loan amount)
$pay = $ex->application->pmt(0.08/12,10,10000);
$pay = sprintf("%.2f",$pay);
print "Monthly payment for $10,000 loan @8% interest /10 months: $ $pay
";
#Should print monthly payment = $ -1,037.03
#可選,保存
$ex->Application->ActiveWorkbook->SaveAs("Ourtest");
#關(guān)閉,不提問
$ex->application->ActiveWorkbook->Close("False");
unset ($ex);
?>
這個例子讓你的PHP與Excel一同工作了,當(dāng)然,也有更多的對象可以使用,訪問一個自已寫的OOP封裝類也與訪問excel一樣容易。
用PHP的COM訪問 Adobe Distiller
這最后一個例子不是MS程序了,如果你的程序有一個PostScript文件,你會對這個有興趣的,改寫(蒸餾)它成為一個PDF文檔. Adobe 有一
個程序叫 Distiller ,它可以生成一個實例。代碼如下:
$pdf = new COM("pdfdistiller.pdfdistiller.1");
?>
有一點要注意的,是在Distiller 的文檔中給出的這個OLE標(biāo)識名 "pdfdistiller" 是無效的。
蒸餾一個文件的最基本的方法是:
$pdf->FileToPdf ($psfile, strOutputPDF '', strJobOptions "");
?>
這 $psfile 是這個PostScript的文件名, strOutputPDF 是輸出文件PDF的文件名。 StrJobOptions 是Distiller的參數(shù)文件名,最后兩個參數(shù)
是可選的,默認(rèn)是同一名字。 這PS文件名與PDF文件名,使用這個默認(rèn)的Job options 文件。例如:
$pdf->FileToPdf ($psfile, "", "");
#這兒$psfile 可以是 Myfile.ps 將返回 Myfile.pdf 文件。
?>
在Distiller中有更多的方法和屬性能被用。如果你感興趣,請參考一下Adobe的技術(shù)文檔。
中止/可能的問題
如果你的代碼中發(fā)生了什么錯誤,你可能會創(chuàng)建了一個實例,但沒有正常地關(guān)閉它。最糟的是,這個應(yīng)用程序可能被這個實例所保持,結(jié)
果,在你的程序列表中就存在多份這個程序的副本,即使你更正了這個錯誤也會干擾你的結(jié)果。解決方法是:修正一個bug以來要及時清除它們
在你重新開始運行之前,用 并結(jié)束任務(wù)。同樣的原因,在你的代碼最后,也要及時關(guān)閉這個程序并刪除這個實例。
你有一些技巧在處理 com_get 和 com_set的異常時。例如:
$Version = Com_get($instance->Application,"Version");
將會在Word中工作但在Excel中會產(chǎn)生一個錯誤。
有一些對象在PHP4中是不能實例化的,這是因為這個程序要一個自定義的接口,但PHP4不支持。
為什么我們要用它?
我希望這三個例子可以給你一些思考的線索,PHP的COM允許你在PHP的腳本中訪問windows4的程序。這個代碼比ASP簡單并且能集成其它的
PHP對數(shù)據(jù)庫強大的支持功能。Microsoft 在各個方面都大力銷售這個COM 技術(shù),在不同的名稱和結(jié)構(gòu)下,如 COM+(Combine COM with
Microsoft Transaction Server MTS), ADO, OLE DB, OWC, Windows DNA, 等等。 PHP 和 Apache的結(jié)合,提供了一個開放源碼的解決方案。

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

When editing content in a word document, lines may automatically wrap. If no adjustment is made at this time, it will have a great impact on our editing and make people very headache. What is going on? In fact, it is a problem with the ruler. Below, I will introduce the solution to how to cancel automatic word wrapping in word. I hope it can help everyone! After opening a Word document and entering text, when you try to copy and paste, the text may jump to a new line. In this case, you need to adjust the settings to solve this problem. 2. To solve this problem, we must first know the cause of this problem. At this time we click View under the toolbar. 3. Then click the "Ruler" option below. 4. At this time we will find that a ruler appears above the document with several conical markers on it.

When we use Word, in order to edit the content more beautifully, we often use rulers. You should know that the rulers in Word include horizontal rulers and vertical rulers, which are used to display and adjust the document's page margins, paragraph indents, tabs, etc. So, how do you display the ruler in Word? Next, I will teach you how to set the ruler display. Students in need should quickly collect it! The steps are as follows: 1. First, we need to bring up the word ruler. The default word document does not display the word ruler. We only need to click the [View] button in word. 2. Then, we find the option of [Ruler] and check it. In this way, we can adjust the word ruler! Yes or no

Word documents are widely used due to their powerful functions. Not only can various formats be inserted into Word, such as pictures and tables, etc., but now for the integrity and authenticity of the files, many files require a manual signature at the end of the document. It sounds like this How to solve complex problems? Today I will teach you how to add a handwritten signature to a word document. Use a scanner, camera or mobile phone to scan or photograph the handwritten signature, and then use PS or other image editing software to perform necessary cropping on the image. 2. Select "Insert - Picture - From File" in the Word document where you want to insert the handwritten signature, and select the cropped handwritten signature. 3. Double-click the handwritten signature picture (or right-click the picture and select "Set Picture Format"), and the "Set Picture Format" pops up.

Among office software, Word is one of our most commonly used software. The text documents we produce are generally operated with Word. Some documents need to be submitted in paper version as required. Before printing, the layout must be set before it can be presented. produce better results. So the question is, how do you set page margins in Word? We have specific course explanations to help you solve your doubts. 1. Open or create a new word document and click the "Page Layout" menu on the menu bar. 2. Click the "Margins" button of the "Page Setup" option. 3. Select a commonly used page margin in the list. 4. If there are no suitable margins in the list, click "Custom Margins". 5. The "Page Setup" dialog box pops up, enter the "Margins" option respectively.

Word is a very powerful office software. Compared with WPS, Word has more advantages in detail processing. Especially when the document description is too complex, it is generally more worry-free to use Word. Therefore, when you enter the society, you must learn some tips on using word. Some time ago, my cousin asked me a question like this. I often see other people drawing tables when using Word, and I feel very high-level. I laughed at that time. It seemed like high-level content, but actually it only took 3 steps to operate. Do you know how to draw a table in Word? 1. Open word, select the place where you want to insert the table, and find the "Insert" option in the upper menu bar. 2. Click the "Table" option, and densely packed small cubes will appear.

We often use word for office work, but do you know where the shading settings are in word? Today I will share with you the specific operation steps. Come and take a look, friends! 1. First, open the word document, select a paragraph of text paragraph information that needs to be added with shading, then click the [Start] button on the toolbar, find the paragraph area, and click the drop-down button on the right (as shown in the red circle in the figure below) ). 2. After clicking the drop-down box button, in the pop-up menu options, click the [Border and Shading] option (as shown in the red circle in the figure below). 3. In the pop-up [Border and Shading] dialog box, click the [Shading] option (as shown in the red circle in the figure below). 4. In the filled column, select a color

Word is a software that we often use in our office. It has many functions that can facilitate our operations. For example, for a large article, we can use the search function inside to find out that a word in the full text is wrong, so we can directly replace it. Make changes one by one; when submitting the document to your superiors, you can beautify the document to make it look better, etc. Below, the editor will share with you the steps on how to draw a dotted line in Word. Let's learn together! 1. First, we open the word document on the computer, as shown in the figure below: 2. Then, enter a string of text in the document, as shown in the red circle in the figure below: 3. Next, press and hold [ctrl+A] Select all the text, as shown in the red circle in the figure below: 4. Click [Start] on the top of the menu bar

As a very commonly used word processing software, Word is used in our life, study and work all the time. Of course, if you want to make good use of Word to edit text, you must lay a good foundation. So today I will take you to learn how to underline in Word. You can do it together with the editor. It is very simple. 1. First, we open the file we need to edit. Here we take the following figure as an example. 2. Use the mouse to select the text we need to edit. In the pop-up tab, we select the [U] icon. The operation is as shown in the figure: 3. Let’s take a look at the effect: 4. In fact, we can use a more convenient and faster The method is to use the key combination [ctrl] + [U] on the keyboard to add and follow your text.
