Learning & Integrating web technology and code help directory

How to use PHP & Add Style & Format into excel (Excel.Application)

No comments
How to use PHP & Add Style & Format into excel (Excel.Application) The Learn / tutorial php programming how to using  PHP Add Style & Format into excel.
ShotDev Focus:
- PHP  & Add Style & Format into excel.
Example
php_format_style.php
  1. <html>  
  2. <head>  
  3. <title>ShotDev.Com Tutorial</title>  
  4. </head>  
  5. <body>  
  6. <?  
  7. //*** Get Document Path ***//  
  8. $strPath = realpath(basename(getenv($_SERVER["SCRIPT_NAME"]))); // C:/AppServ/www/myphp  
  9.   
  10. //*** Excel Document Root ***//  
  11. $strFileName = "MyXls/MyExcel.xls";  
  12.   
  13. //*** Connect to Excel.Application ***//  
  14. $xlApp = new COM("Excel.Application");  
  15. $xlBook = $xlApp->Workbooks->Add();  
  16. $xlSheet1 = $xlBook->Worksheets(1);  
  17.   
  18. $xlApp->Application->Visible = False;  
  19.   
  20. //*** Create Sheet 1 ***//  
  21. $xlBook->Worksheets(1)->Name = "My Sheet1";  
  22. $xlBook->Worksheets(1)->Select;  
  23.   
  24. //*** Width & Height (A1:A1) ***//  
  25. $xlApp->ActiveSheet->Range("A1:A1")->ColumnWidth = 40.0;  
  26. $xlApp->ActiveSheet->Range("A1:A1")->RowHeight = 25.0;  
  27.   
  28. //*** Write text to Row 1 Column 1 ***//  
  29. $xlApp->ActiveSheet->Cells(1,1)->Value = "ShotDev.Com ";  
  30. $xlApp->ActiveSheet->Cells(1,1)->Font->Name = "Tahoma";  
  31. $xlApp->ActiveSheet->Cells(1,1)->Font->Bold = True;  
  32. $xlApp->ActiveSheet->Cells(1,1)->VerticalAlignment = -4108; //*** Center  Rows ***//  
  33. $xlApp->ActiveSheet->Cells(1,1)->HorizontalAlignment = -4108; //*** Center Column ***//  
  34. $xlApp->ActiveSheet->Cells(1,1)->Font->Size = 12;  
  35.   
  36. //*** Write text to Row 1 Column 2 ***//  
  37. $xlApp->ActiveSheet->Cells(1,2)->Value = "Mr.Weerachai Nukitram ";  
  38. $xlApp->ActiveSheet->Cells(1,2)->Font->Name = "Tahoma";  
  39. $xlApp->ActiveSheet->Cells(1,2)->Font->Size = 20;  
  40.   
  41. //*** Width & Height (A2:A2) ***//  
  42. $xlApp->ActiveSheet->Range("A2:A2")->BORDERS->Weight = 1;  //*** Border ***//  
  43.   
  44. //*** Write text to Row 1 Column 2 ***//  
  45. $xlApp->ActiveSheet->Cells(2,1)->Value = "I Love ShotDev.Com ";  
  46. $xlApp->ActiveSheet->Cells(2,1)->Font->Name = "Tahoma";  
  47. $xlApp->ActiveSheet->Cells(2,1)->Font->Size = 10;  
  48. $xlApp->ActiveSheet->Cells(2,1)->HorizontalAlignment = 4;  
  49.   
  50. //*** Width & Height (A3:D3) ***//  
  51. $xlApp->ActiveSheet->Range("A3:D3")->BORDERS->Color = 44; //*** Border Color ***//  
  52. $xlApp->ActiveSheet->Range("A3:D3")->BORDERS->Weight = 1;  //*** Border ***//  
  53. $xlApp->ActiveSheet->Range("A3:D3")->MergeCells = True;    //*** Merge Cells ***//  
  54.   
  55. //*** Write text to Row 1 Column 2 ***//  
  56. $xlApp->ActiveSheet->Cells(3,1)->Value = "I Love My Live";  
  57. $xlApp->ActiveSheet->Cells(3,1)->Font->Name = "Tahoma";  
  58. $xlApp->ActiveSheet->Cells(3,1)->Font->Size = 10;  
  59. $xlApp->ActiveSheet->Cells(3,1)->HorizontalAlignment = -4108;  
  60. $xlApp->ActiveSheet->Cells(3,1)->Interior->ColorIndex = 44; //*** Background Color ***//  
  61.   
  62. //*** Write text to Row 4 Column 5 ***//  
  63. $xlApp->ActiveSheet->Cells(4,5)->Value = "My Life";  
  64. $xlApp->ActiveSheet->Cells(4,5)->Font->Name = "Tahoma";  
  65. $xlApp->ActiveSheet->Cells(4,5)->Font->Size = 10;  
  66. $xlApp->ActiveSheet->Cells(4,5)->Font->Italic = True;  
  67. $xlApp->ActiveSheet->Cells(4,5)->Font->ColorIndex = 4;  
  68. $xlApp->ActiveSheet->Cells(4,5)->EntireColumn->AutoFit;  //*** AutoFit Column ***//  
  69.   
  70. //*** Write text to Row 5 Column 5 ***//  
  71. $xlApp->ActiveSheet->Cells(5,5)->Value = "My Life";  
  72. $xlApp->ActiveSheet->Cells(5,5)->Font->Name = "Tahoma";  
  73. $xlApp->ActiveSheet->Cells(5,5)->Font->Size = 10;  
  74. $xlApp->ActiveSheet->Cells(5,5)->Font->Italic = True;  
  75. $xlApp->ActiveSheet->Cells(5,5)->Font->ColorIndex = 4;  
  76. $xlApp->ActiveSheet->Cells(5,5)->HorizontalAlignment = -4152; // Text align Right  
  77. $xlApp->ActiveSheet->Cells(5,5)->EntireColumn->AutoFit;  //*** AutoFit Column ***//  
  78.   
  79. //*** Write text to Row 6 Column 1 ***//  
  80. $xlApp->ActiveSheet->Cells(6,1)->Value = "Version 2010";  
  81. $xlApp->ActiveSheet->Cells(6,1)->Font->Name = "Tahoma";  
  82. $xlApp->ActiveSheet->Cells(6,1)->Font->Size = 10;  
  83. $xlApp->ActiveSheet->Cells(6,1)->Characters(1, 7)->Font->ColorIndex = 4;  
  84. $xlApp->ActiveSheet->Cells(6,1)->Characters(8, 12)->Font->Bold = True;  
  85.   
  86. /* 
  87. // SheetType 
  88. xlChart = -4109; 
  89. xlWorksheet = -4167; 
  90. // WBATemplate 
  91. xlWBATWorksheet = -4167; 
  92. xlWBATChart = -4109; 
  93. // Page Setup 
  94. xlPortrait = 1; 
  95. xlLandscape = 2; 
  96. xlPaperA4 = 9; 
  97. // Format Cells 
  98. xlBottom = -4107; 
  99. xlLeft = -4131; 
  100. xlRight = -4152; 
  101. xlTop = -4160; 
  102. // Text Alignment 
  103. xlHAlignCenter = -4108; 
  104. xlVAlignCenter = -4108; 
  105. // Cell Borders 
  106. xlThick = 4; 
  107. xlThin = 2; 
  108. */  
  109.   
  110. @unlink($strFileName); //*** Delete old files ***//  
  111.   
  112. $xlBook->SaveAs($strPath."/".$strFileName); //*** Save to Path ***//  
  113. //$xlBook->SaveAs(realpath($strFileName)); //*** Save to Path ***//  
  114.   
  115. //*** Close & Quit ***//  
  116. $xlApp->Application->Quit();  
  117. $xlApp = null;  
  118. $xlBook = null;  
  119. $xlSheet1 = null;  
  120. ?>  
  121. Excel Created <a href="<?=$strFileName?>">Click here</a> to Download.  
  122. </body>  
  123. </html>  
Create a php file and save to path root-path/myphp/
Screenshot
PHP & Add Style & Format into excel (Excel.Application)

No comments :

Post a Comment