Advertisement
Guest User

Untitled

a guest
Feb 1st, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.43 KB | None | 0 0
  1. <!DOCTYPE html">
  2. <html>
  3. <head>
  4. <meta charset=utf-8" />
  5. <title>Generate Reports</title>
  6.  
  7. <link rel="stylesheet" type="text/css" href="style.css" media="screen" />
  8.  
  9. <!--Display the Criteria screen only if Title Org Codes Report (rpt3) is selected-->
  10. <script type="text/javascript">
  11. function showForm() {
  12. var selopt = document.getElementById("selReport").value;
  13.  
  14. if (selopt === "rpt3") {
  15. document.getElementById("criteria").style.display = "block";
  16. }
  17.  
  18. else {
  19. document.getElementById("criteria").style.display = "none";
  20. }
  21.  
  22. }
  23. </script>
  24.  
  25. </head>
  26. <body>
  27. <div id="wrapper">
  28.  
  29. <div id="content">
  30.  
  31. <div id="reports" class="center">
  32. <h3> Generate Reports: </h3>
  33.  
  34. <!--Display the list of reports-->
  35. <form id="frm1" name="frm1" method="post" action="Reports.php">
  36. <!-- Excel, PDF, View buttons -->
  37. <input type="submit" value="Preview Report" id="view" name="view">
  38. <input type="submit" value="Export to Excel" id="excel" name="excel">
  39. <input type="submit" value="Publish as PDF" id="pdf" name="pdf">
  40. <br><br><br>
  41.  
  42. Select a Report:
  43. <select id="selReport" name="selReport" onclick="showForm();">
  44. <option></option>
  45. <option value="rpt1">Units/Ranges Summary</option>
  46. <option value="rpt2">Divisions Table</option>
  47. <option value="rpt3">Title Codes</option>
  48. </select>
  49.  
  50.  
  51. <!--Creates the criteria drop down menu-->
  52. <div id="criteria" style="display:none">
  53. <br><br><h3>Selection Criteria for Reports:</h3>
  54. Title File Status:
  55. <select name="selCriteria" id="selCriteria" onchange="showForm();">
  56. <option></option>
  57. <option value="active">Active</option>
  58. <option value="inactive">Inactive</option>
  59. <option value="all">All</option>
  60. </select>
  61. </div> <!--end criteria -->
  62.  
  63. </form> <!-- end frm1 -->
  64.  
  65. </div> <!-- end #reports -->
  66.  
  67. </div> <!-- end #content -->
  68.  
  69. </div> <!-- end #wrapper -->
  70. </body>
  71. </html>
  72.  
  73. <?php
  74. //Get the user selection and put into variables
  75. $varRpt = $_POST['selReport'];
  76. $varCrit = $_POST['selCriteria'];
  77.  
  78. // require the PHPExcel classes
  79. require 'PHPExcel/Classes/PHPExcel.php';
  80.  
  81. // PHPExcel_Writer_Excel2007
  82. require 'PHPExcel/Classes/PHPExcel/Writer/Excel2007.php';
  83.  
  84. //-------------------------------
  85. // Connect to the MySQL database
  86. //-------------------------------
  87. $dbhost = "localhost";
  88. $dbuser = "root";
  89. $dbpass = "*******";
  90. $dbname = "test";
  91.  
  92. mysql_connect($dbhost,$dbuser,$dbpass)or die ("Could not connect to mysql because ".mysql_error());
  93.  
  94. mysql_select_db($dbname)or die("Unable to select database" .mysql_error());
  95.  
  96. //-------------------------------------
  97. // Figure out which report to generate
  98. //-------------------------------------
  99.  
  100. if ($varRpt == "rpt1")
  101. {
  102. $query = "SELECT a.div_id, a.unit_id, b.unit_beg, b.unit_end, a.title_org, c.long_desc, c.short_desc FROM depunits a LEFT OUTER JOIN unitRanges b ON ( a.unit_id )= ( b.unit_id ) LEFT OUTER JOIN orgcodes c ON ( a.title_org )= ( c.l1l5_id ) ORDER BY a.div_id, a.unit_id" ;
  103.  
  104. //Column headings
  105. $headings = array('Div_id','Unit Id','Unit Begin','Unit End','Title Org','Long Desc','Short Desc');
  106.  
  107. // Sheet name
  108. $title = "Summary Report Units/Ranges";
  109.  
  110. // Name of the saved excel file
  111. $filename = "Rpt1_" . date('Ymd') . ".xls";
  112. }
  113. else
  114. if ($varRpt == "rpt2")
  115. {
  116.  
  117. $query = "SELECT alldiv_id, div_id, L1l2_id, L2_id, L1l3_id, L2l3_id, Exec_beg, Exec_end, Csa_id, Area_id, Area_Desc, Short_Desc, Long_Desc FROM divisions WHERE avail_ind='Y' AND active_ind='Y' ORDER BY alldiv_id ";
  118.  
  119. // Column Labels
  120. $headings = array('All','Div','L1L2','L2','L1L3','L2L3','Exec Begin','Exec End','CSA','Area Id','Area Desc','Short Desc','Long Desc');
  121.  
  122. // Report Title
  123. $title = "Divisions Table";
  124.  
  125. // name of the saved excel file
  126. $filename = "Rpt2_" . date('Ymd') . ".xls";
  127.  
  128. } // end $varRpt == "rpt2"
  129.  
  130. else
  131. if ($varRpt == "rpt3")
  132. {
  133. //Column heading
  134. $headings = array('Title Code','Short Title','Long Title','Status');
  135.  
  136. // Report title
  137. $title = "Title Codes";
  138.  
  139. // Name of the saved file
  140. $filename = "Rpt3_" . date('Ymd') . ".xls";
  141.  
  142. if ($varCrit == "active")
  143. {
  144. $query = "SELECT L2l5, Stitl, Ltitl, Status FROM Tl2l5 WHERE UPPER(TRIM(status))= 'A' ORDER BY L2l5";
  145. }
  146. else
  147. if ($varCrit == "inactive")
  148. {
  149. $query = "SELECT L2l5, Stitl, Ltitl, Status FROM Tl2l5 WHERE UPPER(TRIM(status))= 'I' ORDER BY L2l5";
  150. }
  151. else
  152. if ($varCrit == "all")
  153. {
  154. $query = "SELECT L2l5, Stitl, Ltitl, Status FROM Tl2l5 ORDER BY L2l5";
  155. }
  156. }
  157.  
  158.  
  159.  
  160. //-----------------------------------------
  161. // Insert data into Excel Report template
  162. //-----------------------------------------
  163.  
  164. // Create a new PHPExcel object
  165. $objPHPExcel = new PHPExcel();
  166.  
  167.  
  168. // Sheet name
  169. $objPHPExcel->getActiveSheet()->setTitle('sheet1');
  170.  
  171. // Set the default font type (Arial) and the size (10)
  172. $objPHPExcel->getActiveSheet()->getDefaultStyle()->getFont()->setName('Arial')->setSize(10);
  173.  
  174. // Set the column headings to row 3 and data to start on row 4
  175. $rowHeadStart = 3;
  176. $rowDataStart = 4;
  177.  
  178. //Merge Cells for the report titles
  179. $objPHPExcel->getActiveSheet()->mergeCells("A1:T1"); // Report Title
  180. $objPHPExcel->getActiveSheet()->mergeCells("A2:T2"); // Date
  181.  
  182. //Set Cell Text
  183. $objPHPExcel->getActiveSheet()->setCellValue("A1", $title);
  184. $objPHPExcel->getActiveSheet()->setCellValue('A2', date("m/d/Y"));
  185.  
  186. //Make Report title bold
  187. $objPHPExcel->getActiveSheet()->getStyle("A1:T1")->applyFromArray(array("font" => array( "bold" => true)));
  188. //Make the date italicized
  189. $objPHPExcel->getActiveSheet()->getStyle("A2:T2")->applyFromArray(array("font" => array( "italic" => true)));
  190. //Make Column headers bold
  191. $objPHPExcel->getActiveSheet()->getStyle("A3:T3")->applyFromArray(array("font" => array( "bold" => true)));
  192.  
  193.  
  194. //---------------------------------------------
  195. // Loop through to display the column headings
  196. //---------------------------------------------
  197. $col = 'A';
  198. foreach($headings as $heading)
  199. {
  200. $objPHPExcel->getActiveSheet()->setCellValue($col.$rowHeadStart,$heading);
  201. $col++;
  202. } // end $headings as $heading
  203.  
  204. //-------------------------------------------------
  205. // Loop through the result set to display the data
  206. //-------------------------------------------------
  207. while ($row = mysql_fetch_row($result))
  208. {
  209. $col = 'A';
  210. foreach($row as $cell)
  211. {
  212. $objPHPExcel->getActiveSheet()->setCellValue($col.$rowDataStart,$cell);
  213. $objPHPExcel->getActiveSheet()->getColumnDimension($col)->setAutoSize(true);
  214. $col++;
  215.  
  216. } // end $row as $cell
  217. $rowDataStart++;
  218.  
  219.  
  220. //-----------------------
  221. // Page/Cell Formatting
  222. //-----------------------
  223. //Set font size for the main report title
  224. $objPHPExcel->getActiveSheet()->getStyle("A1")->getFont()->setSize(16);
  225.  
  226. $objPHPExcel->setActiveSheetIndex(0);
  227.  
  228. // Left align the entire document
  229. $objPHPExcel->getDefaultStyle()->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  230.  
  231. // Set the page orientation to landscape
  232. $objPHPExcel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
  233.  
  234. } //end $row = mysql_fetch_row($result)
  235.  
  236.  
  237. // Redirect output to a client’s web browser (Excel2007)
  238. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  239. header('Content-Disposition: attachment;filename="0teste.xlsx"');
  240. header('Cache-Control: max-age=0');
  241.  
  242. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  243. $objWriter->save('php://output');
  244.  
  245.  
  246. exit();
  247.  
  248. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement