phpphpexcelcolumn-sizing

changing the size of column using PHPExcel


I met some troubles while using PHPExcel,

In fact I have read all the tutorials I've found but it does not work for me.

I've put this code on the page:

$i = -1; // index des enregistrements

$objPHPExcel->setActiveSheetIndex(0);
$sheet->getColumnDimension('A')->setWidth(75);
$sheet->getColumnDimension('B')->setWidth(150);
$sheet->getColumnDimension('C')->setWidth(190);
$sheet->getColumnDimension('D')->setWidth(150);
$sheet->getColumnDimension('E')->setWidth(115);
$sheet->getColumnDimension('F')->setWidth(115);
$sheet->getColumnDimension('G')->setWidth(380);
$sheet->getColumnDimension('H')->setWidth(75);
$objPHPExcel->getActiveSheet()->SetCellValue('D1', '<img src="images/agenda.png" alt="AGENDA" width="168" height="24">');
$objPHPExcel->getActiveSheet()->SetCellValue('A3', 'Du '.$_REQUEST['date1'].' Au '.$_REQUEST['date2'].', Critères: '.$_REQUEST['tache'] );
$objPHPExcel->getActiveSheet()->SetCellValue('A5', 'CLIENT');
$objPHPExcel->getActiveSheet()->SetCellValue('B5', 'N° DE DOSSIER');
$objPHPExcel->getActiveSheet()->SetCellValue('C5', 'DEBITEUR');
$objPHPExcel->getActiveSheet()->SetCellValue('D5', 'TEL');
$objPHPExcel->getActiveSheet()->SetCellValue('E5', 'DATE');
$objPHPExcel->getActiveSheet()->SetCellValue('F5', 'TRAITEMENT');
$objPHPExcel->getActiveSheet()->SetCellValue('G5', 'DESCRIPTION');
$objPHPExcel->getActiveSheet()->SetCellValue('H5', 'RESTANT DU EN PPAL');

But it give to me the following errors:

Notice: Undefined variable: sheet in Line 173

Notice: Undefined variable: sheet in Line 173

I'm really Lost, I thought it wad that way for changing column sizes.

Any kind of help will be much appreciated.

Kind Regards.

SP.


Solution

  • You must define $sheet before using it, like any variable in PHP it doesn't exist until it's actually created; but you also want it to be the actual PHPExcel worksheet where you want to set the column widths.

    So

    instead of

    $objPHPExcel->setActiveSheetIndex(0);
    

    use

    $sheet = $objPHPExcel->setActiveSheetIndex(0);
    

    which works because a call to the setActiveSheetIndex() returns the active worksheet object

    Once $sheet has been defined as the current Worksheet, you can make your calls to set the column widths

    EDIT

    Then you can also simplify your calls to set cell values from

    $objPHPExcel->getActiveSheet()->SetCellValue('D1', '<img src="images/agenda.png" alt="AGENDA" width="168" height="24">
    

    to

    $sheet->SetCellValue('D1', '<img src="images/agenda.png" alt="AGENDA" width="168" height="24">
    

    etc

    Note however that setting HTML markup in a cell is simply setting cell to contain a text string, this will not embed an image in your worksheet