13.5 Working with COM Objects


You are an ASP programmer, and you want to have the same ability to handle COM objects in PHP that you had in ASP.

Technique

With the advent of PHP 4, you can now manipulate COM objects with unparalleled ease:

 <?php $word = new COM("word.application")   or die("Unable to instanciate Word"); print "Loaded Word, version {$word->Version}\n"; $word->Visible = 0; $word->Documents->Add(); $word->Selection->TypeText("Testing, testing 1,2,3"); $word->Documents[1]->SaveAs("some_tst.doc"); $word->Quit(); ?> 

Comments

For a long time, one of strongest arguments for ASP was the ability for programmers to access prebuilt COM objects from the Web inside the code, meaning that you really didn't need to do that much programming in ASP. You could write the application end of your Web site in C, C++, or Visual Basic, and then interface it to the Web with ASP and COM objects. Now that PHP supports this feature, I don't really see a reason ever to use ASP. PHP is cross-platform, and much easier to work with or learn, if you have programmed in a language such as C, Perl, or Java. Take the following example in PHP, and compare it to the same example in ASP:

 <?php require("DB.php"); $excel_handle = new COM("excel.application"); $excel_handle->Visible = false; $worksheet = $excel_handle->workbooks->add(); $values = array("Name", "Salary", "Time of Employment"); for ($i = 1; $i < 4; ++$i) {     $cell = &$worksheet->Cells(1, $i);     $cell->value = $values[$i - 1]; $dbh = new DB; $dbh->connect("mssql://username:password@localhost/empreports"); $sth = $dbh->query("SELECT * FROM empnames WHERE salary='salary'"); $idx = 2;     while ($row = $dbh->fetchRow($sth, DB_GETMODE_ASSOC)) {     $values = array($row['name'], $row['Salary'], $row['toe']);     for ($i = 0; $i < 4; ++$i) {         $cell = &$worksheet->Cells($idx, $i);         $cell->value = $values[$i - 1];     } $dbh->disconnect(); $worksheet->SaveAs("emp_reports-$salary.xls"); $excel_handle->quit(); ?> 

Here is the same example in ASP:

 <% Dim excel_handle, worksheet, FileName 'Manipulating the xls file Dim dbh, sth, stmt                    'Database related Dim i, x                              'For looping and assigning Dim Salary                            'From user input set Salary = request.queryString("salary") set excel_handle = server.createObject("excel.application") 'COM Object excel_handle.visible = False excel_handle.workbooks.Add set worksheet = excel_handle.Worksheets(1) ' START DATABASE CONNECTION AND ADD RECORDS set dbh = server.createObject("ADODB.Connection") dbh.Open "DSN=empreports;uid=username;pwd=password" set stmt = "SELECT * FROM empnames WHERE salary='" stmt = stmt & Salary & "'" set x=2 For i=0 to sth.eof     Worksheet.Cells(x,1) = sth(i).name     Worksheet.Cells(x,2) = sth(i).salary     Worksheet.Cells(x++, 3) = sth(i).toe     sth.movenext next dbh.close worksheet.SaveAs "emp_reports-" & Salary & ".xls" excel_handle.quit set worksheet = Nothing set excel_handle = Nothing %> 

Judge for yourself.



PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net