Learning & Integrating web technology and code help directory

How to use PHP & Chart/Graph - Export to Gif,Jpg (Excel.Application)

No comments
How to use PHP & Chart/Graph - Export to Gif,Jpg (Excel.Application) This learn / tutorial php programming how to using  PHP Create Chart/Graph - Export to Gif,Jpg
ShotDev Focus:
- PHP  & Create Chart/Graph - Export to Gif,Jpg
Example
php_chart_gif.php

  1. <html>  
  2. <head>  
  3. <title>ShotDev.Com Tutorial</title>  
  4. </head>  
  5. <body>  
  6. <?  
  7. //*** Connect to MySQL Database ***//  
  8. $objConnect = mysql_connect("localhost","root","root"or die(mysql_error());  
  9. $objDB = mysql_select_db("mydatabase");  
  10. $strSQL = "SELECT * FROM customer";  
  11. $objQuery = mysql_query($strSQL);  
  12. if($objQuery)  
  13. {  
  14. //*** Get Document Path ***//  
  15. $strPath = realpath(basename(getenv($_SERVER["SCRIPT_NAME"]))); // C:/AppServ/www/myphp  
  16.   
  17. //*** File Name Gif,Jpeg,... ***//  
  18. $FileName = "MyXls/MyChart.Gif";  
  19. $Ext = "Gif";  
  20.   
  21. //*** Excel Name ***//  
  22. $XlsName = "MyXls/MyChart.xls";  
  23.   
  24. //*** Connect to Excel.Application ***//  
  25. $xlApp = new COM("Excel.Application");  
  26. $xlBook = $xlApp->Workbooks->Add();  
  27.   
  28. $intStartRows = 3;  
  29. $intEndRows = mysql_num_rows($objQuery)+($intStartRows-1);  
  30.   
  31. $xlSheet = $xlBook->Worksheets(1);  
  32.   
  33. $xlApp->Application->Visible = False;  
  34.   
  35. //*** Delete Sheet (2,3) - Sheet Default ***//  
  36. $xlBook->Worksheets(2)->Select;  
  37. $xlBook->Worksheets(2)->Delete;  
  38. $xlBook->Worksheets(2)->Select;  
  39. $xlBook->Worksheets(2)->Delete;  
  40.   
  41. //*** Sheet Data Rows ***//  
  42. $xlBook->Worksheets(1)->Name = "MyReport";  
  43. $xlBook->Worksheets(1)->Select;  
  44.   
  45. $xlBook->ActiveSheet->Cells(1,1)->Value = "My Customer";  
  46. $xlBook->ActiveSheet->Cells(1,1)->Font->Bold = True;  
  47. $xlBook->ActiveSheet->Cells(1,1)->Font->Name = "Tahoma";  
  48. $xlBook->ActiveSheet->Cells(1,1)->Font->Size = 16;  
  49.   
  50. $xlBook->ActiveSheet->Cells(2,1)->Value = "Customer Name";  
  51. $xlBook->ActiveSheet->Cells(2,1)->Font->Name = "Tahoma";  
  52. $xlBook->ActiveSheet->Cells(2,1)->BORDERS->Weight = 1;  
  53. $xlBook->ActiveSheet->Cells(2,1)->Font->Size = 10;  
  54. $xlBook->ActiveSheet->Cells(2,1)->MergeCells = True;  
  55.   
  56. $xlBook->ActiveSheet->Cells(2,2)->Value = "Used";  
  57. $xlBook->ActiveSheet->Cells(2,2)->BORDERS->Weight = 1;  
  58. $xlBook->ActiveSheet->Cells(2,2)->Font->Name = "Tahoma";  
  59. $xlBook->ActiveSheet->Cells(2,2)->Font->Size = 10;  
  60. $xlBook->ActiveSheet->Cells(2,2)->MergeCells = True;  
  61.   
  62. $i = 0;  
  63. While($result = mysql_fetch_array($objQuery))  
  64. {  
  65. $xlBook->ActiveSheet->Cells($intStartRows+$i,1)->Value = $result["Name"];  
  66. $xlBook->ActiveSheet->Cells($intStartRows+$i,2)->Value = $result["Used"];  
  67. $xlBook->ActiveSheet->Cells($intStartRows+$i,2)->NumberFormat = "$#,##0.00";  
  68. $i++;  
  69. }  
  70. //*** End Data Rows ***//  
  71.   
  72. //*** Creating Chart ***//  
  73. $xlBook->Charts->Add;  
  74. $xlBook->ActiveChart->ChartType = 54;  
  75. $xlBook->ActiveChart->SetSourceData ($xlBook->Sheets("MyReport")->Range("A".$intStartRows.":B".$intEndRows.""));  
  76.   
  77. //*** Set Localtion Sheet ***//  
  78. $xlBook->ActiveChart->Location(2,"MyReport");  
  79.   
  80. //*** Sheet Properties ***//  
  81. $xlBook->ActiveChart->HasTitle = True;  
  82. $xlBook->ActiveChart->ChartTitle->Characters->Text = "My Chart";  
  83.   
  84. //*** Location  ***//  
  85. $xlBook->ActiveSheet->Shapes("Chart 1")->IncrementLeft(20);  
  86. $xlBook->ActiveSheet->Shapes("Chart 1")->IncrementTop(-97.5);  
  87.   
  88. //'*** Set Height & Width ***'  
  89. $xlBook->ActiveSheet->Shapes("Chart 1")->ScaleHeight(1.0, 0,0);  
  90. $xlBook->ActiveSheet->Shapes("Chart 1")->ScaleWidth(1.0, 0,0);  
  91.   
  92. //*** Save Charts ***//  
  93. @unlink($strPath."/".$FileName);  
  94. $xlApp->ActiveChart->Export($strPath."/".$FileName,$Ext);  
  95.   
  96. //*** Save Excel ***//  
  97. @unlink($strPath."/".$XlsName);  
  98. $xlBook->SaveAs($strPath."/".$XlsName);  
  99. //$xlBook->SaveAs(realpath($XlsName));  
  100.   
  101. $xlApp->Application->Quit;  
  102. }  
  103. ?>  
  104. <strong>Charts Created</strong><br><br>  
  105. <img src="<?=$FileName?>">  
  106. </body>  
  107. </html>  
Create a php file and save to path root-path/myphp/
Screenshot
PHP & Chart/Graph - Export to Gif,Jpg (Excel.Application)
.

No comments :

Post a Comment