JezK
Edit File: excel_download.php
<?php session_start(); include('connect.php'); $total_marks=array(); $names="select c.class_name,s.section_name from ced_t_class c join ced_t_sections s on c.class_id=s.class_id where c.class_id=".$_REQUEST['classname']." and s.section_id=".$_REQUEST['sectionname']; $getname=mysql_query($names); $resnames=mysql_fetch_array($getname); $exm="select exam_name from ced_t_exam_names where exam_id=".$_REQUEST['examname']; $exmname=mysql_query($exm); $exname=mysql_fetch_array($exmname); $sql="select s.subject_name,et.total_marks from `ced_t_exam_timetable` et inner join `ced_t_subjects` s on et.subject_id=s.subject_id where exam_id=".$_REQUEST['examname']." and class_id=".$_REQUEST['classname']." and section_id=".$_REQUEST['sectionname']." order by timetable_id"; $exe=mysql_query($sql); $nosub=mysql_num_rows($exe); //echo $sql_income."<br>"; function xlsBOF() { echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0); return; } function xlsEOF() { echo pack("ss", 0x0A, 0x00); return; } function xlsWriteNumber($Row, $Col, $Value) { echo pack("sssss", 0x204, 14, $Row, $Col, 0x0); echo pack("d", $Value); return; } function xlsWriteLabel($Row, $Col, $Value ) { $L = strlen($Value); echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L); echo $Value; return; } // Unique file name with datetime $file_name = "student_marks"; header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Content-Type: application/force-download"); header("Content-Type: application/octet-stream"); header("Content-Type: application/download"); header("Content-Disposition: attachment;filename=$file_name.xls "); header("Content-Transfer-Encoding: binary "); xlsBOF(); // Top of the excel sheet to write header first line xlsWriteLabel(0,0,"Class"); xlsWriteLabel(0,1,$resnames['class_name']); xlsWriteLabel(0,2,"Section"); xlsWriteLabel(0,3,$resnames['section_name']); xlsWriteLabel(0,4,"Exam Name"); xlsWriteLabel(0,5,$exname['exam_name']); // headers second line xlsWriteLabel(1,0,""); xlsWriteLabel(1,1,""); if($nosub) { $i=0; $j=3; while($res=mysql_fetch_array($exe)) { xlsWriteLabel(1,$j,$res['subject_name']); xlsWriteLabel(1,$j+1,""); xlsWriteLabel(1,$j+2,""); $total_marks[$i]=$res['total_marks']; $j=$j+3; $i++; } xlsWriteLabel(1,$j-1,"Grades/Rank"); } // headers third line xlsWriteLabel(2,0,"Roll Number"); xlsWriteLabel(2,1,"Student Name"); for($i=1,$j=2;$i<=$nosub;$i++) { xlsWriteLabel(2,$j,"Total Marks"); xlsWriteLabel(2,$j+1,"Secured Marks*"); xlsWriteLabel(2,$j+2,"Status*"); $j=$j+3; } $qry="select student_rollno,student_firstname,student_lastname from ced_t_students where class_id=".$_REQUEST['classname']." and section_id=".$_REQUEST['sectionname']; $execute=mysql_query($qry); if(mysql_num_rows($execute)) { $row=3; $subs=2; while($rec=mysql_fetch_array($execute)) { $stdname=$rec['student_firstname'].' '.$rec['student_lastname']; xlsWriteLabel($row,0,$rec['student_rollno']); xlsWriteLabel($row,1,$stdname); for($i=0;$i<$nosub;$i++) { xlsWriteLabel($row,$subs,$total_marks[$i]); xlsWriteLabel($row,$subs+1,""); xlsWriteLabel($row,$subs+2,""); $subs=$subs+3; } $row++; $subs=2; } } xlsEOF(); exit();