Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP Excel读取XLS文件时,如何检测单元格是否为粗体?_Php_Formatting_Phpexcel - Fatal编程技术网

使用PHP Excel读取XLS文件时,如何检测单元格是否为粗体?

使用PHP Excel读取XLS文件时,如何检测单元格是否为粗体?,php,formatting,phpexcel,Php,Formatting,Phpexcel,我有一个Excel 2007 xlsx文档,在单元格L2中有“BC/B22--”。 它是粗体的,颜色是红色的。 使用PHPExcel(v1.8.012014-03-02),我想检测单元格是否粗体,但我只能获取值“BC/B22-”,而没有样式信息 这是我的代码: $inputFileType = PHPExcel_IOFactory::identify($sFilepath); $objReader = PHPExcel_IOFactory::createReader($inputFileType

我有一个Excel 2007 xlsx文档,在单元格L2中有“BC/B22--”。 它是粗体的,颜色是红色的。 使用PHPExcel(v1.8.012014-03-02),我想检测单元格是否粗体,但我只能获取值“BC/B22-”,而没有样式信息

这是我的代码:

$inputFileType = PHPExcel_IOFactory::identify($sFilepath);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);  
$objReader->setReadDataOnly(false);
$objPHPExcel = $objReader->load($sFilepath);
$sheet = $objPHPExcel->setActiveSheetIndex(0); 
$cell = $sheet->getCellByColumnAndRow(11,2);

$v = $cell->getValue();
var_dump($v);
// Echoes: string(8) "BC/B22--" 

$rte = $cell->getValue()->getRichTextElements();
// Error: "Call to a member function getRichTextElements() on a non-object"

echo ($cell->getValue() instanceof PHPExcel_RichText) ? 
    "instance" : "no instance";
// Echoes "no instance"

这完全取决于单元格是否包含简单文本内容并将粗体设置为单元格样式,或者单元格内容是否为富文本对象

如果
$cell->getValue()
返回字符串,则需要测试单元格的样式:

$isBold = $cell->getStyle()->getFont()->getBold();
否则,如果
$cell->getValue()
返回一个富格文本对象,则需要遍历该富格文本对象进行搜索,以查看其任何部分是否为粗体:

$isBold = false;
$elements = $cell->getValue()->getRichTextElements();
foreach ($elements as $element) {
    if ($element instanceof PHPExcel_RichText_Run) {
        $isBold |= $element->getFont()->getBold();
    }
}

这完全取决于单元格是否包含简单文本内容并将粗体设置为单元格样式,或者单元格内容是否为富文本对象

如果
$cell->getValue()
返回字符串,则需要测试单元格的样式:

$isBold = $cell->getStyle()->getFont()->getBold();
否则,如果
$cell->getValue()
返回一个富格文本对象,则需要遍历该富格文本对象进行搜索,以查看其任何部分是否为粗体:

$isBold = false;
$elements = $cell->getValue()->getRichTextElements();
foreach ($elements as $element) {
    if ($element instanceof PHPExcel_RichText_Run) {
        $isBold |= $element->getFont()->getBold();
    }
}

完美的它确实返回了一个字符串,并且您的代码示例检测到bold.Perfect。它确实返回了一个字符串,并且您的代码示例检测到粗体。