Quantcast
Channel: PHPExcel Forum Rss Feed
Viewing all articles
Browse latest Browse all 2707

New Post: PHPExcel Set cell with variable

$
0
0
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;

?>

Viewing all articles
Browse latest Browse all 2707

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>