Jump to content

PHPExcel Set cell with variable


kms

Recommended Posts

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;

?>
Link to comment
https://forums.phpfreaks.com/topic/284206-phpexcel-set-cell-with-variable/
Share on other sites

Maybe you should also be using http://us3.php.net/manual/en/function.array-count-values.php

I don't see where you're getting the number of BWMs in the $cars array.

 

Also, each()  returns an array with 4 items. Your list() is only accounting for 2 of those 4.

http://us3.php.net/manual/en/function.each.php

Your code is over complicated.

$carCounts = array_count_vaules($cars);
$carIdx = 0;
foreach ($carCounts as $car=>$cnt){
     # the car name (BWM, Volvo, etc) is available in $car   
     $objWorksheet->setCellValue('A'.($cnt+5) , 'Comments:');
     $carIdx++;  # if you need the index of the car
}

Looks like this will do it, thanks for the help.

<?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;

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.