PHPExcel Make first row bold

PhpPhpexcel

Php Problem Overview


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.

Php Solutions


Solution 1 - Php

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 - Php

Try this

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

Solution 3 - Php

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

That way you get the complete first row

Solution 4 - Php

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 - Php

Use this:

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

Solution 6 - Php

These are some tips to make your cells Bold, Big font, Italic

Let's say I have columns from A to L

A1 - is your starting cell

L1 - is your last cell

$objPHPExcel->getActiveSheet()->getStyle('A1:L1')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('A1:L1')->getFont()->setSize(16);
$objPHPExcel->getActiveSheet()->getStyle('A1:L1')->getFont()->setItalic(true);

Solution 7 - Php

$objPHPExcel->getActiveSheet()->getStyle("A1:".$objPHPExcel->getActiveSheet()->getHighestDataColumn()."1")->getFont()->setBold(true);

I found this to be a working solution, you can replace the two instances of 1 with the row number. The HighestDataColumn function returns for example C or Z, it gives you the last/highest column that's in the sheet containing any data. There is also getHighestColumn(), that one would include cells that are empty but have styling or are part of other functionality.

Solution 8 - Php

This iterates through a variable number of columns of a particular row, which in this case is the 1st row:

$rownumber = 1;
$row = $this->objPHPExcel->getActiveSheet()->getRowIterator($rownumber)->current();

$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false);

foreach ($cellIterator as $cell) {
    $cell->getStyle()->getFont()->setBold(true);
}

Solution 9 - Php

The simple way to make bold headers:

$row = 1;
foreach($tittles as $index => $tittle) {
    $worksheet->getStyleByColumnAndRow($index + 1, $row)->getFont()->setBold(true);
    $worksheet->setCellValueByColumnAndRow($index + 1, $row, $tittle);
}

Solution 10 - Php

Try this

$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
->setLastModifiedBy("Maarten Balliauw")
->setTitle("Office 2007 XLSX Test Document")
->setSubject("Office 2007 XLSX Test Document")
->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
->setKeywords("office 2007 openxml php")
->setCategory("Test result file");
$objPHPExcel->setActiveSheetIndex(0);
$sheet = $objPHPExcel->getActiveSheet();
$sheet->setCellValue('A1', 'No');
$sheet->setCellValue('B1', 'Job ID');
$sheet->setCellValue('C1', 'Job completed Date');
$sheet->setCellValue('D1', 'Job Archived Date');
$styleArray = array(
'font' => array(
'bold' => true
)
);
$sheet->getStyle('A1')->applyFromArray($styleArray);
$sheet->getStyle('B1')->applyFromArray($styleArray);
$sheet->getStyle('C1')->applyFromArray($styleArray);
$sheet->getStyle('D1')->applyFromArray($styleArray);
$sheet->getPageSetup()->setRowsToRepeatAtTopByStartAndEnd(1, 1);

This is give me output like below link.(https://www.screencast.com/t/ZkKFHbDq1le)

Solution 11 - Php

You can try

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRakeshView Question on Stackoverflow
Solution 1 - PhpAli MezalView Answer on Stackoverflow
Solution 2 - PhpSadikhasanView Answer on Stackoverflow
Solution 3 - PhpednincerView Answer on Stackoverflow
Solution 4 - PhpboojerView Answer on Stackoverflow
Solution 5 - PhpTomView Answer on Stackoverflow
Solution 6 - PhpVivekView Answer on Stackoverflow
Solution 7 - PhpArieView Answer on Stackoverflow
Solution 8 - Phpdatasn.ioView Answer on Stackoverflow
Solution 9 - PhpCorrecterView Answer on Stackoverflow
Solution 10 - Phpsuman chauhanView Answer on Stackoverflow
Solution 11 - Phpuser3808975View Answer on Stackoverflow