PHPExcel Set cell with variable

Topics: Developer Forum, Project Management Forum, User Forum
Nov 23, 2013 at 4:11 AM
I have this test script that is acting just like my production script except production script pulls from a SQL db. I want to set the cell placement using a variable based off the count of an array. In the script below, (which you can copy and run as an example) I want to set the cell number based off how many of the same car that is in an array. So for an example there are 7 BMW's listed in the array. So I want the Comments: data copied to cell A12 using the code $num=$no+5; $cell='A'.$num;

The problem I am having is it does place it on that line but it also places it 7 more times about it. If there are 4 cars of that type it would place it on the right line but also place it 3 times above it. I just want it to place it once at the desired location. Any help would be great. Here is the code:
<?PHP

require_once 'Classes/PHPExcel.php';
    include 'Classes/PHPExcel/Writer/Excel2007.php';
    
$dataArray= array();
$cars=array("Versa","Volt","Volt","Volt","Volt","Volkswagen","Bentley","Benz","BMW","BMW","BMW","BMW","BMW","BMW","BMW","Cobra","Cord","Daewoo","Datsun","Dodge","Dodge","Dixi");




$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);

while (list($var, $val) = each($cars)) {

if ($val!=$id){

$dataArray = array();
     
    $objWorksheet = new PHPExcel_Worksheet($objPHPExcel);
    $objPHPExcel->addSheet($objWorksheet);
    $objWorksheet->setTitle(''. $val);
        
        $row_array[$val] = $val;
        
        array_push($dataArray,$row_array);
        $no = count($dataArray);
        $num=$no+5;
    $cell='A'.$num;
    
$objWorksheet->setCellValue('A1' , $no);
$objWorksheet->setCellValue('A2' , $val);
$objWorksheet->setCellValue($cell , 'Comments:');
$id = $val; 
    }
    else {
    
    
    
    $row_array[$val] = $val;
        $count=count($cars);
        $count2=count($loc);
        array_push($dataArray,$row_array);
    $no = count($dataArray);
    $num=$no+5;
    $cell='A'.$num;
    
$objWorksheet->setCellValue('A2' , $no);
$objWorksheet->setCellValue('A3' , $val);

$objWorksheet->setCellValue($cell , 'Comments:');
    
    $id = $val; 
    
    }
    }
    
    // Save Excel 2007 file
#echo date('H:i:s') . " Write to Excel2007 format\n";
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
ob_end_clean();
// We'll be outputting an excel file
header('Content-type: application/vnd.ms-excel');
// It will be called file.xls
header('Content-Disposition: attachment; filename="cars.xlsx"');
$objWriter->save('php://output');
Exit;

?>
Nov 25, 2013 at 12:51 PM
I'm not sure I understood exactly the need, however, you will find below a code that achieves what I understood.
Each mark is on his own sheet, each sheet has one line per vehicle of the mark (recalled in column B), the word 'comment' is a few lines further down.
<?PHP
require_once 'Excel/PHPExcel.php';//path for my config, rewrite for yours
//include 'Classes/PHPExcel/Writer/Excel2007.php'; // not needed, lazy loader job
    
$cars=array("Versa","Volt","Volt","Volt","Volt","Volkswagen","Bentley","Benz","BMW","BMW","BMW","BMW","BMW","BMW","BMW","Cobra","Cord","Daewoo","Datsun","Dodge","Dodge","Dixi");

$objPHPExcel = new PHPExcel();
$objWorksheet=$objPHPExcel->setActiveSheetIndex(0);
$id='';
$countRows=0;
while (list($var, $val) = each($cars)) {
    if ($val!=$id && $id!=''){
        $objWorksheet->setTitle($id);
        $num=$countRows+5;
        $cell='A'.$num;
        
        $objWorksheet->setCellValue($cell , 'Comments:');
        
        $objWorksheet = new PHPExcel_Worksheet($objPHPExcel);
        $objPHPExcel->addSheet($objWorksheet);
        
        $id = $val;
        $countRows=0;
    }//end if
    if($id=='') $id=$val;//the first car
    $no = ++$countRows;
    $objWorksheet->setCellValue('A'.$no , $no);
    $objWorksheet->setCellValue('B'.$no , $id);//Versa, Volt, ...
}
if($countRows>0 && $id!=''){// the last car - if $id=='' the workbook is empty
    $objWorksheet->setTitle($id);
    $num=$countRows+5;
    $cell='A'.$num;
    
    $objWorksheet->setCellValue($cell , 'Comments:');
}//end if
    
// Save Excel 2007 file
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
ob_end_clean();
// We'll be outputting an excel file
header('Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); // note : use correct mime type (not xls for xlsx)
// It will be called cars.xlsx
header('Content-Disposition: attachment; filename="cars.xlsx"');
$objWriter->save('php://output');
Exit;

?>
Marked as answer by KMS77 on 11/26/2013 at 5:46 AM
Nov 25, 2013 at 6:57 PM
Edited Nov 26, 2013 at 1:45 PM
Thanks for the help. I was able to take what you provided above and work it into the prod code. Thanks for the help!
Nov 27, 2013 at 12:38 PM
Edited Nov 27, 2013 at 12:42 PM
(del)