JezK
Edit File: student_markstemplate.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(); $colarray=array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'); // Create a first sheet, representing sales data $objPHPExcel->setActiveSheetIndex(0); $objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(30); $objPHPExcel->getActiveSheet()->getRowDimension('2')->setRowHeight(30); $objPHPExcel->getActiveSheet()->getRowDimension('3')->setRowHeight(30); $objPHPExcel->getActiveSheet()->getRowDimension('4')->setRowHeight(30); $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(100); $objPHPExcel->getActiveSheet()->setCellValue('A1', "Please follow the below instruction and upload the student marks"); $objPHPExcel->getActiveSheet()->setCellValue('A2', "Please don't edit these fields; Class, Section, Exam Name, Roll Number, Student Name & Total Marks"); $objPHPExcel->getActiveSheet()->setCellValue('A3', "Please keep 'Pass' or 'Fail' text only for 'Status' field"); $objPHPExcel->getActiveSheet()->setCellValue('A4', "Please do not copy and paste one field into another"); // 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()->getRowDimension('2')->setRowHeight(20); $objPHPExcel->getActiveSheet()->getRowDimension('3')->setRowHeight(20); $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20); $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(25); $objPHPExcel->getActiveSheet()->setCellValue('A2', 'Class'); $objPHPExcel->getActiveSheet()->setCellValue('B2', $resnames['class_name']); $objPHPExcel->getActiveSheet()->setCellValue('C2', 'Section'); $objPHPExcel->getActiveSheet()->setCellValue('D2', $resnames['section_name']); $objPHPExcel->getActiveSheet()->setCellValue('E2', 'Exam Name'); $objPHPExcel->getActiveSheet()->setCellValue('F2', $exname['exam_name']); $objPHPExcel->getActiveSheet()->setCellValue('A3',"" ); $objPHPExcel->getActiveSheet()->setCellValue('B3', ""); if($nosub) { $i=0; $j=3; while($res=mysql_fetch_array($exe)) { $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($j,3,$res['subject_name']); $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($j+1,3,""); $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($j+2,3,""); $total_marks[$i]=$res['total_marks']; $j=$j+3; $i++; } $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($j-1,3,"Grades/Rank"); } $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0,4,"Roll Number"); $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1,4,"Student Name"); for($i=1,$j=2;$i<=$nosub;$i++) { $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($j,4,"Total Marks"); $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($j+1,4,"Secured Marks*"); $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($j+2,4,"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); $procells=mysql_num_rows($execute)+4; if(mysql_num_rows($execute)) { $row=5; $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()->setCellValueByColumnAndRow($subs,$row,$total_marks[$i]); $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($subs+1,$row,""); $objValidation2 = $objPHPExcel->getActiveSheet()->getCellByColumnAndRow($subs+2,$row)->getDataValidation(); $objValidation2->setType(PHPExcel_Cell_DataValidation::TYPE_LIST); $objValidation2->setErrorStyle(PHPExcel_Cell_DataValidation::STYLE_INFORMATION); $objValidation2->setAllowBlank(false); $objValidation2->setShowInputMessage(true); $objValidation2->setShowDropDown(true); $objValidation2->setPromptTitle('Pick from list'); $objValidation2->setPrompt('Please pick a value from the drop-down list.'); $objValidation2->setErrorTitle('Input error'); $objValidation2->setError('Value is not in list'); $objValidation2->setFormula1('"PASS,FAIL"'); $subs=$subs+3; } $row++; $subs=2; } //$objPHPExcel->getActiveSheet()->getStyle('C1:C10')->getProtection()->setLocked(PHPExcel_Style_Protection::PROTECTION_PROTECTED); for($first=1;$first<=($nosub*3)+3;$first++) $objPHPExcel->getActiveSheet()->setCellValue($colarray[$first-1].'1', 'Column'.$first); $objSheet = $objPHPExcel->getActiveSheet(); for($first=0;$first<=($nosub*3)+3;$first++) { if(($first%3==0) && $first<($nosub*3)+3) { // UNPROTECT THE CELL RANGE $objSheet->getStyle($colarray[$first].'4:'.$colarray[$first].''.$procells)->getProtection() ->setLocked(PHPExcel_Style_Protection::PROTECTION_UNPROTECTED); $objSheet->getStyle($colarray[$first+1].'4:'.$colarray[$first+1].''.$procells)->getProtection() ->setLocked(PHPExcel_Style_Protection::PROTECTION_UNPROTECTED); } } $objSheet->getStyle($colarray[($nosub*3)+2].'4:'.$colarray[($nosub*3)+2].''.$procells)->getProtection() ->setLocked(PHPExcel_Style_Protection::PROTECTION_UNPROTECTED); // PROTECT THE WORKSHEET SHEET $objSheet->getProtection()->setSheet(true); $objSheet->getStyle('A5:B'.$procells)->getProtection()->setLocked(PHPExcel_Style_Protection::PROTECTION_PROTECTED); } // 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="student_marks.xls"'); header('Cache-Control: max-age=0'); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); $objWriter->save('php://output');