phpphpexcelphpexcelreaderphpexcel-1.8.0

How to get the font name of the excel cell


I am iterating all cells of the excel sheet and printing out the values. Same time i also want to print the font name they are using. Is there any function to get the specific cell font name in phpexcel ? Thanks. below is the code snippet

 include('lib/phpexcel/Classes/PHPExcel/IOFactory.php');

  //Use whatever path to an Excel file you need.
  $inputFileName = 'text.xlsx';

  try {
    $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
    $objReader = PHPExcel_IOFactory::createReader($inputFileType);
    $objPHPExcel = $objReader->load($inputFileName);
  } catch (Exception $e) {
    die('Error loading file "' . pathinfo($inputFileName, PATHINFO_BASENAME) . '": ' . 
        $e->getMessage());
  }

  $sheet = $objPHPExcel->getSheet(0);
  $highestRow = $sheet->getHighestRow();
  $highestColumn = $sheet->getHighestColumn();

  foreach ($sheet->getRowIterator() as $row) {
  echo '<tr>' . "\n";
  $cellIterator = $row->getCellIterator();
  $cellIterator->setIterateOnlyExistingCells(false); 
  foreach ($cellIterator as $cell) {
// Is there any similar $cell->getFont() function ?? which will echo"time new roman" 
    echo '<td>' .$cell->getValue(). '</td>' . "\n";
  }

  echo '</tr>' . "\n";
}
  ?>

Solution

  • The font is an aspect of the style of a cell; so you need to get the style details for the cell, and read the font information from that:

    $cell->getStyle()
        ->getFont()
        ->getName();
    

    Note that you can also get font size, italic, bold, underlining, super/subscript, strikethru, and the font colour in a similar manner.... the font object holds more information than simply the font name.