-1

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";
}
  ?>
Abhigyan
  • 641
  • 10
  • 25

1 Answers1

2

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.

Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • thanks ! it worked.. and what if i have multiple fonts in same cell ? – Abhigyan May 14 '17 at 15:11
  • 1
    If you have multiple fonts in the same cell, then you're retrieving a rich text object when you do `$cell->getValue()`, not simply a scalar value; and you can look at the content of the cells as a series of runs with different stylings – Mark Baker May 14 '17 at 16:59