【问题标题】:How to get the font name of the excel cell如何获取excel单元格的字体名称
【发布时间】:2017-05-13 15:48:23
【问题描述】:

我正在迭代 excel 工作表的所有单元格并打印出值。同时我也想打印他们正在使用的字体名称。是否有任何函数可以在 phpexcel 中获取特定的单元格字体名称?谢谢。 下面是代码sn-p

 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";
}
  ?>

【问题讨论】:

    标签: php phpexcel phpexcelreader phpexcel-1.8.0


    【解决方案1】:

    字体是单元格样式的一个方面;因此您需要获取单元格的样式详细信息,并从中读取字体信息:

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

    请注意,您还可以通过类似的方式获取字体大小、斜体、粗体、下划线、上标/下标、删除线和字体颜色……字体对象包含的信息不仅仅是字体名称。

    【讨论】:

    • 谢谢!它有效.. 如果我在同一个单元格中有多种字体怎么办?
    • 如果您在同一个单元格中有多种字体,那么当您执行$cell-&gt;getValue() 时,您正在检索一个富文本对象,而不仅仅是一个标量值;您可以将单元格的内容视为一系列具有不同样式的运行
    猜你喜欢
    • 2016-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多