Learning & Integrating web technology and code help directory

How to use PHP & Word (Word.Application) - Word Document Sample Report

No comments
How to use PHP & Word (Word.Application) - Word Document Sample Report The Learn / Lutorial / Sctipts php programming how to using  PHP export/report to word document
ShotDev Focus:
- PHP  & Export/report to word document
Example
php_word_report.php

  1. <html>  
  2. <head>  
  3. <title>ShotDev.Com Tutorial</title>  
  4. </head>  
  5. <body>  
  6. <?  
  7. $wdAlignParagraphCenter = "1";  
  8. $wdAlignParagraphRight = "2";  
  9.   
  10. $Wrd = new COM("Word.Application");  
  11. $DocName = "MyDoc/MyWord.doc";  
  12. $Wrd->Application->Visible = False;  
  13.   
  14. //$strPath = realpath(basename(getenv($_SERVER["SCRIPT_NAME"]))); // C:/AppServ/www/myphp  
  15.   
  16. $WrdDoc = $Wrd->Documents->Open(realpath("shotdev.dot"));  
  17.   
  18. $MyRange1 = $WrdDoc->Paragraphs->Add->Range;  
  19. $MyRange1->ParagraphFormat->Alignment = $wdAlignParagraphCenter;  
  20. $MyRange1->Font->Name = "Verdana";  
  21. $MyRange1->Font->Size = "20";  
  22. $MyRange1->Font->Bold = True;  
  23. $MyRange1->InsertBefore("Customer Report".chr(13));  
  24.   
  25. $objConnect = mysql_connect("localhost","root","root"or die(mysql_error());  
  26. $objDB = mysql_select_db("mydatabase");  
  27. $strSQL = "SELECT * FROM customer";  
  28. $objQuery = mysql_query($strSQL);  
  29. $intRows = mysql_num_rows($objQuery);  
  30.   
  31. $MyRange2 = $WrdDoc->Paragraphs->Add->Range;  
  32. $MyRange2->Font->Size = "10";  
  33. $objTable = $Wrd->ActiveDocument->Tables->Add($MyRange2,$intRows+1,6,1,2); //** Range,Rows,Column **//  
  34.   
  35. //*** Header ***//  
  36. $objTable->Cell(1,1)->Range->InsertAfter("CustomerID");  
  37. $objTable->Cell(1,1)->Range->Bold = True;  
  38. $objTable->Cell(1,1)->Range->ParagraphFormat->Alignment = 1;  
  39.   
  40. $objTable->Cell(1,2)->Range->InsertAfter("Name");  
  41. $objTable->Cell(1,2)->Range->Bold = True;  
  42. $objTable->Cell(1,2)->Range->ParagraphFormat->Alignment = 1;  
  43.   
  44. $objTable->Cell(1,3)->Range->InsertAfter("Email");  
  45. $objTable->Cell(1,3)->Range->Bold = True;  
  46. $objTable->Cell(1,3)->Range->ParagraphFormat->Alignment = 1;  
  47.   
  48. $objTable->Cell(1,4)->Range->InsertAfter("CountryCode");  
  49. $objTable->Cell(1,4)->Range->Bold = True;  
  50. $objTable->Cell(1,4)->Range->ParagraphFormat->Alignment = 1;  
  51.   
  52. $objTable->Cell(1,5)->Range->InsertAfter("Budget");  
  53. $objTable->Cell(1,5)->Range->Bold = True;  
  54. $objTable->Cell(1,5)->Range->ParagraphFormat->Alignment = 1;  
  55.   
  56. $objTable->Cell(1,6)->Range->InsertAfter("Used");  
  57. $objTable->Cell(1,6)->Range->Bold = True;  
  58. $objTable->Cell(1,6)->Range->ParagraphFormat->Alignment = 1;  
  59.   
  60. //*** Detail ***//  
  61. $intRows = 1;  
  62. while($result = mysql_fetch_array($objQuery))  
  63. {  
  64. $intRows++;  
  65. $objTable->Cell($intRows,1)->Range->InsertAfter($result["CustomerID"]);  
  66. $objTable->Cell($intRows,1)->Range->ParagraphFormat->Alignment = 1;  
  67.   
  68. $objTable->Cell($intRows,2)->Range->InsertAfter($result["Name"]);  
  69. $objTable->Cell($intRows,2)->Range->ParagraphFormat->Alignment = 0;  
  70.   
  71. $objTable->Cell($intRows,3)->Range->InsertAfter($result["Email"]);  
  72. $objTable->Cell($intRows,3)->Range->ParagraphFormat->Alignment = 0;  
  73.   
  74. $objTable->Cell($intRows,4)->Range->InsertAfter($result["CountryCode"]);  
  75. $objTable->Cell($intRows,4)->Range->ParagraphFormat->Alignment = 1;  
  76.   
  77. $objTable->Cell($intRows,5)->Range->InsertAfter(number_format($result["Budget"],2));  
  78. $objTable->Cell($intRows,5)->Range->ParagraphFormat->Alignment = 2;  
  79.   
  80. $objTable->Cell($intRows,6)->Range->InsertAfter(number_format($result["Used"],2));  
  81. $objTable->Cell($intRows,6)->Range->ParagraphFormat->Alignment = 2;  
  82. }  
  83.   
  84. $MyRange3 = $WrdDoc->Paragraphs->Add->Range;  
  85. $MyRange3->ParagraphFormat->Alignment = $wdAlignParagraphRight;  
  86. $MyRange3->Font->Name = "Verdana";  
  87. $MyRange3->Font->Size = "10";  
  88. $MyRange3->InsertBefore(chr(13).chr(13).chr(13)." ................................Manager".chr(13).date("Y-m-d H:i:s"));  
  89.   
  90. //$WrdDoc->SaveAs($strPath."/".$DocName);  
  91. $WrdDoc->SaveAs(realpath($DocName));  
  92. $Wrd->Application->Quit;  
  93. $Wrd = null;  
  94. ?>  
  95. Word Created <a href="<?=$DocName?>">Click here</a> to Download.  
  96. </body>  
  97. </html>  
Create a php file and save to path root-path/myphp/
Screenshot
PHP & Word Document Sample Report
.

No comments :

Post a Comment