JezK
Edit File: 01simple-download-xls.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); require_once 'Classes/PHPExcel.php'; require_once 'Classes/PHPExcel/IOFactory.php'; // Create new PHPExcel object $objPHPExcel = new PHPExcel(); // Create a first sheet, representing sales data $objPHPExcel->setActiveSheetIndex(0); $objPHPExcel->getActiveSheet()->setCellValue('A1', 'Please follow the below instruction and upload the student marks'); $objPHPExcel->getActiveSheet()->setCellValue('A3', 'Please follow the below instruction and upload the student marks'); $objPHPExcel->getActiveSheet()->setCellValue('A4', 'Please follow the below instruction and upload the student marks'); $objPHPExcel->getActiveSheet()->setCellValue('A5', 'Please follow the below instruction and upload the student marks'); // Rename sheet $objPHPExcel->getActiveSheet()->setTitle('Instructions'); // Create a new worksheet, after the default sheet $objPHPExcel->createSheet(); // Add some data to the second sheet, resembling some different data types $objPHPExcel->setActiveSheetIndex(1); $objPHPExcel->getActiveSheet()->setCellValue('A1', 'Class'); $objPHPExcel->getActiveSheet()->setCellValue('B1', $resnames['class_name']); $objPHPExcel->getActiveSheet()->setCellValue('C1', 'Section'); $objPHPExcel->getActiveSheet()->setCellValue('D1', $resnames['section_name']); $objPHPExcel->getActiveSheet()->setCellValue('E1', 'Exam Name'); $objPHPExcel->getActiveSheet()->setCellValue('F1', $exname['exam_name']); $objPHPExcel->getActiveSheet()->setCellValue('A2',"" ); $objPHPExcel->getActiveSheet()->setCellValue('B2', ""); if($nosub) { $i=0; $j=3; while($res=mysql_fetch_array($exe)) { $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($j,1,$res['subject_name']); $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($j+1,1,""); $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($j+2,1,""); $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($j,1,$res['total_marks']); $total_marks[$i]=$res['total_marks']; $j=$j+3; $i++; } $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($j-1,1,"Grades/Rank"); } $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0,2,"Roll Number"); $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1,2,"Student Name"); for($i=1,$j=2;$i<=$nosub;$i++) { $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($j,2,"Total Marks"); $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($j+1,2,"Secured Marks*"); $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($j+2,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']; $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0,$row,$rec['student_rollno']); $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1,$row,$stdname); for($i=0;$i<$nosub;$i++) { $objPHPExcel->getActiveSheet()->getProtection()->setSheet(true); $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($subs,$row,$total_marks[$i]); $objPHPExcel->getActiveSheet()->getStyle($subs)->getProtection()->setLocked(PHPExcel_Style_Protection::PROTECTION_PROTECTED); $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($subs+1,$row,""); $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($subs+2,$row,""); $subs=$subs+3; } $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($subs+2,$row,"<select><option value='Pass'>Pass</option><option value='Fail'>Fail</option></select>"); $row++; $subs=2; } } // Rename 2nd sheet $objPHPExcel->getActiveSheet()->setTitle('Student_marks'); // Redirect output to a client’s web browser (Excel5) header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="name_of_file.xls"'); header('Cache-Control: max-age=0'); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); $objWriter->save('php://output');