Learning & Integrating web technology and code help directory

How to use PHP & Write Multiple Sheet into excel (Excel.Application)

No comments
How to use PHP & Write Multiple Sheet into excel (Excel.Application) The Learn / tutorial php programming how to using  PHP Write Multiple Sheet into excel
ShotDev Focus:
- PHP  & Write Multiple Sheet into excel.
Example
php_multiple_sheet.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. //*** Excel Document Root ***//  
  12. $strFileName = "MyXls/MyExcel.xls";  
  13.   
  14. //*** Connect to Excel.Application ***//  
  15. $xlApp = new COM("Excel.Application");  
  16. $xlBook = $xlApp->Workbooks->Add();  
  17. $xlSheet1 = $xlBook->Worksheets(1);  
  18.   
  19. $xlApp->Application->Visible = False;  
  20.   
  21. //*** Add Sheet 4 ***//  
  22. $xlBook->Sheets->Add; //*** Sheet Default=3 Add New 1 Sheet=4 ***//  
  23.   
  24. //*** Create Sheet 1 ***//  
  25. $xlBook->Worksheets(1)->Name = "My Sheet1";  
  26. $xlBook->Worksheets(1)->Select;  
  27.   
  28. //*** Write text to Row 1 Column 1 ***//  
  29. $xlApp->ActiveSheet->Cells(1,1)->Value = "ShotDev.Com 1";  
  30.   
  31. //*** Write text to Row 1 Column 2 ***//  
  32. $xlApp->ActiveSheet->Cells(1,2)->Value = "Mr.Weerachai Nukitram 1";  
  33.   
  34. //*** Create Sheet 2 ***//  
  35. $xlBook->Worksheets(2)->Name = "My Sheet2";  
  36. $xlBook->Worksheets(2)->Select;  
  37.   
  38. //*** Write text to Row 1 Column 1 ***//  
  39. $xlApp->ActiveSheet->Cells(1,1)->Value = "ShotDev.Com 2";  
  40.   
  41. //*** Write text to Row 1 Column 2 ***//  
  42. $xlApp->ActiveSheet->Cells(1,2)->Value = "Mr.Weerachai Nukitram 2";  
  43.   
  44. //*** Create Sheet 3 ***//  
  45. $xlBook->Worksheets(3)->Name = "My Sheet3";  
  46. $xlBook->Worksheets(3)->Select;  
  47.   
  48. //*** Write text to Row 1 Column 1 ***//  
  49. $xlApp->ActiveSheet->Cells(1,1)->Value = "ShotDev.Com 3";  
  50.   
  51. //*** Write text to Row 1 Column 2 ***//  
  52. $xlApp->ActiveSheet->Cells(1,2)->Value = "Mr.Weerachai Nukitram 3";  
  53.   
  54. //*** Create Sheet 4 ***//  
  55. $xlBook->Worksheets(4)->Name = "My Sheet4";  
  56. $xlBook->Worksheets(4)->Select;  
  57.   
  58. //*** Write text to Row 1 Column 1 ***//  
  59. $xlApp->ActiveSheet->Cells(1,1)->Value = "ShotDev.Com 4";  
  60.   
  61. //*** Write text to Row 1 Column 2 ***//  
  62. $xlApp->ActiveSheet->Cells(1,2)->Value = "Mr.Weerachai Nukitram 4";  
  63.   
  64. $xlBook->Worksheets(1)->Select; //*** Focus Sheet 1 ***//  
  65.   
  66. @unlink($strFileName); //*** Delete old files ***//  
  67.   
  68. $xlBook->SaveAs($strPath."/".$strFileName); //*** Save to Path ***//  
  69. //$xlBook->SaveAs(realpath($strFileName)); //*** Save to Path ***//  
  70.   
  71. //*** Close & Quit ***//  
  72. $xlApp->Application->Quit();  
  73. $xlApp = null;  
  74. $xlBook = null;  
  75. $xlSheet1 = null;  
  76. ?>  
  77. Excel Created <a href="<?=$strFileName?>">Click here</a> to Download.  
  78. </body>  
  79. </html>  
Create a php file and save to path root-path/myphp/
Screenshot
PHP & Write Multiple Sheet into excel (Excel.Application)

No comments :

Post a Comment