0

I would like to write into a csv file using PHPEXCEL in the following format. The class in bold and a space after every class group.

Format wanted

But i have not been able to find a way to do it.

Here is what I could do.

The format i have

I generate the data from an sql and loop thought it. Here is my code

$sql="SELECT * FROM table ORDER BY typeclass"; $query=mysql_query($sql); $check=''; $i=1; while($row=mysql_fetch_assoc($query)) { if($row['class']!=$check) { $objPHPExcel->getActiveSheet()->setCellValue('A'.$i,$row['Class']); $check=$row['class']; } $objPHPExcel->getActiveSheet()->setCellValue('B' .$i,$row['Name']); $objPHPExcel->getActiveSheet()->setCellValue('C' .$i,$row['score']); $i++; } 

Can anyone help me get the desired output.

1
  • Note that writing as a CSV file will lose all formatting: a CSV file only supports data, not formatting. If you want formatting like bold, then you need to save in a file format that supports it such as xls (the Excel5 Writer) or xlsx (the Excel2007 Writer) Commented Nov 8, 2012 at 19:24

1 Answer 1

1

How to format a cell bold:

$styleArray = array('font' => array('bold' => true)); $objPHPExcel->getActiveSheet()->getStyle('A1')->applyFromArray($styleArray); 

You can insert a new Row after inserting the class name.

 if($row['class']!=$check) { $objPHPExcel->getActiveSheet()->setCellValue('A'.$i,$row['Class']); $check=$row['class']; // Merge the cells with the class name $objPHPExcel->getActiveSheet()->mergeCells('A'.$i.':B'.$i); $i++; // Next Row } 

Then you can put the name and score into column A and B:

$objPHPExcel->getActiveSheet()->setCellValue('A' .$i,$row['Name']); $objPHPExcel->getActiveSheet()->setCellValue('B' .$i,$row['score']); 

That should do all the trick. For further questions using PHPEXCEL youre able to contact me via email.

By the way, poor Simon :(

Sign up to request clarification or add additional context in comments.

3 Comments

It worked like a charm. Thanks. Now how would I add a space between the first group and the second?
at the beginning of the if statement ($row['class']!=$check) use if($bolSpace) $i++; at the end of the if statement set $bolSpace = true; This just adds a blank row, if its not the first class group.
what is ur email. I have a few more questions anout PHPEXCEL

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.