looping through database createSheet()

Topics: Developer Forum, User Forum
Nov 12, 2009 at 3:51 PM

I have 100 students taking exams for a single course.  Each course has numerous Team-based Learning (TBL) "sessions" where they take three exams.  How could I use createSheet(); to put each session on its own worksheet?

Each worksheet would have each of the three exams on it.  One course has up to 12 TBL sessions so it would have 12 worksheets.

Each worksheet would need it's own labels at the top in the first row. (I've tried to bold these but no luck so far.)

$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'Last Name')
            ->setCellValue('B1', 'First Name')
            ->setCellValue('C1', 'UID')
            ->setCellValue('D1', 'Group')
            ->setCellValue('E1', 'Session')
            ->setCellValue('F1', 'Session Name')
            ->setCellValue('G1', 'Irat')
            ->setCellValue('H1', 'Grat')
            ->setCellValue('I1', 'Appex');

Then

 

$i=2;
while ($row = $db->sql_fetchrow($result)) {

$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A'.$i, $row['Name_Last'])
            ->setCellValue('B'.$i, $row['Name_First'])
            ->setCellValue('C'.$i, $row['UID'])
            ->setCellValue('D'.$i, $row['Group_Name'])
            ->setCellValue('E'.$i, $row['Session_Detail'])
            ->setCellValue('F'.$i, $row['Session_Name'])
            ->setCellValue('G'.$i, $row['IRAT_Raw'])
            ->setCellValue('H'.$i, $row['GRAT_Raw'])
            ->setCellValue('I'.$i, $row['AppEx_Raw']);
            
     $i++;
 }


Basically when the session_id changes I need to createSheet();

Can someone give me an example?

 

Developer
Nov 12, 2009 at 10:57 PM

Basically you are asking how to create many sheets and fill them with data?

In this example you end up with 10 sheets. Maybe it helps you a bit:

 

$objPHPExcel = new PHPExcel();

$objPHPExcel->getActiveSheet()->setTitle('Sheet0');
$objPHPExcel->getActiveSheet()->getCell('A1')->setValue('This is Sheet0');

for ($i = 1; $i <= 9; $i++) {
    $objPHPExcel->createSheet();
    $objPHPExcel->setActiveSheetIndex($i);
    $objPHPExcel->getActiveSheet()->setTitle("Sheet$i");
    $objPHPExcel->getActiveSheet()->getCell('A1')->setValue("This is Sheet$i");
}

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('write.xlsx');