Learning & Integrating web technology and code help directory

How to use PHP & Open Excel on Specified Worksheet (.xls)

No comments
How to use PHP & Open Excel on Specified Worksheet (.xls) (Excel.Application) The Learn / tutorial php programming how to using  PHP Open Excel on Specified Worksheet.
ShotDev Focus:
- PHP  & Open Excel on Specified Worksheet.
PHP & Open Excel on Specified Worksheet (.xls)  (Excel.Application)
Example
php_open_write.php
  1. <html>  
  2. <head>  
  3. <title>ShotDev.Com Tutorial</title>  
  4. </head>  
  5. <body>  
  6. <?  
  7.   
  8. //*** Get Document Path ***//  
  9. $strPath = realpath(basename(getenv($_SERVER["SCRIPT_NAME"]))); // C:/AppServ/www/myphp  
  10.   
  11. $xlEdgeLeft = 7;  
  12. $xlEdgeTop = 8;  
  13. $xlEdgeBottom = 9;  
  14. $xlEdgeRight = 10;  
  15.   
  16. //*** Excel Open Document ***//  
  17. $strOpenDoc = "Excel.xls";  
  18.   
  19. //*** Excel Document Root ***//  
  20. $strFileName = "MyXls/MyExcel.xls";  
  21.   
  22. //*** Connect to Excel.Application ***//  
  23. $xlApp = new COM("Excel.Application");  
  24. $xlBook = $xlApp->Workbooks->Open($strPath."/".$strOpenDoc);  
  25. $xlSheet1 = $xlBook->Worksheets(1);  
  26. $xlApp->Application->Visible = False;  
  27.   
  28. //*** Sheet 1 ***//  
  29. $xlSheet1->Name = "My Sheet1";  
  30.   
  31. //*** Write text to Row 3 Column 1 ***//  
  32. for($i = 3;$i <= 10;$i++)  
  33. {  
  34. $xlApp->ActiveSheet->Cells($i,1)->Value = "Data 1";  
  35. $xlApp->ActiveSheet->Cells($i,2)->Value = "Data 2";  
  36. $xlApp->ActiveSheet->Cells($i,3)->Value = "Data 3";  
  37. $xlApp->ActiveSheet->Cells($i,4)->Value = "Data 4";  
  38. }  
  39.   
  40. @unlink($strFileName); //*** Delete old files ***//  
  41.   
  42. $xlBook->SaveAs($strPath."/".$strFileName); //*** Save to Path ***//  
  43.   
  44. //*** Close & Quit ***//  
  45. $xlApp->Application->Quit();  
  46. $xlApp = null;  
  47. $xlBook = null;  
  48. $xlSheet1 = null;  
  49. ?>  
  50. Excel Created <a href="<?=$strFileName?>">Click here</a> to Download.  
  51.   
  52. </body>  
  53. </html>  
Create a php file and save to path root-path/myphp/
Screenshot
PHP & Open Excel on Specified Worksheet (.xls) (Excel.Application)

No comments :

Post a Comment