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

New Post: PHPExcel Set cell with variable

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

?>

Viewing all articles
Browse latest Browse all 2707

Trending Articles



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