PHPExcel Make first row bold

I am trying to make cells in first row are bold.

This is the method I have created for that purpose.

function ExportToExcel($tittles,$excel_name)
 {
  $objPHPExcel = new PHPExcel();
  $objRichText = new PHPExcel_RichText();
  // Set properties
  $objPHPExcel->getProperties()->setCreator("SAMPLE1");
  $objPHPExcel->getProperties()->setLastModifiedBy("SAMPLE1");
  $objPHPExcel->getProperties()->setTitle("SAMPLE1");
  $objPHPExcel->getProperties()->setSubject("SAMPLE1");
  $objPHPExcel->getProperties()->setDescription("SAMPLE1");


  // Add some data
  $objPHPExcel->setActiveSheetIndex(0);

  $letters = range('A','Z');
  $count =0;
  $cell_name="";
  foreach($tittles as $tittle)
  {
   $cell_name = $letters[$count]."1";
   $count++;
   $value = $tittle;
   $objPHPExcel->getActiveSheet()->SetCellValue($cell_name, $value);
   // Make bold cells
   $objPHPExcel->getActiveSheet()->getStyle($cell_name)->getFont()->setBold(true);
  }
  // Save Excel 2007 file
  $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
  //$objWriter->save(str_replace('.php', '.xlsx', __FILE__));
  $objWriter->save($excel_name.".xlsx");
 }

The problem is in output excel file the cells are not bold.


Solution 1:

Try this for range of cells:

$from = "A1"; // or any value
$to = "B5"; // or any value
$objPHPExcel->getActiveSheet()->getStyle("$from:$to")->getFont()->setBold( true );

or single cell

$cell_name = "A1";
$objPHPExcel->getActiveSheet()->getStyle( $cell_name )->getFont()->setBold( true );

hope that helps

Solution 2:

Try this

$objPHPExcel->getActiveSheet()->getStyle('A1:D1')->getFont()->setBold(true);

Solution 3:

$objPHPExcel->getActiveSheet()->getStyle('1:1')->getFont()->setBold(true);

That way you get the complete first row

Solution 4:

Assuming headers are on the first row of the sheet starting at A1, and you know how many of them there are, this was my solution:

$header = array(
    'Header 1',
    'Header 2'
);

$objPHPExcel = new PHPExcel();
$objPHPExcelSheet = $objPHPExcel->getSheet(0);
$objPHPExcelSheet->fromArray($header, NULL);
$first_letter = PHPExcel_Cell::stringFromColumnIndex(0);
$last_letter = PHPExcel_Cell::stringFromColumnIndex(count($header)-1);
$header_range = "{$first_letter}1:{$last_letter}1";
$objPHPExcelSheet->getStyle($header_range)->getFont()->setBold(true);

Solution 5:

Use this:

$sheet->getStyle('A1:'.$sheet->getHighestColumn().'1')->getFont()->setBold(true);